Start your game development portfolio.
Python and its free game development library, Pygame, handle a lot of the detail work of game development for you, allowing you to focus on your game design. Still, Crysis wasn’t written in a day, so you’ll need to start out where video games themselves started out by making a game like “Pong.” It might not be what you are getting into game design to do, but it will teach you the basics and let you get a game up and running quickly.
1. Open Idle, the default Python IDE from your Windows Start menu. Click “File” and “New Window.” Immediately save your new file and give it the name “Ping.py.”
2. Get the game environment ready. Type the following into your Ping.py file:
import os, sys
from pygame.locals import *
screen = pygame.display.set_mode((640,480))
This imports from Python extra commands you’ll need to write your game and creates a new window for your game to run in. Hit “F5” to run the program. Alternatively, click “Run” and “Run Module” from the menu. Both will have the same result–a completely transparent window will appear on your desktop. It will be 640 pixels wide and 480 pixels high.
And it will crash and give you an error message pretty quickly because even though the window was created, nothing was done with it.
3. Create the player’s paddle. Add the following code to your game immediately beneath the last section of code:
PADDLE_WIDTH = 50
PADDLE_HEIGHT = 10
p1Paddle = pygame.Rect(10, 430, PADDLE_WIDTH, PADDLE_HEIGHT)
PADDLE_COLOR = pygame.color.Color(“red”)
SCREEN_COLOR = pygame.color.Color(“black”);
This tells the computer how many pixels wide and high the player’s paddle should be as well as what color it should be. It also tells Python that you want your paddle to be 10 pixels from the left side of the screen and 430 pixels from the top of the screen. Remember, the screen is 640 pixels wide and 480 pixels high, so the paddle will be on the left side of the screen, near the bottom.
Also, it defines that the screen itself will have the color black.
4. Create the puck. Type the following into your game:
puckSpeedX = 1
puckSpeedY = 1
PUCK_COLOR = pygame.color.Color(“green”)
puck = pygame.Rect(320,240,10,10)
This tells the computer what color the puck will be and where it should be located on the screen. In this case, it will be 320 pixels from the left and 240 pixels from the top. It will also be 10 pixels wide and 10 pixels high.
Finally, since the puck moves of its own will, it needs a speed. Often in a 2D game such as “Pong,” it is easier to divide speeds into how vast an object moves horizontally and how fast it moves vertically. In this case, the puck will move horizontally (X) at a speed of 1 and will move vertically (Y) at a speed of 1 also.
5. Set up the game loop by typing:
p1Paddle.left = pygame.mouse.get_pos()
puck.left = puck.left + puckSpeedX
puck.top = puck.top + puckSpeedY
Going line by line, the “while true” command tells the game to keep running indefinitely or until the user hits the “Close” button on his window.
Next, it tells the computer that the player’s paddle should be in the same place on the screen, horizontally, than the mouse pointer. That way, the player can follow the puck by moving his mouse. The puck itself is moved by adding its X and Y speeds to its position on the screen. Then it draws the screen background, the player’s paddle and the puck, in that order. Hit “F5” to run the game.
Notice any problems? None of your graphics being drawn to the screen. Nearly all modern games use a trick called “double buffering” to draw to the screen. This means graphics are drawn to memory first and then drawn, all at once, to the screen later. Without double buffering, game graphics have a habit of flickering. To tell Python to actually write the graphics to the computer screen, or “flip the buffer,” add the following line to the game:
Press “F5” to run the game. There’s still a pretty big flaw in the game: the puck just flies through the paddle and off the screen as if it wasn’t there.
6. Add collision detection to the puck. This might require a little refresher on freshman algebra. Add this code to your game loop:
puckSpeedY = puckSpeedY * -1
if puck.top 480:
puckSpeedY = puckSpeedY * -1
if puck.left 640:
puckSpeedX = puckSpeedX * -1
Going one by one, consider what happens here. If the puck collides with the player’s paddle, then it’s Y speed is multiplied by minus-1. So, if the Y speed (remember, this means vertical speed) was 1, it is now minus-1. So it is moving in the exact opposite direction vertically. The same happens if the puck hits the top or bottom of the screen. And finally, if the puck hits the left or right side of the screen, its X speed (or its horizontal speed) is multiplied by minus-1. Hit “F5” to run your game and see the effect.