To play, you’ll first need to compile the source into an executable. This can be done in only 5-10 minutes following the steps below, and is the first step to your exploring the elements of programming. Unzip/copy these files into a new folder before continuing (you can unzip them anywhere; creating a “Projects” folder on your desktop works).
Use Left and Right keyboard arrows to turn the ship.
The Up key applies forward thrust.
Spacebar “fires” (drops) a green laser.
Press the Escape key to quit.
Are you only seeing a small white line on an otherwise completely black screen? Perfect! That’s all there is to the program in its current form. The rest, as you’ll find outlined in the comments of the code (the core.cpp, main.cpp, and core.h files), is up to you.
Once you have the program compiling – on whichever operating system you prefer – for next steps, read through the comments in the project’s main.cpp file. See if you can figure out how to modify the text to perform some of the remaining tasks. For a conceptual explanation of programming, check out Text Lessons Vol. 1. For a detailed introduction to different programming structures as they relate to videogame programming, check out Text Lessons Vol. 5.
Compiling on PC/Windows
Download and install Dev-C++ 5.0 beta 9.2 (184.108.40.206) (9.0 MB) with Mingw/GCC 3.4.2.
After it’s installed, run Dev-C++, check the “Tools” menu for “Check for Updates/Packages”, then under “Select devpak server” choose “devpaks.org Community Devpaks”. Press the “Check for Updates” button, then check the boxes to install Allegro (the library for audio, input, and graphical functions) and Allegro Supplement (documentation and sample files) – if you’re having a hard time locating those options in the list, click the heading of that names column to alphabetize the listings.
Next, use the File menu to create a New -> Project, type Empty Project, with the “C++ Project” radio button filled. Name it “Asteroids” and save the “Asteroids.dev” project file in the same folder as the downloaded source files.
To add the project’s files to the project, use the Project menu to find the option for “Add to Project”, then select all 3 files that were copied from the downloaded asteroids_ex.zip file (core.cpp, core.h, and main.cpp). Click on any project file in the left hand project view to see the contents of that file – main.cpp is the most interesting, as it’s where the game-specific action takes place.
Before we can compile, we need to add the Allegro library to the project’s setup. Under the “Project” menu, open “Project Options”, then flip to the “Parameters” tab. Under the right-most dialog box, the one headed “Linker”, type in “-lalleg” (minus the quotes). Press Ok.
Lastly, use the menus to go to Execute, then “Compile & Run”. The game (at this point only a white line on a black screen) should open, which you can interact with by using the arrow keys and spacebar. Press escape to quit.
Any time you make changes to the program source, you can update the game itself by repeating that last step to “Compile & Run”. Each time the game is compiled, the “Asteroids.exe” file in the same folder as the source files will be updated. That file is the program which you can share with others.
Compiling on a Mac or Linux
If on a Mac, download Xcode for Mac-only Development from Apple’s website to get all developer’s tools installed – if you’re on Linux, the OS shell probably already is set up for all of the commands that we’ll need. Also download the Allegro source for Linux (I found that here). Double click that downloaded allegro tar.gz to turn it into a folder, then use the Terminal to ‘cd’ command your way into that new allegro-4.2.2 folder (need to learn the Basic Terminal Commands?), then type the following lines into the terminal to install Allegro:
chmod +x fix.sh ./fix.sh macosx make sudo make install sudo make install-man sudo make install-applehelp sudo make install-framework make sudo make install
Followed by using the Terminal to navigate into the folder containing the Asteroids game sample source. Then type in this command:
g++ main.cpp core.cpp -o asteroids-mac `allegro-config --libs`
NOTE that the ` is a BACKTICK on the same key as tilde (~), NOT a single quote/apostrophe from the same key as the parenthesis (“).
To edit the code, open main.cpp using any plain text editor (TextEdit, TextWrangler, DashCode…). To recompile the program to reflect changes made to main.cpp, repeat the step above for typing “g++ main.cpp…[and the rest]” in the Terminal.
(Originally posted as GameDevLessons.com Vol. 9 Sec. 1)
Learn and practice team game development with Gamkedo Club.
Membership worldwide. Professional support. Proven process.
Subscribe by e-mail to receive weekly updates with Gamkedo.Community interviews and YouTube training videos for game developers!