218 Chapter 9
screen = pygame.display.set_mode([800,600])
pygame.display.set_caption("Smiley Explosion")
mousedown = False
keep_going = True
clock = pygame.time.Clock()
pic = pygame.image.load("CrazySmile.bmp")
colorkey = pic.get_at((0,0))
pic.set_colorkey(colorkey)
u sprite_list = pygame.sprite.Group()
The setup looks like SmileyBounce2.py, but we’re adding a
variable called sprite_list at u that will contain our group of
smiley face sprites. Storing the sprites in a Group will make it
faster and easier to do things like draw all the smileys on the
screen every frame, move all the smileys for each step of the ani-
mation, and even check to see if the smiley sprites are colliding
with objects or with one another.
To create sprite objects for complex animations and games,
we will create our own Sprite class that extends, or builds on,
Pygame’s Sprite class, adding the variables and functions that we
want for our custom sprites. We’ll name our sprite class Smiley,
and we’ll add variables for the position of each smiley (pos), its
x- and y-velocity (xvel and yvel; remember velocity is another word
for speed), and its scale, or how big each smiley will be (scale):
class Smiley(pygame.sprite.Sprite):
pos = (0,0)
xvel = 1
yvel = 1
scale = 100
Our Smiley class definition starts with the keyword class,
followed by the name we want for our class, plus the type we’re
extending (pygame.sprite.Sprite).
s tting Up e sprites
The next step after starting our Smiley class and listing the data
variables that we’d like each smiley sprite object to remember is
called initialization, sometimes also referred to as the constructor
for our class. This will be a special function that is called every
time a new object of our Smiley class is created, or constructed, in
our program. Just like initializing a variable gives it a starting
value, the initialization function, __init__(), in our Smiley class will