-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPygames Space Invaders beta
290 lines (225 loc) · 9.75 KB
/
Pygames Space Invaders beta
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#!/usr/bin/env python
'''Space Invaders game written by Rob McKinley IV'''
'''Import pygame for graphics, import sys for exit function, random for random numbers
os is used for environment variables to set the position to centre'''
import pygame, sys, random, os
'''import constants used by pygame such as event type = QUIT'''
from pygame.locals import *
'''Declare classes and functions'''
def initPyGame():
'''Initialize pygame components'''
pygame.init()
'''
Centres the pygame window. Note that the environment variable is called
SDL_VIDEO_WINDOW_POS because pygame uses SDL (standard direct media layer)
for it's graphics, and other functions
'''
os.environ['SDL_VIDEO_WINDOW_POS'] = 'center'
'''Set the window title'''
pygame.display.set_caption("Space Invaders!")
'''hide the mouse cursor'''
pygame.mouse.set_visible(False)
class coachClass(pygame.sprite.Group):
def __init__(self, sprites=None):
pygame.sprite.Group.__init__(self, sprites)
self.change = False
def update(self):
if self.change:
self.change = False
piClass.speedx = -piClass.speedx
pygame.sprite.Group.update(self)
class backgroundClass():
'''Class containing properties and methods for background'''
def __init__(self):
'''Variable to store background image is called'''
backgroundfile = "background.png"
'''Create a pygame surface called image with background image'''
baseimage = pygame.image.load(backgroundfile).convert()
self.image = pygame.transform.scale(baseimage, (int(baseimage.get_width() * 1.5), int(baseimage.get_height() * 1.5)))
def draw(self):
'''Code to draw the background to screen surface'''
screen.blit(self.image, (0,0))
class crosshairsClass(pygame.sprite.Sprite):
'''This class inherits functions from pygame.sprite.Sprite
containing methods and properties for crosshairs'''
def __init__(self):
'''Initialize a sprite, passing this instance as a parameter'''
pygame.sprite.Sprite.__init__(self)
'''Variable to store what image is called'''
crosshairsfile = "crosshairsmouse.png"
'''get image from crosshairs file and rectangle from image'''
self.image = pygame.image.load(crosshairsfile).convert_alpha()
self.rect = self.image.get_rect()
def update(self):
'''move the crosshairs to where the mouse is'''
'''get the mouse position'''
position = pygame.mouse.get_pos()
screenrect = screen.get_rect()
if not pygame.mouse.get_focused() or position[1] < screenrect.bottom - 50:
position = (-200, 0)
pygame.mouse.set_visible(True)
else:
'''hide the mouse cursor'''
pygame.mouse.set_visible(False)
'''assign it to the centre of rectangle'''
self.rect.center = position
class bulletClass(pygame.sprite.Sprite):
attacker = None
defender = None
def __init__(self, position, speedy, targets):
pygame.sprite.Sprite.__init__(self)
if bulletClass.attacker is None:
bulletClass.attacker = pygame.Surface((6, 18))
bulletClass.attacker.fill(pygame.Color('red'))
bulletClass.defender = pygame.Surface((6, 18))
bulletClass.defender.fill(pygame.Color('yellow'))
if speedy < 0: # Defender bullet
self.image = bulletClass.defender.convert()
else: # Attacker bullet
self.image = bulletClass.attacker.convert()
self.rect = self.image.get_rect()
self.rect.centerx = position[0]
self.rect.centery = position[1]
self.speedy = speedy
self.targets = targets
def update(self):
global gameover, hits
self.rect.top += self.speedy
if pygame.sprite.spritecollide(self, self.targets, True):
if self.speedy < 0:
hits += 1
bullets.remove(self)
if len(cross) == 0:
gameover = True
'''Get the rectangle for the screen'''
screenrect = screen.get_rect()
if self.rect.bottom <= 0 or self.rect.top >= screenrect.bottom:
bullets.remove(self)
class piClass(pygame.sprite.Sprite):
'''This class inherits functions from pygame.sprite.Sprite
containing methods and properties for pi'''
image = None
'''Variable to store image is called'''
image_file = "invader1.png"
speedx = 10
def __init__(self, startx=10, starty=0, speedx=10, speedy=25, random_scale=False):
'''Initialize a sprite, passing this instance as a parameter'''
pygame.sprite.Sprite.__init__(self)
if piClass.image is None:
'''get image from pi file and rectangle from image'''
piClass.image = pygame.image.load(self.image_file)
piClass.speedx = speedx
if random_scale:
scale = random.randint(4, 12)
w = int((piClass.image.get_width() * scale) / 10.0)
h = int((piClass.image.get_height() * scale) / 10.0)
else:
w, h = piClass.image.get_size()
self.image = pygame.transform.scale(piClass.image, (w, h)).convert_alpha()
'''set right of rectangle to start x and y co ordinates'''
self.rect = self.image.get_rect()
self.rect.left = startx
self.rect.centery = starty
'''How many pixels to move the pi image across the screen'''
self.speedx = speedx
self.speedy = speedy
def update(self):
global gameover, coach
nz = 10
'''Add pispeed to current rectangle x value so it moves horizontaly across the screen'''
if self.speedx != piClass.speedx:
self.speedx = piClass.speedx
self.rect.top += self.speedy
self.rect.right += self.speedx
'''Get the rectangle for the screen'''
screenrect = screen.get_rect()
if self.rect.bottom >= screenrect.bottom - 50:
gameover = True
'''If the left edge of pi is >= edge of screen then...'''
if self.speedx == piClass.speedx and (self.rect.right >= screenrect.right - nz or self.rect.left <= nz):
'''set rectangle right x value back to zero'''
self.groups()[0].change = True
if random.randint(1, 500) == 1:
b = bulletClass((self.rect.centerx, self.rect.bottom), 25, cross)
bullets.add(b)
def eventHandling():
global shots
'''The code below handles event'''
for event in pygame.event.get():
'''Close the program when the X button is pressed'''
if event.type == QUIT:
pygame.quit()
exit()
if event.type == MOUSEBUTTONDOWN:
'''The mouse has been clicked so see if the sprites rectangles collide
The pygame.sprite.collide_rect returns either True or False.
'''
screenrect = screen.get_rect()
if event.pos[1] >= screenrect.bottom - 50:
if event.button == 1: # Left button
if len(bullets) < 4:
bullets.add(bulletClass(event.pos, -12, invaders))
shots += 1
elif event.button == 3: # Right button
if len(bullets) <= 1:
bullets.add([bulletClass((i, event.pos[1]), -12, invaders) for i in range(event.pos[0] - 30, event.pos[0] + 40, 20)])
shots += 4
elif len(bullets) < 4:
bullets.add(bulletClass(event.pos, -12, invaders))
shots += 1
hit = pygame.sprite.collide_rect(crosshairs, pi)
if hit == True:
'''The crosshairs was over the pi sprite when mouse was clicked'''
print "hit"
'''Initialize pygame and set some environment variables'''
initPyGame()
'''Initialize a display with width 800 and height 600 with 32 bit colour'''
screen = pygame.display.set_mode((800, 600), 0, 32)
'''Used to manage how fast the screen updates'''
clock = pygame.time.Clock()
'''variable for how many loops a second'''
framesPerSecond = 30
'''Declare class instances to be used in game loop'''
background = backgroundClass()
crosshairs = crosshairsClass()
pis = []
for j in range(5):
pis.extend([piClass(10 + j * 80, 50 + i * 80) for i in range(5)])
'''create a sprite Group which will contain all sprites'''
invaders = coachClass(pis)
cross = pygame.sprite.Group(crosshairs)
bullets = pygame.sprite.Group()
gameover = False
hits = 0
shots = 0
coach = False
while True:
'''The game loop'''
'''Limit screen updates to 20 frames per second so we dont use 100% cpu time'''
clock.tick(framesPerSecond)
'''Runs the code in the event handling function'''
eventHandling()
if not gameover:
'''Draws background to display surface'''
background.draw()
'''run the update code in for sprites and then draw them to the screen'''
invaders.update() # This is really a coach, not a generic pygame Group
invaders.draw(screen)
cross.update()
cross.draw(screen)
bullets.update()
bullets.draw(screen)
gameover = gameover or len(invaders) == 0
'''update the full display surface to the screen'''
pygame.display.update()
else:
# Adjust accuracy by discounting unresolved shots
for bullet in bullets:
if bullet.speedy < 0:
shots -= 1
print 'Game Over!'
print hits, 'hits!'
if shots > 0:
print float(hits) / shots * 100, '% Accuracy!'
pygame.mouse.set_visible(True)
break