-
Notifications
You must be signed in to change notification settings - Fork 0
/
Asteroids.py
195 lines (160 loc) · 4.94 KB
/
Asteroids.py
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
187
188
189
190
191
192
193
194
195
import turtle, math, random
wn = turtle.Screen()
wn.bgcolor("black")
wn.title("Asteroids!")
wn.setup(800, 600)
wn.tracer(0)
pen = turtle.Turtle()
pen.penup()
pen.speed(0)
class Sprite():
def __init__(self):
self.x = 0
self.y = 0
self.heading = 0
self.dx = 0
self.dy = 0
self.shape = "square"
self.color = "white"
self.size = 1.0
self.active = True
def update(self):
if self.active:
self.x += self.dx
self.y += self.dy
if self.x > 400:
self.x = -400
elif self.x < -400:
self.x = 400
if self.y > 300:
self.y = -300
elif self.y < -300:
self.y = 300
def render(self, pen):
if self.active:
pen.goto(self.x, self.y)
pen.setheading(self.heading)
pen.shape(self.shape)
pen.shapesize(self.size, self.size, 0)
pen.color(self.color)
pen.stamp()
def is_collision(self, other):
x = self.x-other.x
y = self.y-other.y
distance = ((x**2) + (y**2)) ** 0.5
if distance < ((10 * self.size) + (10 * other.size)):
return True
else:
return False
def goto(self, x, y):
self.x = x
self.y = y
class Player(Sprite):
def __init__(self):
Sprite.__init__(self)
self.shape = "triangle"
self.lives = 3
self.score = 0
def rotate_left(self):
self.heading += 30
def rotate_right(self):
self.heading -= 30
def accelerate(self):
ax = math.cos(math.radians(self.heading))
ay = math.sin(math.radians(self.heading))
self.dx += ax * 0.1
self.dy += ay * 0.1
def decelerate(self):
ax = math.cos(math.radians(self.heading))
ay = math.sin(math.radians(self.heading))
self.dx -= ax * 0.1
self.dy -= ay * 0.1
def render(self, pen):
if self.active:
pen.goto(self.x, self.y)
pen.setheading(self.heading)
pen.shape(self.shape)
pen.shapesize(self.size/2.0, self.size, 0)
pen.color(self.color)
pen.stamp()
class Asteroid(Sprite):
def __init__(self):
Sprite.__init__(self)
self.shape = "circle"
class Missile(Sprite):
def __init__(self):
Sprite.__init__(self)
self.shape = "circle"
self.size = 0.2
self.active = False
def update(self):
if self.active:
self.x += self.dx
self.y += self.dy
if self.x > 400:
self.active = False
elif self.x < -400:
self.active = False
if self.y > 300:
self.active = False
elif self.y < -300:
self.active = False
def fire(self):
if not self.active:
self.active = True
self.x = player.x
self.y = player.y
self.heading = player.heading
self.dx = math.cos(math.radians(self.heading)) * 1
self.dy = math.sin(math.radians(self.heading)) * 1
sprites = []
player = Player()
sprites.append(player)
missile = Missile()
sprites.append(missile)
for _ in range(5):
asteroid = Asteroid()
x = random.randint(-375, 375)
y = random.randint(-275, 275)
asteroid.goto(x, y)
dx = random.randint(-5, 5) / 20.0
dy = random.randint(-5, 5) / 20.0
asteroid.dx = dx
asteroid.dy = dy
size = random.randint(10, 30) / 10.0
asteroid.size = size
sprites.append(asteroid)
wn.listen()
wn.onkeypress(player.rotate_left, "Left")
wn.onkeypress(player.rotate_right, "Right")
wn.onkeypress(player.accelerate, "Up")
wn.onkeypress(player.decelerate, "Down")
wn.onkeypress(missile.fire, "space")
while True:
pen.goto(-350, 250)
pen.write(f"{player.score}", False, font=("commodore 64 pixelized", 18, "normal"))
pen.clear()
for i in range(player.lives):
pen.goto(-350 + 30 * i, 225)
pen.shape("triangle")
pen.shapesize(0.7, 0.7, 0)
pen.setheading(90)
pen.stamp()
for sprite in sprites:
sprite.update()
sprite.render(pen)
for sprite in sprites:
if isinstance(sprite, Asteroid):
if player.is_collision(sprite):
player.lives -= 1
player.goto(0, 0)
sprite.goto(100, 100)
if player.lives <= 0:
player.active = False
if player.lives == 0:
wn.bye()
if missile.active and missile.is_collision(sprite):
missile.active = False
player.score += 10
sprite.goto(100, 100)
wn.mainloop()