1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
5
>Revision: Pygame fundamentals</TITLE
8
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
10
HREF="MakeGames.html"><LINK
12
HREF="MakeGames.html"><LINK
14
TITLE="Kicking things off"
15
HREF="games3.html"> <style type="text/stylesheet">
17
PRE.PROGRAMLISTING { background-color: #EEEEEE; border-color: #333333; border-style: solid; border-width: thin } -->
31
SUMMARY="Header navigation table"
78
>2. Revision: Pygame fundamentals</H1
86
>2.1. The basic Pygame game</H2
88
>For the sake of revision, and to ensure that you are familiar with the basic structure of a Pygame program, I'll briefly run through
89
a basic Pygame program, which will display no more than a window with some text in it, that should, by the end, look something like
90
this (though of course the window decoration will probably be different on your system):</P
98
>The full code for this example looks like this:</P
100
CLASS="PROGRAMLISTING"
104
from pygame.locals import *
109
screen = pygame.display.set_mode((150, 50))
110
pygame.display.set_caption('Basic Pygame program')
113
background = pygame.Surface(screen.get_size())
114
background = background.convert()
115
background.fill((250, 250, 250))
118
font = pygame.font.Font(None, 36)
119
text = font.render("Hello There", 1, (10, 10, 10))
120
textpos = text.get_rect()
121
textpos.centerx = background.get_rect().centerx
122
background.blit(text, textpos)
124
# Blit everything to the screen
125
screen.blit(background, (0, 0))
126
pygame.display.flip()
130
for event in pygame.event.get():
131
if event.type == QUIT:
134
screen.blit(background, (0, 0))
135
pygame.display.flip()
138
if __name__ == '__main__': main()</PRE
147
>2.2. Basic Pygame objects</H2
149
>As you can see, the code consists of three main objects: the screen, the background, and the text. Each of these objects is created
150
by first calling an instance of an in-built Pygame object, and then modifying it to fit our needs. The screen is a slightly special
151
case, because we still modify the display through Pygame calls, rather than calling methods belonging to the screen object. But for
152
all other Pygame objects, we first create the object as a copy of a Pygame object, giving it some attributes, and build our game
153
objects from them.</P
155
>With the background, we first create a Pygame Surface object, and make it the size of the screen. We then perform the convert()
156
operation to convert the Surface to a single pixel format. This is more obviously necessary when we have several images and surfaces,
157
all of different pixel formats, which makes rendering them quite slow. By converting all the surfaces, we can drastically speed up
158
rendering times. Finally, we fill the background surface with white (255, 255, 255). These values are <I
162
Blue), and can be worked out from any good paint program.</P
164
>With the text, we require more than one object. First, we create a font object, which defines which font to use, and the size of the
165
font. Then we create a text object, by using the <TT
168
> method that belongs to our font object, supplying three arguments:
169
the text to be rendered, whether or not it should be anti-aliased (1=yes, 0=no), and the color of the text (again in RGB format). Next
170
we create a third text object, which gets the rectangle for the text. The easiest way to understand this is to imagine drawing a
171
rectangle that will surround all of the text; you can then use this rectangle to get/set the position of the text on the screen. So
172
in this example we get the rectangle, set its <TT
175
> attribute to be the <TT
179
background (so the text's center will be the same as the background's center, i.e. the text will be centered on the screen on the x
180
axis). We could also set the y coordinate, but it's not any different so I left the text at the top of the screen. As the screen is
181
small anyway, it didn't seem necessary.</P
192
>Now we have created our game objects, we need to actually render them. If we didn't and we ran the program, we'd just see a
193
blank window, and the objects would remain invisible. The term used for rendering objects is <I
197
you copy the pixels belonging to said object onto the destination object. So to render the background object, you blit it onto the
198
screen. In this example, to make things simple, we blit the text onto the background (so the background will now have a copy of the
199
text on it), and then blit the background onto the screen.</P
201
>Blitting is one of the slowest operations in any game, so you need to be careful not to blit too much onto the screen in every frame.
202
If you have a background image, and a ball flying around the screen, then you could blit the background and then the ball in every
203
frame, which would cover up the ball's previous position and render the new ball, but this would be pretty slow. A better solution is
204
to blit the background onto the area that the ball previously occupied, which can be found by the ball's previous rectangle, and then
205
blitting the ball, so that you are only blitting two small areas.</P
214
>2.4. The event loop</H2
216
>Once you've set the game up, you need to put it into a loop so that it will continuously run until the user signals that he/she wants
217
to exit. So you start an open <TT
220
> loop, and then for each iteration of the loop, which will be each frame of the game,
221
update the game. The first thing is to check for any Pygame events, which will be the user hitting the keyboard, clicking a mouse
222
button, moving a joystick, resizing the window, or trying to close it. In this case, we simply want to watch out for for user trying
223
to quit the game by closing the window, in which case the game should <TT
226
>, which will end the <TT
230
Then we simply need to re-blit the background, and flip (update) the display to have everything drawn. OK, as nothing moves or happens
231
in this example, we don't strictly speaking need to re-blit the background in every iteration, but I put it in because when things are
232
moving around on the screen, you will need to do all your blitting here.</P
243
>And that's it - your most basic Pygame game! All games will take a form similar to this, but with lots more code for the actual game
244
functions themselves, which are more to do your with programming, and less guided in structure by the workings of Pygame. This is what
245
this tutorial is really about, and will now go onto.</P
253
SUMMARY="Footer navigation table"
264
HREF="MakeGames.html"
273
HREF="MakeGames.html"
302
>Kicking things off</TD