-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweek14.py
More file actions
50 lines (41 loc) · 1.4 KB
/
week14.py
File metadata and controls
50 lines (41 loc) · 1.4 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
# Week 14: Explorer (Smooth Keyboard Control in Pygame Zero)
# Goal: Control a player with arrow keys using keyboard state checking
# New concept: keyboard object for smooth movement
WIDTH = 600
HEIGHT = 400
# Player position
x = WIDTH // 2
y = HEIGHT // 2
speed = 4 # Pixels to move per frame
# === UPDATE FUNCTION ===
def update():
global x, y
# Check keyboard state each frame
# This allows smooth diagonal movement!
if keyboard.left:
x -= speed
if keyboard.right:
x += speed
if keyboard.up:
y -= speed
if keyboard.down:
y += speed
# Keep player inside the screen (boundary checking)
# max() ensures value doesn't go below minimum
# min() ensures value doesn't go above maximum
x = max(10, min(WIDTH - 10, x))
y = max(10, min(HEIGHT - 10, y))
# === DRAW FUNCTION ===
def draw():
screen.clear()
screen.fill('darkblue') # Background color
# Instructions
screen.draw.text("Explorer - Use arrow keys", (20, 20), color='white')
# Draw player as white circle
screen.draw.filled_circle((x, y), 10, "white")
# === KEY CONCEPTS ===
# 1. keyboard.left, keyboard.right, keyboard.up, keyboard.down
# These are TRUE when key is held down
# 2. Checking multiple keys in same frame = diagonal movement
# 3. Boundary checking: max(min_val, min(max_val, current))
# 4. This feels smoother than on_key_down() events!