forked from realpython/materials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathturtle_invaders.py
More file actions
186 lines (154 loc) · 4.41 KB
/
Copy pathturtle_invaders.py
File metadata and controls
186 lines (154 loc) · 4.41 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import random
import time
import turtle
FRAME_RATE = 30 # Frames per second
TIME_FOR_1_FRAME = 1 / FRAME_RATE # Seconds
CANNON_STEP = 10
LASER_LENGTH = 20
LASER_SPEED = 20
ALIEN_SPAWN_INTERVAL = 1.2 # Seconds
ALIEN_SPEED = 3.5
window = turtle.Screen()
window.tracer(0)
window.setup(0.5, 0.75)
window.bgcolor(0.2, 0.2, 0.2)
window.title("The Real Python Space Invaders")
LEFT = -window.window_width() / 2
RIGHT = window.window_width() / 2
TOP = window.window_height() / 2
BOTTOM = -window.window_height() / 2
FLOOR_LEVEL = 0.9 * BOTTOM
GUTTER = 0.025 * window.window_width()
# Create laser cannon
cannon = turtle.Turtle()
cannon.penup()
cannon.color(1, 1, 1)
cannon.shape("square")
cannon.setposition(0, FLOOR_LEVEL)
cannon.cannon_movement = 0 # -1, 0 or 1 for left, stationary, right
# Create turtle for writing text
text = turtle.Turtle()
text.penup()
text.hideturtle()
text.setposition(LEFT * 0.8, TOP * 0.8)
text.color(1, 1, 1)
lasers = []
aliens = []
def draw_cannon():
cannon.clear()
cannon.turtlesize(1, 4) # Base
cannon.stamp()
cannon.sety(FLOOR_LEVEL + 10)
cannon.turtlesize(1, 1.5) # Next tier
cannon.stamp()
cannon.sety(FLOOR_LEVEL + 20)
cannon.turtlesize(0.8, 0.3) # Tip of cannon
cannon.stamp()
cannon.sety(FLOOR_LEVEL)
def move_left():
cannon.cannon_movement = -1
def move_right():
cannon.cannon_movement = 1
def stop_cannon_movement():
cannon.cannon_movement = 0
def create_laser():
laser = turtle.Turtle()
laser.penup()
laser.color(1, 0, 0)
laser.hideturtle()
laser.setposition(cannon.xcor(), cannon.ycor())
laser.setheading(90)
# Move laser to just above cannon tip
laser.forward(20)
# Prepare to draw the laser
laser.pendown()
laser.pensize(5)
lasers.append(laser)
def move_laser(laser):
laser.clear()
laser.forward(LASER_SPEED)
# Draw the laser
laser.forward(LASER_LENGTH)
laser.forward(-LASER_LENGTH)
def create_alien():
alien = turtle.Turtle()
alien.penup()
alien.turtlesize(1.5)
alien.setposition(
random.randint(
int(LEFT + GUTTER),
int(RIGHT - GUTTER),
),
TOP,
)
alien.shape("turtle")
alien.setheading(-90)
alien.color(random.random(), random.random(), random.random())
aliens.append(alien)
def remove_sprite(sprite, sprite_list):
sprite.clear()
sprite.hideturtle()
window.update()
sprite_list.remove(sprite)
turtle.turtles().remove(sprite)
# Key bindings
window.onkeypress(move_left, "Left")
window.onkeypress(move_right, "Right")
window.onkeyrelease(stop_cannon_movement, "Left")
window.onkeyrelease(stop_cannon_movement, "Right")
window.onkeypress(create_laser, "space")
window.onkeypress(turtle.bye, "q")
window.listen()
draw_cannon()
# Game loop
alien_timer = 0
game_timer = time.time()
score = 0
game_running = True
while game_running:
timer_this_frame = time.time()
time_elapsed = time.time() - game_timer
text.clear()
text.write(
f"Time: {time_elapsed:5.1f}s\nScore: {score:5}",
font=("Courier", 20, "bold"),
)
# Move cannon
new_x = cannon.xcor() + CANNON_STEP * cannon.cannon_movement
if LEFT + GUTTER <= new_x <= RIGHT - GUTTER:
cannon.setx(new_x)
draw_cannon()
# Move all lasers
for laser in lasers.copy():
move_laser(laser)
# Remove laser if it goes off screen
if laser.ycor() > TOP:
remove_sprite(laser, lasers)
break
# Check for collision with aliens
for alien in aliens.copy():
if laser.distance(alien) < 20:
remove_sprite(laser, lasers)
remove_sprite(alien, aliens)
score += 1
break
# Spawn new aliens when time interval elapsed
if time.time() - alien_timer > ALIEN_SPAWN_INTERVAL:
create_alien()
alien_timer = time.time()
# Move all aliens
for alien in aliens:
alien.forward(ALIEN_SPEED)
# Check for game over
if alien.ycor() < FLOOR_LEVEL:
game_running = False
break
time_for_this_frame = time.time() - timer_this_frame
if time_for_this_frame < TIME_FOR_1_FRAME:
time.sleep(TIME_FOR_1_FRAME - time_for_this_frame)
window.update()
splash_text = turtle.Turtle()
splash_text.hideturtle()
splash_text.color(1, 1, 1)
splash_text.write("GAME OVER", font=("Courier", 40, "bold"), align="center")
turtle.done()