- What sort of language do you want to program in?
- What sort of platform do you want to deploy your game to?
- What sort of game are you making?
Most of the time you can answer each of these questions independently and find a perfect language/framework that fits your needs. Other times, you are constrained. For example, there aren't many HTML5 frameworks that allow you to write a high-performance 3D game.
For PyGame, I am going to assume you gave the following answers to the previous 3 questions:
- You want to program in Python. Also, you already know Python. Teaching Python from scratch is not covered in this tutorial.
- You want to create a client app that can potentially be wrapped in a standalone executable. You don't care about playing it in a browser or on a mobile device.
- The game you want to create is graphical, but not 3D.
If this sounds like your situation, continue.
Setting Up Python
I recommend Python 2.7 for various reasons I won't go in to here. If you're on windows, just get the Windows Installer. Installation is simple. Just hit Next Next Next Next Finish. Defaults are fine.
Be sure to download the version that corresponds to the version of Python you downloaded just now (and the version that corresponds to your operating system, of course). If you downloaded Python 2.7 like I suggested, you would download the pygame-1.9.2a0.win32-py2.7.msi if you're on Windows. If you're not on Windows, then you probably know what you're doing anyway. Installation of PyGame is equally as simple as Python. Just go with the defaults.
The Anatomy of a PyGame Game
The following is the simplest barebones app that can be made using the PyGame pipeline:
import pygame - this is of course needed to access the PyGame framework.
pygame.init() - This kicks things off. It initializes all the modules required for PyGame.
pygame.display.set_mode((width, height)) - This will launch a window of the desired size. The return value is a Surface object which is the object you will perform graphical operations on. This will be discussed later.
pygame.event.get() - this empties the event queue. If you do not call this, the windows messages will start to pile up and your game will become unresponsive in the opinion of the operating system.
pygame.QUIT - This is the event type that is fired when you click on the close button in the corner of the window.
pygame.display.flip() - PyGame is double-buffered. This swaps the buffers. All you need to know is that this call is required in order for any updates that you make to the game screen to become visible.
When you run this, you'll see:
Which is not amazing at all. Because it's a black box that does nothing.
Drawing Somethingpygame.draw.rect - As you can imagine, this will draw a rectangle. I takes in a few arguments, including the surface to draw on (I'll be drawing on the screen instance), the color, and the coordinates/dimensions of the rectangle.
The first argument is the surface instance to draw the rectangle to.
The second argument is the (red, green, blue) tuple that represents the color to draw with.
The third argument is a pygame.Rect instance. The arguments for this constructor are the x and y coordintaes of the top left corner, the width, and the height.
...but not by much. Right now it may as well just be an image in a window. Next up: interactivity.
InteractivityThe point of a game is to be interactive. Right now, the only thing you can interact with is the close button. Which isn't a very fun game. All user input events come through the event queue. Simply add more if statements to that for loop to add more interactivity.
Add the following code before the loop:
Modify your rectangle code to pick a color conditionally:
Finally, the important bit. Add the following if statement to your for loop in the same sequence as the other if statement in there...
Press space to change the box color.
As you can imagine, there is also a corresponding pygame.KEYUP event type and a pygame.K_%%%%% for almost every key on your keyboard. To see this list, go to a python terminal and use dir(pygame).
There are also mouse event types, but that will be covered later.
Adventuring aroundOur box is bored of switching from aquamarine to orangish red. He wants to move around.
There is an additional way to access key events. You can get the depression status of any key by calling pygame.key.get_pressed(). This returns a huge array filled with 1's and 0's. Mostly 0's.
When you check the integer value of any of the pygame.K_%%%%% constants, you'll notice it's a number.
This value is not-so-coincidentally the index of the get_pressed() array that corresponds to that key. So if you want to see if the Up Arrow is pressed, the way to do that is:
Simple as that.
This is useful for the sort of events that you want to do when the user holds down a button. For example, moving around a sprite when the user holds down any arrow keys.
Applying this concept to our current box game, this is what the code looks like now:
Hmm...that's not what I wanted...
Two things are wrong.
- Each time you draw a rectangle, the rectangle from the previous frames remains on the screen.
- It moves really really really fast.
For the first, you simply need to reset the screen to black before you draw the rectangle. There is a simple method on Surface called fill that does this. It takes in an rgb tuple.
Secondly, the duration of each frame is as short as your super fancy computer can make it. The framerate needs to be throttled at a sane number such as 60 frames per second. Luckily, there is a simple class in pygame.time called Clock that does this for us. It has a method called tick which takes in a desired fps rate.
Put it all together and you get:
You'll have to take my word on the fact that you can move it around.
That concludes this part of the tutorial.
Next up: Images.