130 likes | 507 Views
Lecture 2 Game Programming Configuring your Game Development Workstation. Lecture 2 Game Programming Configuring your Game Development Workstation. Selecting an Operating System.
E N D
Lecture 2 Game Programming Configuring your Game Development Workstation Lecture 2 Game Programming Configuring your Game Development Workstation
Selecting an Operating System We will be using standard C/C++ for our computer language and open-source tools such as Allegro. Therefore you are welcome to use any operating system you prefer. The notes of this course will be use examples using Windows XP/Vista, however all projects should be transportable to Linux without modification. The version of Allegro used in the examples makes use of DirectX, but is transparent to the programmer. Allegro implements the same functionality in every environment using the native graphics primitives available. • Microsoft Windows • Linux • Mac OS X • Solaris • BeOS • FreeBSD
Selecting a Compiler/IDE Since we will be using standard C/C++ and Allegro we have selected Dev-C++ developed by Bloodshed available at http://www.bloodshed.net/dev/devcpp.html. http://csclab.murraystate.edu/bob.pilgrim/575/devcpp-4.9.9.2_setup.exe
Selecting a Gaming Software Library We will be using Allegro 4.2 in our game programming examples. Implementations for this version of Allegro are available locally at: http://csclab.murraystate.edu/bob.pilgrim/info/Allegro/ Other Useful Links http://www.talula.demon.co.uk/allegro/ http://www.allegro.cc/ http://www.allegro.cc/manual/ http://en.wikipedia.org/wiki/Allegro_library
Configuring Dev-C++ 1. Run the installer devcpp-4.9.9.2_setup.exe on your game development computer. 2. Copy the mingw-4.2.0.zip file into the C:\Dev-Cpp (assuming you took the default location in Step 1) and unzip it. 3. Copy the files in the Allegro lib directory into the Dev-Cpp/lib directory. 4. Copy the files in the Allegro include directory into the Dev-Cpp/include directory. 5. Copy the DLLs in the Allegro bin directory into Windows/System32 directory. 6. Start Dev-Cpp and create a New Project called Test_Allegro or similar. This should be an empty project. 7. Create and save a source file named main.cpp for this project. Write or copy the sample program on the next slide into main.cpp. 8. Under project options, change the project Type to Win32 GUI. and add the reference -lalleg to the Linker options under the Parameters tab. 9. Run/Debug your first Allegro Dev-Cpp program...
Sample Program Test_Allegro #include <allegro.h> int main(void) { allegro_init(); set_gfx_mode(GFX_SAFE, 640, 480, 0, 0); install_keyboard(); textout_ex(screen,font,"Hello World!",1,1,10,-1); textout_ex(screen, font, "Press ESCape to quit.",1,12,11,-1); while(!key[KEY_ESC]); allegro_exit(); return 0; } END_OF_MAIN()
The First Game Pong was the first video game, so we will make it our first programming exercise. 1. Create an Allegro GUI Project 2. Use solid (filled) rectangles as the paddles. Implement paddle motion for left and right paddles using the 'A' and 'Z' keys for the left paddle and the 'K' and 'M' keys for the right paddle. 3. Create a ball using a filled circle that appears in the center of the screen and initially moves horizontally to the left or the right (alternate). 4. Determine a bounce function for the ball off the paddles that is a function of the velocity of the paddle when contacted by the ball. 5. Include a simple bounce function for the top and bottom of the screen to keep the ball in play. 6. Add keys for start game, and quit game and a legend describing their function. 7. Include a score-board and mechanism for ending the game when one of the players reaches a score of 11. 8. Add sound to your game. 9. Add any features you feel improve your game.
Game Space Layout (SCREEN_W,0) (0,0) maintain 4 to 3 aspect ratio (0,SCREEN_H) (SCREEN_W,SCREEN_H)
#include <allegro.h> #define BLACK makecol(0,0,0) #define GREEN makecol(64,255,64) #define RED makecol(255,64,64) #define YELLOW makecol(255,255,64) : int padLeftX, padRightX; int padLeftY, padRightY; int padLeftYold, padRightYold; int padWidth = 6; int padHeight = 40; int padVy = 4; : padLeftX = 20; padRightX = SCREEN_W - 20; padLeftY = SCREEN_H/2; padRightY = SCREEN_H/2; int ret = set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0); Initializing Game Parameters The parameters SCREEN_W and SCREEN_H are defined by Allegro set_gfx_mode( ). You should use these parameters to place your game objects so that screen resolution can be easily changed.
Handling Player Input install_keyboard(); : if(key[KEY_A]) padLeftY -= padVy; if(key[KEY_Z]) padLeftY += padVy; if(key[KEY_K]) padRightY -= padVy; if(key[KEY_M]) padRightY += padVy; Allegro function install_keyboard( ) initializes keyboard input. In the main game loop you should use a series of if( ) statements to test for key presses rather than a switch( ) construct, since you want to detect if more than one key has been pressed in a single pass through the game loop. The position of a movable object can be controlled by adding or subtracting a fixed velocity, padVy.
Checking Game Space Limits (0,0) ? if(padLeftY<padHeight/2) padLeftY = padHeight/2; if(padLeftY>SCREEN_H - padHeight/2) padLeftY = SCREEN_H - padHeight/2; if(padRightY<padHeight/2) padRightY = padHeight/2; if(padRightY>SCREEN_H - padHeight/2) padRightY = SCREEN_H - padHeight/2; padHeight padLeftY After player inputs have been handled the new positions of the movable objects need to be checked to ensure that they are within the game space limits. What to do to keep the objects inside the game space depends on the object's function. For the paddles, you can just replace the new position with the limiting position as if it has hit a boundary.
game loop init variables quit game ? init allegro new game ? save old positiions init game state ball at top or bottom bounce ball exit allegro ball hit paddle return ball ball miss paddle score & reset update scoreboard players input paddles at top or bottom stop paddle animate end loop Pong Game Block Diagram