Skip to content

Latest commit

 

History

History
123 lines (99 loc) · 2.94 KB

File metadata and controls

123 lines (99 loc) · 2.94 KB

Week 13 – Student Tasks: Moving Dot (Pygame Zero)

FIRST: Installation Check

Before starting, make sure Pygame Zero is installed:

  1. Open week13.py in Thonny
  2. Press F5 to run
  3. You should see a white dot moving and bouncing

If it doesn't work: Ask teacher for help with installation!

Task A: Run and observe

  1. Run the program
  2. Watch the dot move and bounce
  3. Notice: It bounces automatically off left and right edges
  4. Try to count: How many times does it bounce in 10 seconds?

Task B: Make modifications

  1. Change speed

    • Find: vx = 3
    • Try: vx = 5 (faster)
    • Try: vx = 1 (slower)
    • Try: vx = -3 (starts moving left!)
  2. Change dot appearance

    • Find: screen.draw.filled_circle((x, y), 10, "white")
    • Change 10 to 20 (bigger dot)
    • Change "white" to "red" or "yellow"
  3. Change window size

    • Find: WIDTH = 600 and HEIGHT = 400
    • Try: WIDTH = 800 and HEIGHT = 600 (bigger window)
  4. 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

Task C: Add vertical bouncing

Make the dot bounce up and down too!

  1. Add vertical velocity:

    vx = 3
    vy = 2  # Add this line
  2. 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!

Task D: Creative challenges

  1. 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])
  2. 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")
  3. 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

Success checklist

  • 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)