Project 1
Computers are almost everywhere today — from laptops, tablets, or phones, to TVs, watches, medical devices, kitchen appliances, cars, spaceships, big factories, little robots, and millions of other places large and small.
How do computers know what to do inside all these things? Someone has to teach them! Behind every cool animated movie, website, game, vehicle, or device, someone has worked hard to instruct a computer on how to perform its task. That person was a programmer.
In this chapter, I give you a little background about programming and how programmers organize their thoughts when writing computer software or code. I share some background about Ruby, the programming language I cover throughout this book. Then I tell you how to install the tools you’ll use for all the projects in the rest of the book.
Computers are kind of dumb by themselves. Without a person to tell it exactly what to do, a computer will just sit there. Everything a computer does — and I mean everything, from the display of pictures and text on a screen, to the understanding of what you type on a keyboard or touch and swipe on a tablet — requires some software to interpret signals coming through the various circuits in one part of the computer and modify and send them to the right place in another part to get something done. That’s a lot of work!
Fortunately, over the years, many smart people have come up with different ways to communicate clearly with computers. Writing instructions for a computer is called programming or coding, and the end result is a program or software.
A computer programming language shares many similarities to a human language. It has symbols and words (like nouns and verbs) that you put together following a syntax (rules for spelling, order, and punctuation).
When you start learning to program, you open up a wide world in which you can apply this knowledge when working with any technology that uses computers. You’ll be able to read other people’s programs to learn more about computers or to use code you write to solve homework problems, create puzzles, build a new game, create a website, or even control machines like robots.
Programs needs to be very precise in order to instruct a computer to do something. Imagine that you want to tell your friend to do something. For instance, how would you tell someone to sit down in a desk chair? You might say:
Your friend is smart enough that your instructions make perfect sense, and she’ll sit on the chair safely without falling over or anything crazy like that. People have a lot of knowledge they can use to interpret instructions like this.
Now, if you have to tell a computer to sit down, what would that be like? You have to be a lot more exact. For example, you would have to say:
Even these instructions might not be enough for a computer because they make some assumptions (like what your body parts are called).
Try it yourself: How would you tell a computer exactly how to do something like filling a glass with water?
Programmers need to think in this very detail-oriented way. As you learn to write computer programs, you’ll get good at breaking a problem down into smaller and smaller parts. Each of those parts will eventually be a line of code that you create. Over time, you’ll learn other techniques that help you identify the different objects you’ll need to describe to the computer and the actions those objects will take. This will help you organize your code in ways that make it possible to create very sophisticated software. Pretty cool, huh?
There are many different computer programming languages out there. Each language has strengths and weaknesses. Some languages are easier if you’re trying to control large machines. Some languages are specialized for mobile apps — the kind on an iPhone, for example. Some languages make it easy to create websites. And some languages are for doing science and engineering.
A general-purpose programming language is good for many different kinds of projects. There are many general-purpose programming languages to choose from. The important thing when you’re wanting to learn programming is to pick something and dive into training yourself to think like a programmer. When you learn one programming language, learning another one is much, much easier.
In this book, I use the language Ruby. Ruby is a flexible, general-purpose language that is useful for many kinds of projects. It was created in the mid-1990s in Japan by Yukihiro Matsumoto (best known by his nickname, “Matz”). Don’t worry — you don’t have to learn Japanese to program with Ruby! Today Ruby is used around the world for all kinds of projects, by beginners and professionals alike.
Matz had a wonderful philosophy in mind when creating Ruby: He wanted programmers to be productive, enjoy programming, and be happy. This is one of my favorite things about Ruby: As you learn it and write programs, you’ll have fun!
Most obviously, you need a computer that’s running a current version of a consumer desktop operating system (Mac OS X or Windows).
For the projects in this book, you need only a few basic tools, and they’re all free.
First, you need Ruby installed, as well as some other software that helps Ruby use the capabilities of your computer. I walk you through how to install Ruby in this section.
Second, you need a text editor that is specifically for coding. Word processors don’t work well when coding, so you’ll use a tool that is built for programmers. There are a number of good, free code editors out there, and I help you install one of them in this section. (You may use any other editing program you like as long as it’s a code editor of some kind.)
To run Ruby on Windows, you have to install Ruby and several developer tools. The following instructions have been tested with Windows 8 and 8.1.
http://rubyinstaller.org
in your web browser.Click the big red Download button.
A list of RubyInstallers appears.
Click Ruby 2.2.2 near the top of the RubyInstallers list (see Figure 1-1).
Do not click Ruby 2.2.2 (x64).
An installer program downloads to your computer.
Run the installer program by choosing Run Program (if Windows presents this option) or double-clicking the file when it’s done downloading.
The installer will ask you to select a language to use during installation. Accept the license, and then the installer will have you set some configuration options. Leave the default folder choice alone, but uncheck the Install Tcl/Tk Support check box (you won’t be using it for this book), and make sure that the other two check boxes — Add Ruby Executables to Your PATH and Associate .rb and .rbw Files with This Ruby Installation — are selected (see Figure 1-2).
When the installer is done, it will have created a topmost folder with all the Ruby software on your C: drive called C:Ruby22. You can use Windows 8 Desktop and the File Explorer to confirm that it’s there (as shown in Figure 1-3).
You must also download the Development Kit from http://rubyinstaller.org
to get some of the cool tools used by the projects in this book. Follow these steps:
http://rubyinstaller.org
in your web browser.Scroll down to the Development Kit section and click the file under “For use with Ruby 2.0 and above (32bits version only)” (see Figure 1-4).
An installer program downloads to your computer.
Run the Development Kit installer by choosing Run Program (if Windows presents this option) or double-clicking the file when it finishes downloading.
The installer will ask you where to put the kit. You want to put the kit in its own folder, not in the Ruby folder you selected in Step 1. To make things easy for the projects in this book, select a folder next to the Ruby22 one at the top of your C: drive.
Now you need some final setup. Follow these steps:
Click the Start Command Prompt with Ruby program (my machine looks like Figure 1-6).
If you have a lot of programs installed in your launch screen, use the search feature and type command to help narrow down the choices.
In the command prompt application, you type commands to get things done. Typing commands is a low-level way of working with a computer. Before the mouse and graphical interface was invented, this was the only way to tell the computer what to do!
Change your location to the Developer Kit folder.
When you first open the command prompt application, you’re usually in your home directory. To complete setup, you need to move to the DevKit directory. The display will show you a prompt that gives a hint of where you’re starting from:
C:Userschris>
When you see commands in this book, you see them displayed next to the prompt. You don’t need to type the prompt part, just the command line itself.
Change folders by typing cd DevKit and pressing the Enter key to tell the computer you’re done with that command.
You see the prompt change to your new location:
C:Userschris > cd DevKit
C:DevKit>
Use Ruby to set up more Ruby tools.
Development Kit has a Ruby setup program called dk.rb that you use for a couple of different steps. Enter the first command and watch for it to complete, as shown in Figure 1-7.
C:DevKit> ruby dk.rb init
If you see the Initialization complete! message, Development Kit is ready to finish installing its tools.
C:DevKit> ruby dk.rb install
Phew! You only have to do these commands once to get your computer ready for Ruby programming and you’re almost there!
The Development Kit is ready and now you can install Ruby gems (little add-on enhancements for Ruby) that you use for the more advanced projects in this book.
You learn a lot more about Gosu, the graphics and game programming library in later chapters. Enter the command and review the progress messages (see Figure 1-9):
C:DevKit> gem install gosu
You may get a Windows security warning saying that Ruby is trying to use the network. This is okay — you can select the default values in the dialog box. You may have to enter your password to dismiss the dialog box.
Wow, that was a lot, but now you have Ruby installed!
Now you will want a code editor to help make writing your programs easy:
In your browser, go to www.atom.io
and click the Download Windows Installer button (see Figure 1-10).
Atom is a free, powerful code editor that can be used for programming many different computer languages. It works well with Ruby.
An installer program downloads to your computer.
Run the Atom installer by choosing Run Program (if Windows presents this option) or double-clicking the installer program when it finishes downloading.
You see a progress dialog box. When the installer is done, the Atom editor starts up (see Figure 1-11).
You see the Atom welcome screen, which means you’re all set to start programming with Ruby!
To run Ruby on Mac OS X, you have to install Ruby and several developer tools. The following instructions have been tested on Mac OS X Yosemite (10.10.4). They should work fine with Mac OS X El Capitan (10.11.1) or Mavericks (10.9.5) as long as you’re running the latest updates for that version of Mac OS X:
Open the terminal application.
You see a prompt indicator that looks like a dollar sign ($) (see Figure 1-14). In the terminal application, you type commands and press the Return key to get things done. Typing commands is a low-level way of working with a computer. Before the mouse and graphical interface was invented, this was the only way to tell the computer what to do!
Note that your default prompt will include information like the name of your computer, the directory you’re in, and even who you’re logged in as:
Christophers-MacBook-Pro:~ chaupt$
In this book, I omit the full prompt and just show the $ to save space.
$ ruby --version
ruby 2.0.0p481 (2014-05-08 revision 45883) [universal.x86_64-darwin14]
In this example, the version of Ruby is 2.0.0, and the number following the letter p is the current patch or build number. On my machine, the patch level is 481. Although newer versions of Ruby are available, and the Mac’s version will change if you install updates, the current version should work great for the projects in this book.
$ xcode-select --install
After you press Return, the program will open a window to confirm you want to install the tools (see Figure 1-15).
Click the Install button, agree to the license, and wait for the tools to get set up.
This step will take a few minutes depending on the speed of your Internet connection.
Next, you install a set of software installation tools called Homebrew. Homebrew makes it easy to install and update additional software, called packages. Some of the projects in this book use Ruby gems that rely on low-level software to get their jobs done. Homebrew makes it much easier to get everything working. In your browser, go to the Homebrew web site, www.brew.sh
(see Figure 1-16).
You see instructions for installing Homebrew. The page shows a really long command that starts with ruby -e. You can copy it from the web page and paste it into your terminal window at the prompt.
This is one long line, so if something goes wrong, make sure that you got the whole thing.
The command uses Ruby to install the Homebrew tool. It displays a warning about using sudo and then asks you for your password to finish the installation. This warning may be unfamiliar, but it’s safe to proceed with the installation. Many progress messages will fly by on your screen (see Figure 1-17). If you get a message saying that you haven’t agreed to the Xcode license, follow the instructions that are displayed and continue on here.
If you’re just learning about command-line programs on Mac OS X, you may be unfamiliar with using sudo. sudo is a way to give temporary permission to a program to use Administrator privileges. Homebrew needs this permission to set up the directories and software needed to do its job. The Homebrew installation program is used by thousands of people and is very careful about where it puts its software. If you ever need to use sudo yourself, you want to be extra careful that you type the associated commands correctly.
$ brew doctor
If everything is set up properly, you should see a message saying Your system is ready to brew. Otherwise, you may have some instructions to follow to finish updating Homebrew.
$ brew install sdl2 libogg libvorbis
After you press Return, you see a series of progress reports as Homebrew installs the software (see Figure 1-18).
$ sudo gem install gosu
Ruby installs the gem and should provide a confirmation message (as shown in Figure 1-19).
You need to use sudo here because when you install a Ruby gem, you install it for the entire system to use. Remember to take care when entering the command — you’re giving it special permissions using the sudo program.
You have Ruby and its associated developer software ready to go, so now you need a programming editor:
www.atom.io
.Click the Download for Mac button (see Figure 1-20).
Atom is a free, powerful code editor that can be used for programming many different computer languages. It works well with Ruby.
Depending on the settings of your browser, the Atom download archive may automatically uncompress or a zip file may be placed in your Downloads folder.
Drag the Atom icon to your Applications folder.
If you see a zip file rather than the Atom icon, double-click the zip file to uncompress it manually.
Double-click the Atom icon in the Applications folder and check out the editor.
You should see the Atom welcome screen (shown in Figure 1-21). You’re all set to start programming with Ruby!
3.142.119.114