-
Notifications
You must be signed in to change notification settings - Fork 0
/
asteroids1_3.py
218 lines (130 loc) · 4.78 KB
/
asteroids1_3.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
196
197
198
199
200
201
202
203
204
205
206
#Dean Church
#asteroids
#rev
#add ship to screen
#rev
#add thrust and screenwrapping
#rev
#add laser and laser sound
#get the asteroids moving on screen
#imports
from superwires import games
import random
import math
games.init(screen_width = 640, screen_height = 480, fps = 60)
#global variables
#classes
class Asteroid(games.Sprite):
SMALL = 1
MEDIUM = 2
LARGE = 3
images = {SMALL : games.load_image("sprites/asteroid3.png"),
MEDIUM : games.load_image("sprites/asteroid2.png"),
LARGE : games.load_image("sprites/asteroid1.png")}
SPEED = 2
def __init__(self,x,y,size):
super(Asteroid, self).__init__(
image = Asteroid.images[size],
x=x,
y=y,
dx = random.choice([1, -1])*Asteroid.SPEED*random.random()/size,
dy = random.choice([1, -1])*Asteroid.SPEED*random.random()/size)
self.size = size
def update(self):
if self.left > games.screen.width:
self.right = 0
if self.right < 0:
self.left = games.screen.width
if self.top > games.screen.height:
self.bottom = 0
if self.bottom < 0:
self.top = games.screen.height
class Ship(games.Sprite):
image = games.load_image("sprites/spaceship.png")
sound = games.load_sound("sounds/soundfx/explosion.wav")
ROTATION_STEP = 8
VELOCITY_STEP = .03
def update(self):
#key setup
if games.keyboard.is_pressed(games.K_a) or games.keyboard.is_pressed(games.K_LEFT):
self.angle -= Ship.ROTATION_STEP
if games.keyboard.is_pressed(games.K_d) or games.keyboard.is_pressed(games.K_RIGHT):
self.angle += Ship.ROTATION_STEP
if games.keyboard.is_pressed(games.K_t):
Ship.sound.play()
#apply thrust
if games.keyboard.is_pressed(games.K_w) or games.keyboard.is_pressed(games.K_UP):
angle = self.angle * math.pi/180
self.dx += Ship.VELOCITY_STEP * math.sin(angle)
self.dy += Ship.VELOCITY_STEP * -math.cos(angle)
#Wrapping screen
if self.left > games.screen.width:
self.right = 0
if self.right < 0:
self.left = games.screen.width
if self.top > games.screen.height:
self.bottom = 0
if self.bottom < 0:
self.top = games.screen.height
#fire missile
if games.keyboard.is_pressed(games.K_SPACE):
Missile.sound.play()
new_missile = Missile(self.x, self.y, self.angle)
games.screen.add(new_missile)
class Missile(games.Sprite):
image = games.load_image("sprites/projectile.png")
sound = games.load_sound("sounds/soundfx/laser.wav")
BUFFER = 5
VELOCITY_FACTOR = 7
LIFETIME = 40
def __init__(self, ship_x, ship_y, ship_angle):
Missile.sound.play()
angle = ship_angle * math.pi/180
buffer_x = Missile.BUFFER * math.sin(angle)
buffer_y = Missile.BUFFER * math.sin(angle)
x = ship_x + buffer_x
y = ship_y + buffer_y
dx = Missile.VELOCITY_FACTOR * math.sin(angle)
dy = Missile.VELOCITY_FACTOR * -math.cos(angle)
super(Missile, self).__init__(image = Missile.image,
x = x, y = y,
dx = dx, dy = dy)
self.lifetime = Missile.LIFETIME
def update(self):
self.lifetime -= 1
if self.lifetime == 0:
self.destroy()
#wrap laser
if self.left > games.screen.width:
self.right = 0
if self.right < 0:
self.left = games.screen.width
if self.top > games.screen.height:
self.bottom = 0
if self.bottom < 0:
self.top = games.screen.height
class Explosion(games.Sprite):
pass
#main
def main():
#loading data
bg_img = games.load_image("sprites/background.png", transparent = False)
#create objects
for i in range(8):
x = random.randrange(games.screen.width)
y = random.randrange(games.screen.height)
size = random.choice([Asteroid.SMALL, Asteroid.MEDIUM, Asteroid.LARGE])
new_asteroid = Asteroid(x=x, y=y, size=size)
games.screen.add(new_asteroid)
player = Ship(image = Ship.image,
x = games.screen.width/2,
y = games.screen.height/2)
shot = Missile(200,200,0)
#draw to screen
games.screen.background = bg_img
games.screen.add(player)
games.screen.add(shot)
#set up game
#start mainloop
games.screen.mainloop()
main()