-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweek13.py
More file actions
61 lines (49 loc) · 1.9 KB
/
week13.py
File metadata and controls
61 lines (49 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# Week 13: Pygame Zero - Moving Dot (No images needed)
# Goal: Learn Pygame Zero basics - automatic game loop, update() and draw()
# Run with: pgzrun week13.py (or just F5 in Thonny if pgzero installed)
# === CONFIGURATION ===
# These MUST be at the top, outside functions
# Pygame Zero uses these to create the window
WIDTH = 600
HEIGHT = 400
# === GAME VARIABLES ===
# Position of the dot
x = WIDTH // 2 # Start at horizontal center (// = integer division)
y = HEIGHT // 2 # Start at vertical center
# Velocity (speed and direction)
vx = 3 # Positive = move right, negative = move left
# === UPDATE FUNCTION ===
# Called automatically 60 times per second!
# This is where game logic happens (movement, collisions, etc.)
def update():
global x, vx # Need 'global' to modify variables from outside function
# Move the dot
x += vx # Same as: x = x + vx
# Bounce off edges
if x < 10 or x > WIDTH - 10:
vx = -vx # Reverse direction (flip the sign)
# === DRAW FUNCTION ===
# Called automatically after each update()
# This is where we display everything
def draw():
screen.clear() # IMPORTANT: Erase previous frame
# Draw background text
screen.draw.text("Moving Dot", (20, 20))
# Draw the dot (filled circle)
# Parameters: (x, y), radius, color
screen.draw.filled_circle((x, y), 10, "white")
# === NO MAINLOOP NEEDED! ===
# Pygame Zero runs the game loop automatically
# It calls update() then draw(), 60 times per second
# This is MUCH simpler than turtle.ontimer()
# === KEY CONCEPTS ===
# 1. WIDTH/HEIGHT create window (must be CAPITALS)
# 2. update() = change game state
# 3. draw() = show game state
# 4. screen is automatic (no import needed!)
# 5. 60 FPS = 60 frames (updates) per second
# === TRY THIS ===
# 1. Change vx to 5 (faster)
# 2. Remove screen.clear() - see trails!
# 3. Add vy for vertical movement
# 4. Change color to "red" or "yellow"