-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweek12.py
More file actions
123 lines (97 loc) · 3.26 KB
/
week12.py
File metadata and controls
123 lines (97 loc) · 3.26 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# Week 12: Turtle Catcher (Capstone Project)
# Goal: Combine all turtle concepts - click targets before time runs out!
# New concepts: Mouse events and continuous game loop with ontimer()
import turtle
import random
import time
# Setup screen
screen = turtle.Screen()
screen.title("Turtle Catcher")
screen.setup(width=700, height=500)
# Create target turtle (what we click on)
target = turtle.Turtle()
target.shape("circle")
target.color("red")
target.penup()
target.speed(0)
# Create display turtle
writer = turtle.Turtle()
writer.hideturtle()
writer.penup()
writer.goto(-330, 210)
# Game variables
score = 0
GAME_SECONDS = 25
start_time = time.time()
# ===== Helper Functions =====
def move_target():
"""Move target to random position"""
target.goto(random.randint(-320, 320), random.randint(-200, 200))
def draw_ui():
"""Update score and timer display"""
# Calculate remaining time
remaining = max(0, int(GAME_SECONDS - (time.time() - start_time)))
# Update display
writer.clear()
writer.write(f"Score: {score} Time: {remaining}",
font=("Arial", 16, "normal"))
def time_up():
"""Check if game time has expired"""
return (time.time() - start_time) >= GAME_SECONDS
# ===== Event Handler for Mouse Clicks =====
def on_click(x, y):
"""
Handle mouse click on target
Args:
x: X coordinate of click
y: Y coordinate of click
"""
global score
# Don't allow clicks after game ends
if time_up():
return
# Check if click is close enough to target
# Using distance between click position (x,y) and target position
if target.distance(x, y) < 25:
score += 1
move_target() # Move to new position
draw_ui() # Update display
# ===== Game Loop =====
# This is different from previous games!
# tick() is called repeatedly to update the display
def tick():
"""
Main game loop - called repeatedly
This creates continuous updates even without user input
"""
if time_up():
# Game over - show message and STOP the loop
writer.goto(-90, 0)
writer.write("TIME UP!", font=("Arial", 24, "bold"))
return # Don't schedule next tick (loop stops)
# Update UI (refreshes timer)
draw_ui()
# Schedule next update
# ontimer(function, milliseconds) calls function after delay
# 250ms = 0.25 seconds = updates 4 times per second
screen.ontimer(tick, 250)
# ===== Initialize Game =====
# Connect click event to target turtle
# When target is clicked, on_click() is called with click coordinates
target.onclick(on_click)
# Place target at random starting position
move_target()
# Start the game loop
tick() # This begins the continuous update cycle
# Keep window open
screen.mainloop()
# ===== Key Concepts =====
# 1. Mouse events: onclick() receives x, y coordinates
# 2. Game loop: tick() → update → schedule next tick → repeat
# 3. ontimer(function, ms) schedules future function call
# 4. Stop loop by NOT scheduling next tick (return early)
# 5. This pattern is used in most real games!
# ===== Difference from Keyboard Games =====
# Keyboard games: Update only when key pressed
# This game: Updates continuously (every 250ms)
# Why? Timer needs to update even when player doesn't click