Game Development Reference
In-Depth Information
17 - Graphics and Animation
In this program the size of the window's width and height is used for more than just the
call to set_mode() . We will use a constant variables to make the program more
readable. Remember, readability is for the benefit of the programmer, not the computer. If
we ever want to change the size of the window, we only have to change lines 8 and 9.
If we did not use the constant variable, we would have to change ever occurance of the
int value 400 . If any unrelated values in the program were also 400 , we might think it was
for the width or height and also accidentally change it too. This would put a bug in our
program. Since the window width and height never change during the program's execution,
a constant variable is a good idea.
11. pygame.display.set_caption('Animation')
For this program, we will set the caption at the top of the window to 'Animation'
with a call to pygame.display.set_caption() .
13. Setting Up Constant Variables for Direction
14. # set up direction variables
15. DOWNLEFT = 1
16. DOWNRIGHT = 3
17. UPLEFT = 7
18. UPRIGHT = 9
We will use the keys on the number pad of the keyboard to remind us which belongs to
which direction. This will be similar to our Tic Tac Toe game. 1 is down and left, 3 is
down and right, 7 is up and left, and 'Animation' 9 is up and right. However, it may be
hard to remember this, so instead we will use constant variables instead of these integer
values.
We could use any values we wanted to for these directions, as long as we had different
values for each direction. For example, we could use the string 'downleft' to represent
the down and left diagonal direction. However, if we ever mistype the 'downleft' string
(for example, as 'fownleft' ), the computer would not recognize that we meant to type
'downleft' instead of 'downleft' . This bug would cause our program to behave
strangely.
But if we use constant variables, and accidentally type the variable name FOWNLEFT
instead of the name DOWNLEFT , Python would notice that there is no such variable named
FOWNLEFT and crash the program with an error. This would still be a pretty bad bug, but
at least we would know immediately about it and could fix it. Otherwise it may be hard to
notice that there is a bug at all.
19. MOVESPEED = 4
Search WWH ::




Custom Search