Python PyGame: 1

Introduction:

A particular nephew of mine asked if I could help him write a computer game. I said of course, but I really had no idea what this meant. So, I did some quick research on Amazon and came up with a couple of books to run through on the subject. The next few blogs will be working through “Game Programming The Express Line to Learning”. It was written in 2006 fro Python (which you wouldn’t know by the name). Why Python? Because I’m teaching myself Python and I enjoyed my experiences with Django and Python on the more familiar web development side of things. Okay, enough with the chatter, onto some meat.

Assumptions:
This is written assuming you have Python 2.7 installed on your Windows 64bit machine. If not, modify accordingly.
You’re at least a level 1 programmer with a +1 ring of protection… (j/k).

Editors: I have switched to notepad++ for my Python work because my free PyCharm ran out. A major bummer because PyCharm is super awesome. Luckily, notepad++ is pretty good to when you add in the right extensions.

Download the appropriate installer.
If you go to the obvious place [http://pygame.org/download.shtml], you’ll notice there are no Windows 64bit installations.

If you try to install a 32 bit version, then on the next step you’ll get this error:

 
You have to go here for those: http://www.lfd.uci.edu/~gohlke/pythonlibs/#pygame

Run the installer. Select the Entire Features to be installed and change the directory to your Python27 location.

Now open up IDLE and type: import pygame
It should think a bit and then come back to a >>> line.
If it does not you’re probably missing libraries, so go find them and download them.

So, now it’s time to write your first game.
Go ahead and make a folder inside your Python27 folder called games and then put another one in there called idea:

The core idea this book sets us up with surprisingly takes the form of ingenious acronyms! Acronyms are largely responsible for our higher pay and social eschewing. Regardless, they are a constant.
Here’s the SETUP:
IDEA and ALTER
Where A=ALTER
Thus, it’s really: IDE[ALTER]
What does it mean in pseudo-code?
IDEA (the game framework) =
1.Import & Initialize
2.Display Configuration
3.Entities
4.Action (broken into the ALTER steps)
ALTER
1.Assign values to key variables
2.Loop (infinite w/breakout clause)
3.Timer (setup framerate)
4.Event Handling
5.Refresh Display

 
Okay,now that we have a total understanding of what we’ll be diong from the beginning game to the end game, let’s jump right into the intro program that does nothing but display a blue screen:

Create a file called idea.py inside the games/idea folder.
Slap this code in there:

Let’s go over some of the less obvious things. We’re using an infinite loop. True. This appears to be acceptable in the gaming world, whereas I’m used to this being an unforgivable sin in the web world. Pygame is doing most of the heavy lifting. You don’t really need to know that pygame is a wrapper around SDL (Simple DirectMedia Layer: http://www.libsdl.org/ ), but, take it for granted that someone (Pete Shinners) did a lot of heavy lifting for you to make writing games fun and easy. Thanks Pete!
The clock.tick(30) is setting your FPS (FRAMES PER SECOND).
BLIT = Copies data from one rectangle to another (moves objects)
FLIP = displaying graphics on the screen the easiest way will be slow and create a flicker effect. Thus we use the “screen flipping” technique that implements “double buffering” routines (most new systems support this now) that is a special kind of blit which copies the screen object onto the visual display. This reduces screen refresh rates and improves the flicker effect.
And Blue = RGB value 0,0,255.

Open a command prompt and run this file:

You’ll see this:

It’s fairly amazing that something this awesome could be so easy… okay, so it’s not fairly amazing, but it is the foundation for everything from here on out.

Okay, so now lets move a box across a screen so we can see this screen refresh stuff in actions.
Create a new game file called movebox.py and slap this code in there (I made a new folder under the same name):

 

Not much has changed, but we have added another entity. Now we have a background and a box. Then in the Events we have told the box to move positions to the right on the X axis. We’ve also added in a “collision” detection with the edge so it will start over. Nothing mind blowing. Go run it.

 

You’ll see an animated version of this:
 

Okay, that’s a bit cooler than a blue background. Okay, that took us through to chapter 5. So, next blog we’ll get into some more interesting elements around drawing and events. Thanks!

Resources:
http://www.amazon.com/Game-Programming-Line-Express-Learning/dp/0470068221
http://pygame.org
http://www.lfd.uci.edu/~gohlke/pythonlibs/#pygame
http://pygame.org/docs/tut/newbieguide.html
http://notepad-plus-plus.org/
 

© Copyright Duke Hall - Designed by Pexeto