-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
274 lines (188 loc) · 8.54 KB
/
test.py
File metadata and controls
274 lines (188 loc) · 8.54 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# Natalie Sanders
# PyGame Primer
import pygame
from math import pi, atan2, sin, cos
import sys
class GameSpace:
def main(self):
# BASIC INITIALIZATION
pygame.mixer.pre_init(44100, -16, 2, 2048) # setup mixer to avoid sound lag
pygame.init()
self.url = "/afs/nd.edu/user37/cmc/Public/cse332_sp15/pygame_primer/media/"
self.size = self.width, self.height = 640, 480
self.black = 0, 0, 0
# INITIALIZE MUSIC/SOUNDS
pygame.mixer.music.load("./imperialmarch.wav") # LOAD MUSIC
self.beamSound = pygame.mixer.Sound(self.url + "screammachine.wav") # LOAD LASER BEAM SOUND
self.explodeSound = pygame.mixer.Sound(self.url + "explode.wav") # LOAD EXPLOSION SOUND
self.screen = pygame.display.set_mode(self.size)
pygame.key.set_repeat(1, 50)
# SET UP GAME OBJECTS
self.clock = pygame.time.Clock()
self.player = Player(self)
self.target = Target(self)
self.explosion = Explosion(self)
# PLAY MUSIC
pygame.mixer.music.play(-1)
# START GAME LOOP
running = True
while running:
# CLOCK TICK REGULATION (FRAMERATE)
self.clock.tick(60)
# HANDLE USER INPUTS
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
running = False
if event.type == pygame.KEYDOWN:
self.player.keyDown(event.key)
if event.type == pygame.KEYUP:
self.player.keyUp(event.key)
# SEND TICK TO ALL GAME OBJECTS
self.player.tick()
for laser in self.player.lasers: # GIVE TICK TO ALL CURRENT LASER OBJECTS
laser.tick()
if self.target.life: # GIVE TICK TO TARGET IF IT STILL HAS LIVES
self.target.tick()
elif not self.explosion.done:
self.explosion.tick() # GIVE TICK TO EXPLOSION IF TARGET HAS NO LIVES
# DISPLAY GAME OBJECTS
self.screen.fill(self.black) # CLEAR SCREEN
for laser in self.player.lasers:
self.screen.blit(laser.image, laser.rect) # BLIT ALL CURRENT LASTERS TO SCREEN
self.screen.blit(self.player.image, self.player.rect) # BLIT PLAYER TO SCREEN
if self.target.life:
self.screen.blit(self.target.image, self.target.rect) # BLIT TARGET TO SCREEN IF TARGET HAS LIVES LEFT
elif not self.explosion.done:
self.screen.blit(self.explosion.image, self.explosion.rect) # BLIT EXPLOSION TO SCREEN IF TARGET HAS NO LIVES LEFT
pygame.display.flip()
class Laser(pygame.sprite.Sprite):
def __init__(self, pl=None):
pygame.sprite.Sprite.__init__(self)
self.pl = pl
self.onscreen = True
self.image = pygame.image.load(self.pl.gs.url + "laser.png")
self.rect = self.image.get_rect()
self.orig_image = self.image
# CALCULATE LASER ANGLE
mx, my = pygame.mouse.get_pos()
self.lcx, self.lcy = self.rect.center
#+ 2*self.dy
self.angle = atan2(my - self.pl.pcy, mx - self.pl.pcx)
self.dy = 10*sin(self.angle)
self.dx = 10*cos(self.angle)
# POSITION LASER
self.rect = self.rect.move(self.pl.pcx-10, self.pl.pcy-10)
self.angle = 0
def tick(self):
self.rect = self.rect.move(self.dx, self.dy)
def move(self, dx, dy):
new_rect = self.rect.move(dx, dy)
if new_rect.right < 640 or \
new_rect.left > 0 or \
new_rect.bottom < 480 or \
new_rect.top > 0:
self.rect = new_rect
else:
self.offscreen = True
def rotate(self, angle):
self.image = pygame.transform.rotate(self.orig_image, angle)
class Player(pygame.sprite.Sprite):
def __init__(self, gs=None):
pygame.sprite.Sprite.__init__(self)
self.gs = gs
self.image = pygame.image.load(self.gs.url + "deathstar.png")
self.rect = self.image.get_rect()
self.lasers = list()
# KEEP ORIGINAL IMAGE TO LIMIT RESIZE ERRORS
self.orig_image = self.image
self.angle = 0
# LASER FIRING FLAG
self.tofire = False
def tick(self):
# GET MOUSE X AND Y POSITION ON SCREEN
mx, my = pygame.mouse.get_pos()
self.pcx, self.pcy = self.rect.center
# PREVENT MOVEMENT WHILE FIRING
if self.tofire:
# EMIT LASER BEAM
self.lasers.append(Laser(self))
else:
# CALCULATE ANGLE BTW CURRENT DIRECTION & MOUSE POSITION
self.angle = -1*(atan2(my - self.pcy, mx - self.pcx)*(180/pi) + 45)
# ROTATE IMAGE TO FACE MOUSE
self.image = pygame.transform.rotate(self.orig_image, self.angle)
self.rect = self.image.get_rect(center=[self.pcx, self.pcy])
def updateLasers(self, update, move=False, rotate=False):
if move:
dx = update[0]
dy = update[1]
for l in self.lasers:
l.move(dx, dy)
if rotate:
angle = rotate
for l in self.lasers:
l.rotate(angle)
def keyDown(self, key):
if key == pygame.K_DOWN: # down key
new_rect = self.rect.move(0, 5)
if not new_rect.bottom > 480:
self.rect = new_rect
elif key == pygame.K_UP: # up key
new_rect = self.rect.move(0, -5)
if not new_rect.top < 0:
self.rect = new_rect
elif key == pygame.K_RIGHT: # right key
new_rect = self.rect.move(5, 0)
if not new_rect.right > 640:
self.rect = new_rect
elif key == pygame.K_LEFT: # left key
new_rect = self.rect.move(-5, 0)
if not new_rect.left < 0:
self.rect = new_rect
elif key == pygame.K_SPACE:
self.tofire = True
self.gs.beamSound.play()
def keyUp(self, key):
if key == pygame.K_SPACE:
self.tofire = False
class Target(pygame.sprite.Sprite):
def __init__(self, gs=None):
pygame.sprite.Sprite.__init__(self)
self.gs = gs
self.image = pygame.image.load(self.gs.url + "globe.png")
self.rect = self.image.get_rect()
self.rect.move_ip(300, 200)
# KEEP TRACK OF HITS:
self.life = 1000
def tick(self):
if self.life <= 100:
self.image = pygame.image.load(self.gs.url + "globe_red100.png")
for laser in self.gs.player.lasers:
collide = self.rect.colliderect(laser.rect)
if collide:
#del self.gs.player.lasers[collide_index]
self.life = self.life - 1
if not self.life:
self.gs.explodeSound.play()
break
class Explosion(pygame.sprite.Sprite):
def __init__(self, gs=None):
pygame.sprite.Sprite.__init__(self)
self.gs = gs
self.image = pygame.image.load(self.gs.url + "explosion/frames000a.png")
self.image
self.rect = self.image.get_rect()
self.rect.move_ip(300, 200)
self.imageNo = 0
self.done = False
def tick(self):
self.imageNo = self.imageNo + 1
if self.imageNo > 16:
self.done = True
else:
imageStr = "frames" + str(self.imageNo).zfill(3) + "a.png"
self.image = pygame.image.load(self.gs.url + "explosion/" + imageStr)
if __name__ == '__main__':
gs = GameSpace()
gs.main()