Teach Your Kids To Code: A Parent-friendly Guide to Python Programming
Timers and Animation: What Would Disney Do? 195 Then, all we have to do in our game loop is change picx and picy by this new spe ...
196 Chapter 8 But that’s not all! Because our if statement is also checking for the left screen boundary (picx <= 0), when ou ...
Timers and Animation: What Would Disney Do? 197 With this first version of the program, we have created what looks like a smooth ...
198 Chapter 8 We can then modify our image position updates in the game loop: picx += speedx picy += speedy We change picx (the ...
Timers and Animation: What Would Disney Do? 199 Let’s do the same thing with picy: if picy <= 0 or picy + pic.get_height() &g ...
200 Chapter 8 picx += speedx picy += speedy if picx <= 0 or picx + pic.get_width() >= 800: speedx = -speedx if picy <= ...
Timers and Animation: What Would Disney Do? 201 Figure 8-9: If we comment out the line that clears our screen after each frame, ...
202 Chapter 8 that Pygame has a different coordinate system from the Turtle library, with the origin (0, 0) in the upper-left co ...
Timers and Animation: What Would Disney Do? 203 Programming Challenges Here are three challenge problems to extend the skills yo ...
204 Chapter 8 for n in range(100): colors[n] = (random.randint(0,255),random.randint(0,255), random.randint(0,255)) locations[n] ...
Timers and Animation: What Would Disney Do? 205 #3: Raining Dots Finally, let’s take RandomDots.py one step further by pro- gram ...
206 Chapter 8 Figure 8-10: Four frames showing 100 random dots as they move right and down across the screen ...
9 User interaction: Get into the Game In Chapter 8, we used some of the Pygame library’s features to draw shapes and images on t ...
208 Chapter 9 Interactive programs give us this sense of control in an app or game, because we can move or interact with a chara ...
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.” Th ...
210 Chapter 9 At u, we handle the pygame.QUIT event by setting our loop vari- able keep_going to False. The second if statement, ...
User Interaction: Get into the Game 211 if event.type == pygame.MOUSEBUTTONDOWN: spot = event.pos pygame.draw.circle(screen, RED ...
212 Chapter 9 and MOUSEBUTTONUP events; in other words, we want to separate mouse button clicks into presses and releases so tha ...
User Interaction: Get into the Game 213 Next, we’ll add event handlers to our game loop. These event handlers will set mousedown ...
214 Chapter 9 Putting It all together The last step is to end the program with pygame.quit() as usual. Here’s the full program. ...
«
7
8
9
10
11
12
13
14
15
16
»
Free download pdf