-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweek11.py
More file actions
135 lines (107 loc) · 3.43 KB
/
week11.py
File metadata and controls
135 lines (107 loc) · 3.43 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
124
125
126
127
128
129
130
131
132
133
134
135
# Week 11: Timed Coin Collector (Timer + Random)
# Goal: Add time pressure with countdown timer and win/lose conditions
# This creates urgency and makes the game more exciting!
import turtle
import random
import time # New module for tracking time
# Setup screen
screen = turtle.Screen()
screen.title("Timed Coin Collector")
screen.setup(width=700, height=500)
# Create hero turtle
hero = turtle.Turtle()
hero.shape("turtle")
hero.penup()
hero.speed(0)
# Create coin turtle
coin = turtle.Turtle()
coin.shape("circle")
coin.color("gold")
coin.penup()
coin.speed(0)
# Create display turtle (for score AND timer)
writer = turtle.Turtle()
writer.hideturtle()
writer.penup()
writer.goto(-330, 210)
# Game variables
score = 0
STEP = 20
GAME_SECONDS = 20 # Total time to play
# Record when game starts
start_time = time.time() # time.time() returns current time in seconds
# ===== Helper Functions =====
def place_coin():
"""Move coin to random position"""
coin.goto(random.randint(-320, 320), random.randint(-200, 200))
def draw_ui():
"""Update score and timer display"""
# Calculate how much time has passed
elapsed = time.time() - start_time
# Calculate remaining time (don't go below 0)
remaining = max(0, int(GAME_SECONDS - elapsed))
# Update display
writer.clear()
writer.write(f"Score: {score} Time: {remaining}",
font=("Arial", 16, "normal"))
def game_over():
"""Display game over message"""
writer.goto(-80, 0) # Move to center
writer.write("GAME OVER", font=("Arial", 24, "bold"))
def check_time():
"""Check if time has run out"""
if time.time() - start_time >= GAME_SECONDS:
return True # Time's up!
return False # Still time left
def check_collect():
"""Check if hero collected coin"""
global score
# Check if game is over first
if check_time():
game_over()
return # Stop checking collection
# Check if hero is close to coin
if hero.distance(coin) < 25:
score += 1
place_coin()
# Update display (both score and timer)
draw_ui()
# ===== Movement Functions =====
# Helper function to reduce code duplication
def move(dx, dy):
"""
Move hero by dx, dy and check collection
Args:
dx: Change in X coordinate
dy: Change in Y coordinate
"""
# Check if game is over
if check_time():
game_over()
return # Don't allow movement if time's up
# Move hero
hero.goto(hero.xcor() + dx, hero.ycor() + dy)
# Check if collected coin
check_collect()
# Define arrow key functions using move() helper
def up(): move(0, STEP) # Move up: no X change, +Y change
def down(): move(0, -STEP) # Move down: no X change, -Y change
def left(): move(-STEP, 0) # Move left: -X change, no Y change
def right(): move(STEP, 0) # Move right: +X change, no Y change
# ===== Initialize Game =====
place_coin() # Put first coin on screen
draw_ui() # Show initial score and timer
# ===== Connect Keyboard Events =====
screen.listen()
screen.onkey(up, "Up")
screen.onkey(down, "Down")
screen.onkey(left, "Left")
screen.onkey(right, "Right")
# Keep window open
screen.mainloop()
# ===== Key Concepts =====
# 1. time.time() gives current time in seconds
# 2. Subtract start_time to get elapsed time
# 3. Check time before allowing actions
# 4. max(0, value) prevents negative numbers
# 5. Helper functions reduce code duplication