-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main.py
executable file
·326 lines (281 loc) · 9.43 KB
/
Main.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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
''' Asteroid Colliders
- @ authors Nick Yaculak & Michael Stevens
- Released under the GNU General Public License 3
'''
import pygame, sys, time, random
from pygame.locals import *
from Player import *
from Asteroid import *
from Laser import *
import random
from CollisionDetection import collide
import dumbmenu as dm
from ProgressBar import ProgressBar
# set up pygame, window, and colors
pygame.init()
mainClock = pygame.time.Clock()
WINDOWWIDTH = 700
WINDOWHEIGHT = 600
size = (WINDOWWIDTH, WINDOWHEIGHT)
screen = pygame.display.set_mode(size)
windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32)
pygame.display.set_caption('Asteroid Colliders')
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
# set up the block data structure
livesImage = pygame.transform.smoothscale(pygame.image.load('res/image/player.png'), [45,45])
MOVESPEED = 10
player = Player(MOVESPEED, size, 64, 64, 8)
asteroids = []
asteroidCounter = 0
NEWASTEROID = 40
lasers = []
laserCounter = 0
laserTimer = 0
ScoreTimer = 0
Score = 0
Timer = 0
CYCLECOUNTER = 0
# set up keyboard variables
isMoveLeft = False
isMoveRight = False
isMoveUp = False
isMoveDown = False
oneThird = 1/3
# set up music
kaboomSound = pygame.mixer.Sound('res/sound/kaboom.wav')
laserSound = pygame.mixer.Sound('res/sound/laser_blast.wav')
pygame.mixer.music.load('res/sound/background.mp3')
pygame.mixer.music.play(-1, 0.0)
musicPlaying = True
laserBar = ProgressBar(5, 5, 200, 5, [255,0,0])
shieldBar = ProgressBar(5, 15, 200, 5, [0,0,255])
healthBar = ProgressBar(5, 25, 200, 5, [0, 255, 0])
def checkQuit(event):
if event.type == QUIT:
pygame.quit()
sys.exit()
def quit():
pygame.quit()
sys.exit()
def addAsteroid():
global asteroidCounter
global WINDOWWIDTH
asteroidCounter += 1
if asteroidCounter >= NEWASTEROID:
# add new asteroid
asteroidCounter = 0
foo = Asteroid(random.randrange(10, 17), random.randrange(0, WINDOWWIDTH - 64), -64, 64, 64, 5)
asteroids.append(foo)
def addLaser():
global laserCounter
global player
laser = Laser(20, player.x, player.y, 15, 82, 10)
if laserCounter < 30:
laserCounter += 1
laser.x = player.x + (player.width / 2) - (laser.width / 2)
laser.y = player.y - laser.height
lasers.append(laser)
laserSound.play()
playGame = 0
def showControls():
while 1:
pygame.display.update()
pygame.display.set_caption('Controls')
pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32)
screen.fill(BLACK)
# Create a font
head = pygame.font.Font(None, 50)
# Render the text
text = head.render('Powered by Python and PyGame', True,
GREEN, BLACK)
text2 = head.render('These are the game controls', True, GREEN,
BLACK)
# Create a rectangle
textRect = text.get_rect()
text2Rect = text2.get_rect()
# Center the rectangle
textRect.centerx = screen.get_rect().centerx
textRect.y = screen.get_rect().y
x = 100
y = 100
text2Rect.x = screen.get_rect().x
text2Rect.y = screen.get_rect().y
# Blit the text
screen.blit(text, textRect)
#screen.blit(text2, text2Rect)
pygame.display.quit
for event in pygame.event.get():
checkQuit(event)
if event.type == KEYDOWN:
if event.key == K_LEFT:
mainMenu()
def pauseMenu():
global musicPlaying
while 1:
#558, 90
# title = pygame.image.load('res/image/title.png')
# titlerect = title.get_rect(center=(350, 300))
# screen.blit(title, titlerect)
# pygame.display.flip()
choose = dm.dumbmenu(screen, [
'Resume Game',
'Quit Game'], 64, 64, None, 32, 1.4, GREEN, GREEN)
if choose == 0:
print('Resuming Game!')
global playGame
playGame = 1
break
if choose == 1:
print('Exiting Game!')
quit()
def mainMenu():
global musicPlaying
while 1:
screen.fill(BLACK)
#558, 90
title = pygame.image.load('res/image/title.png')
titlerect = title.get_rect(center=(350, 300))
screen.blit(title, titlerect)
pygame.display.flip()
choose = dm.dumbmenu(screen, [
'Start Game',
'Options',
'Controls',
'Highscores',
'Quit Game',
'Toggle Music'], 375, 400, None, 32, 1.4, GREEN, GREEN, True)
if choose == 0:
print('Starting Game!')
global playGame
playGame = 1
break
if choose == 1:
print('Showing options!')
#showOptions()
if choose == 2:
print('Showing controls!')
showControls()
if choose == 3:
print('Showing highscores!')
#showHighscores()
if choose == 4:
print('Exiting Game')
quit()
if choose == 5:
if musicPlaying:
pygame.mixer.music.stop()
else:
pygame.mixer.music.play(-1, 0.0)
musicPlaying = not musicPlaying
mainMenu()
# run the game loop
while playGame == 1:
for event in pygame.event.get():
checkQuit(event)
if event.type == KEYDOWN:
# change the keyboard variables
if event.key == K_LEFT or event.key == ord('a'):
player.moveLeft(True)
if event.key == K_RIGHT or event.key == ord('d'):
player.moveRight(True)
if event.key == K_UP or event.key == ord('w'):
player.moveUp(True)
if event.key == K_DOWN or event.key == ord('s'):
player.moveDown(True)
if event.key == ord('f'):
addLaser()
if event.key == ord('p'):
pauseMenu()
if event.key == K_SPACE:
if player.isShield:
player.isShield = False
else:
player.isShield = True
if event.type == KEYUP:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
if event.key == K_LEFT or event.key == ord('a'):
player.moveLeft(False)
if event.key == K_RIGHT or event.key == ord('d'):
player.moveRight(False)
if event.key == K_UP or event.key == ord('w'):
player.moveUp(False)
if event.key == K_DOWN or event.key == ord('s'):
player.moveDown(False)
if event.key == ord('m'):
if musicPlaying:
pygame.mixer.music.stop()
else:
pygame.mixer.music.play(-1, 0.0)
musicPlaying = not musicPlaying
windowSurface.fill(BLACK)
# draw the player
player.update(pygame.time.get_ticks())
player.draw(windowSurface)
#make the player explode if he dies
if player.lives <= 0:
player.explode()
if player.sprite.done:
quit()
# draw the asteroids
addAsteroid()
for asteroid in asteroids:
asteroid.update(pygame.time.get_ticks())
asteroid.draw(windowSurface)
# draw the lasers
for laser in lasers:
laser.draw(windowSurface)
laser.update(pygame.time.get_ticks())
# check if a laser collides with an asteroid
for laser in lasers:
for asteroid in asteroids:
if(collide(laser, asteroid)):
#lasers.remove(laser)
asteroids.remove(asteroid)
kaboomSound.play()
if laser.y < 0 - laser.height:
lasers.remove(laser)
# check if the player has collided with an asteroid
for asteroid in asteroids:
c = False
if player.isShield:
c = collide(player.shield, asteroid)
else:
c = collide(player, asteroid)
if c:
asteroids.remove(asteroid)
if not player.isShield:
player.lives -= 1;
if musicPlaying:
kaboomSound.play()
Timer += 1
if Timer > 500:
NEWASTEROID /= 1.2
CYCLECOUNTER += 1
Timer = 0
laserTimer += 1
if laserTimer > 500 and laserCounter != 0:
laserCounter -= 1
laserTimer = 0
ScoreTimer += 1
if ScoreTimer > 50:
Score += 1
ScoreTimer = 0
#draw lives and other player information bars
for life in range(0, player.lives):
windowSurface.blit(livesImage, [livesImage.get_height()*life, WINDOWHEIGHT - livesImage.get_height()])
laserBar.update((30 - laserCounter)/30)
laserBar.draw(windowSurface)
shieldBar.update((player.shield.timeLimit - player.timer)/player.shield.timeLimit)
shieldBar.draw(windowSurface)
healthBar.update(player.lives / 3)
healthBar.draw(windowSurface)
head = pygame.font.Font(None, 20)
text = head.render("Score: " + str(Score), True, [255,255,255])
windowSurface.blit(text, [30, 50])
# draw the window onto the screen
pygame.display.update()
mainClock.tick(40)