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

(vip2019) #1
User Interaction: Get into the Game 209

Figure 9-1: The title bar at the top of ClickDots.py tells the user,
“Click to draw.”

The rest of our setup creates our game loop variable, keep_going;
sets a color constant (we’ll draw in red for this program); and creates
a radius for our drawing dots:

keep_going = True
RED = (255,0,0) # RGB color triplet for RED
radius = 15

Now let’s move on to our game loop.

Game Loop: handling mouse clicks
In our game loop, we need to tell the program when to quit and
how to handle mouse-button presses:

while keep_going: # Game loop
for event in pygame.event.get(): # Handling events
u if event.type == pygame.QUIT:
keep_going = False
v if event.type == pygame.MOUSEBUTTONDOWN:
w spot = event.pos
x pygame.draw.circle(screen, RED, spot, radius)

Free download pdf