Before starting, make sure Pygame Zero is installed:
- Open week13.py in Thonny
- Press F5 to run
- You should see a white dot moving and bouncing
If it doesn't work: Ask teacher for help with installation!
- Run the program
- Watch the dot move and bounce
- Notice: It bounces automatically off left and right edges
- Try to count: How many times does it bounce in 10 seconds?
-
Change speed
- Find:
vx = 3 - Try:
vx = 5(faster) - Try:
vx = 1(slower) - Try:
vx = -3(starts moving left!)
- Find:
-
Change dot appearance
- Find:
screen.draw.filled_circle((x, y), 10, "white") - Change
10to20(bigger dot) - Change
"white"to"red"or"yellow"
- Find:
-
Change window size
- Find:
WIDTH = 600andHEIGHT = 400 - Try:
WIDTH = 800andHEIGHT = 600(bigger window)
- Find:
-
See the trail effect
- Find:
screen.clear() - Comment it out:
# screen.clear() - Run again - the dot leaves a trail!
- Uncomment it to go back to normal
- Find:
Make the dot bounce up and down too!
-
Add vertical velocity:
vx = 3 vy = 2 # Add this line
-
Update y position in update():
def update(): global x, vx, y, vy # Add y and vy x += vx y += vy # Add this line if x < 10 or x > WIDTH - 10: vx = -vx if y < 10 or y > HEIGHT - 10: # Add vertical bounce vy = -vy
Now it bounces in all directions!
-
Color-changing dot
# At top, add: colors = ["red", "blue", "green", "yellow", "purple"] color_index = 0 # In update(), when bouncing: if x < 10 or x > WIDTH - 10: vx = -vx color_index = (color_index + 1) % len(colors) # In draw(): screen.draw.filled_circle((x, y), 10, colors[color_index])
-
Two dots
# Variables x1 = 100 vx1 = 3 x2 = 500 vx2 = -3 # Update both def update(): global x1, vx1, x2, vx2 x1 += vx1 x2 += vx2 if x1 < 10 or x1 > WIDTH - 10: vx1 = -vx1 if x2 < 10 or x2 > WIDTH - 10: vx2 = -vx2 # Draw both def draw(): screen.clear() screen.draw.filled_circle((x1, HEIGHT//2), 10, "white") screen.draw.filled_circle((x2, HEIGHT//2), 10, "red")
-
Accelerating dot
# Make it speed up over time def update(): global x, vx x += vx if x < 10 or x > WIDTH - 10: vx = -vx * 1.1 # Increase speed by 10% each bounce
- I can run Pygame Zero programs
- I understand update() changes things
- I understand draw() shows things
- I can modify speed and colors
- I added vertical bouncing (Task C)