Python Programming for the Absolute Beginner, 3rd Edition
Moving and rotating sprites adds excitement to a game, but animation really makes a game come to life. Fortunately, the games module contains a class for animations, aptly named Animation.
Introducing the Explosion Program
The Explosion program creates an animation of an explosion in the middle of a graphics screen. The animation plays continuously so that you can get a good look at it. When you're done appreciating the cool effect, you can end the program by closing the graphics window. Figure 12.5 shows a snapshot of the program.
Examining the Explosion Images
An animation is a sequence of images (also called frames) displayed in succession. I created a sequence of nine images that, when displayed in succession, resembles a fiery explosion. Figure 12.6 shows off all nine images.
Setting Up the Program
As always, the initial code imports the games module and defines constants for the graphics screen's dimensions:
# Explosion # Demonstrates creating an animation # Michael Dawson 5/19/03 from livewires import games SCREEN_WIDTH = 640 SCREEN_HEIGHT = 480
In the main part of the program, I create a graphics screen with the following lines:
# main my_screen = games.Screen(SCREEN_WIDTH, SCREEN_HEIGHT) nebula_image = games.load_image("nebula.jpg", transparent = 0) my_screen.set_background(nebula_image)
Creating a List of Image Files
The constructor of the Animation class takes a list of image file names or a list of image objects for the sequence of images to display. So next, I create a list of image file names, which correspond to the images shown in Figure 12.6:
explosion_files = ["explosion1.bmp", "explosion2.bmp", "explosion3.bmp", "explosion4.bmp", "explosion5.bmp", "explosion6.bmp", "explosion7.bmp", "explosion8.bmp", "explosion9.bmp"]
Creating an Animation Object
Finally, I create an Animation object in the following lines:
games.Animation(screen = my_screen, x = SCREEN_WIDTH/2, y = SCREEN_HEIGHT/2, images = explosion_files, n_repeats = 0, repeat_interval = 5)
The Animation class is derived from Sprite, so it inherits all of Sprite's methods and attributes. To create an animation, you must supply a screen and x-and y-coordinates as arguments to define where the object will be located, just as you do for a new sprite. In the previous code, I supply coordinates so that the animation is created at the center of the screen.
An animation requires images, so you must supply a list of image file names or a list of image objects for the images to be displayed. I supply a list of image file names, explosion_files.
Next, I supply the n_repeats parameter with the value 0. n_repeats represents how many times the animation (as a sequence of all of its images) is displayed. A value of 0 means that the animation will loop forever. The default value of n_repeats is 0.
Then, I pass to the repeat_interval parameter the value 5. repeat_interval represents the delay between successive animation images. A higher number means a longer delay between frames, resulting in a slower animation. A lower number represents a shorter delay, producing a faster animation.
Finally, I kick off the program by invoking my_screen's mainloop() method:
my_screen.mainloop()
Категории