User Interaction: Get into the Game 229
#2: Painting in Colors
Let the user draw in two or more consistent colors using any
of the following options:
• Change the current drawing color each time the user
presses a key, either to a random color each time or to a
specific color for certain keys (like red for R, blue for B,
and so on).
• Draw with different colors for each of the mouse buttons
(red for the left mouse button, green for the middle but-
ton, and blue for the right mouse button, for example).
• Add some colored rectangles to the bottom or side of the
screen, and modify the program so that if the user clicks
in a rectangle, the drawing color changes to the same
color as the rectangle.
Try one approach, or all three, and save your new file as
ColorPaint.py.
#3: Throwing Smileys
Pygame has a function called pygame.mouse.get_rel() that
will return the amount of relative motion, or how much
the mouse’s position has changed in pixels since the last
call to get_rel(), in the x- and y-directions. Modify your
SmileyExplosion.py file to use the amount of relative mouse
motion in the x- and y-directions as the horizontal and ver-
tical speeds of each smiley (instead of generating a pair of
random speedx and speedy values). This will look like the user
is throwing smileys because they will speed off in the direc-
tion the user is dragging the mouse!
To add another realistic effect, slow the smileys slightly
by multiplying xvel and yvel by a number smaller than 1.0
(like 0.95) in the update(self) section every time the smileys
bounce off an edge of the screen. The smileys will slow
down over time, as if friction from each wall bounce is
making them move slower and slower. Save your new app
as SmileyThrow.py.