Game Development Reference
In-Depth Information
know that we should run any code that we want to happen to stop the program. You
could choose to ignore the QUIT event entirely, but that may cause the program to be
confusing to the user.
The pygame.quit() Function
62. pygame.quit()
63. sys.exit()
If the QUIT event has been generated, then we can know that the user has tried to close
the window. In that case, we should call the exit functions for both Pygame
( pygame.quit() ) and Python ( sys.exit() ).
This has been the simple "Hello world!" program from Pygame. We've covered many
new topics that we didn't have to deal with in our previous games. Even though they are
more complicated, the Pygame programs can also be much more fun and engaging than our
previous text games. Let's learn how to create games with animated graphics that move.
Animation
In this program we have several different blocks bouncing off of the edges of the
window. The blocks are different colors and sizes and move only in diagonal directions. In
order to animate the blocks (that is, make them look like they are moving) we will move
the blocks a few pixels over on each iteration through the game loop. By drawing new
blocks that are located a little bit differently then the blocks before, we can make it look
like the blocks are moving around the screen.
The Animation Program's Source Code
Type the following program into the file editor and save it as animation.py . You can also
download this source code from http://inventwithpython.com/chapter17.
animation.py
This code can be downloaded from http://inventwithpython.com/animation.py
If you get errors after typing this code in, compare it to the topic's code with the online
diff tool at http://inventwithpython.com/diff or email the author at
al@inventwithpython.com
1. import pygame, sys, time
2. from pygame.locals import *
3.
4. # set up pygame
5. pygame.init()
6.
7. # set up the window
8. WINDOWWIDTH = 400
Search WWH ::




Custom Search