Teach Your Kids To Code: A Parent-friendly Guide to Python Programming

(vip2019) #1

212 Chapter 9


and MOUSEBUTTONUP events; in other words, we want to separate
mouse button clicks into presses and releases so that we know
when the mouse is being dragged (with the button down) versus
just being moved with the button up.
One way to accomplish this is with another Boolean flag vari-
able. We can set a Boolean called mousedown to True whenever the
user presses the mouse button and to False whenever the user
releases the mouse button. In our game loop, if the mouse but-
ton is down (in other words, when mousedown is True), we can get
the mouse’s location and draw a circle on the screen. If the pro-
gram is fast enough, the drawing should be smooth like in a
paintbrush app.

s tupe
Make the setup section of your code look like this:

import pygame # Setup
pygame.init()
screen = pygame.display.set_mode([800,600])
u pygame.display.set_caption("Click and drag to draw")
keep_going = True
v YELLOW = (255,255,0) # RGB color triplet for YELLOW
radius = 15
w mousedown = False

The setup portion of our app looks like ClickDots.py, except for
the different window caption u, the YELLOW color we’ll be drawing
with v, and the very last line w. The Boolean variable mousedown
will be our flag variable to signal to the program that the mouse
button is down, or pressed.
Free download pdf