-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver.py
executable file
·399 lines (325 loc) · 15.2 KB
/
driver.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# Driver
import pygame
from pygame import *
from sys import exit
import time as pause
# import other class objects
from entity import Entity
from player import Player
from platform import Platform
from powerUp import PowerUp
from fire import Fire
from boss import Boss
from enemy import Enemy
from level import Level
# camera for scrolling
from camera import *
pygame.init()
############################################################################################
# Title Screen
def titleScreen():
title = True # True while the game waits for the user to make a choice
music = pygame.mixer.Sound('sounds/songs/titleSong.wav')
music.play()
screen = pygame.display.set_mode(DISPLAY, FLAGS, DEPTH) # information comes from the camera class
screen_rect = screen.get_rect() # get the screen rect
pygame.display.set_caption("Seven Deadly") # Window caption
background_image = pygame.transform.scale(pygame.image.load("images/intro/title.png"),
(WIN_WIDTH, WIN_HEIGHT)) # Load the title background
# All of the button images
play = pygame.image.load('images/intro/play.png')
play2 = pygame.image.load('images/intro/play2.png')
tut = pygame.image.load('images/intro/tutorial.png')
tut2 = pygame.image.load('images/intro/tutorial2.png')
# Blit the initial images to the screen; order: PLAY, TUT
screen.blit(background_image, (0,0))
b1 = screen.blit(play, (WIN_WIDTH/2.45, WIN_HEIGHT*.6))
b2 = screen.blit(tut, (WIN_WIDTH/2.8, WIN_HEIGHT*.8))
# We want the cursor on the main menu and tutorial screen.
pygame.mouse.set_visible(True)
# Keep the title screen looping while the user makes a decision;
# checks for clicking within a image to perform the action (color changes when scrolled over)
while title == True:
for e in pygame.event.get():
pos = pygame.mouse.get_pos()
if e.type == QUIT: # "X"ed out of the game
raise SystemExit()
if e.type == MOUSEMOTION: # If the user scrolls over one of the buttons change to the alternate color
if b1.collidepoint(pos):
screen.blit(play2, (WIN_WIDTH/2.45, WIN_HEIGHT*.6))
elif b2.collidepoint(pos):
screen.blit(tut2, (WIN_WIDTH/2.8, WIN_HEIGHT*.8))
else: # Show the original background image if the user is not scrolled over the image
screen.blit(play, (WIN_WIDTH/2.45, WIN_HEIGHT*.6))
screen.blit(tut, (WIN_WIDTH/2.8, WIN_HEIGHT*.8))
if e.type == MOUSEBUTTONDOWN:
if b1.collidepoint(pos):
title = False
music.stop()
main() # Click to start the game
if b2.collidepoint(pos):
tutorial(music) # Click to go to the tutorial screen
pygame.display.update() # Update the display
################################################################################################################
# Tutorial Menu
def tutorial(music):
tutstatus = True # True if still on tutorial screen
# Display Settings
screen = pygame.display.set_mode(DISPLAY, FLAGS, DEPTH)
screen_rect = screen.get_rect()
pygame.display.set_caption("Seven Deadly")
background_image = pygame.transform.scale(pygame.image.load("images/intro/tutscreen.png"), (WIN_WIDTH, WIN_HEIGHT)) # Tutorial background
screen.blit(background_image, (0,0))
# Menu buttons
menu = pygame.image.load('images/intro/play.png')
menu2 = pygame.image.load('images/intro/play2.png')
m1 = screen.blit(menu, (WIN_WIDTH*.8, WIN_HEIGHT*.8))
# Continue running while the status is True
while tutstatus == True:
for e in pygame.event.get():
pos = pygame.mouse.get_pos()
if e.type == QUIT: # "X"ed out of the game
raise SystemExit()
if e.type == MOUSEMOTION:
if m1.collidepoint(pos): # Scrolling over the Main Menu button, so change the image so the user knows they are on it
screen.blit(menu2, (WIN_WIDTH*.8, WIN_HEIGHT*.8))
else:
screen.blit(menu, (WIN_WIDTH*.8, WIN_HEIGHT*.8)) # Change back to the normal image since the user is no longer on it
if e.type == MOUSEBUTTONDOWN:
if m1.collidepoint(pos):
music.stop()
main() # Clicked to start the game
pygame.display.update()
#################################################################################
# Game Loop
def main():
currentLevel = 1
while currentLevel < 9:
timer = pygame.time.Clock()
screen = pygame.display.set_mode(DISPLAY, FLAGS, DEPTH) # Set the screen information
screen_rect = screen.get_rect()
pygame.mouse.set_visible(False)
cutScreenImage = pygame.transform.scale(pygame.image.load("images/cutScreens/" + str(currentLevel) +".png"), (WIN_WIDTH, WIN_HEIGHT))
screen.blit(cutScreenImage, (0,0))
pygame.display.update()
pygame.time.delay(3000)
# Load Level class and store info
level = Level(currentLevel)
level.setLevelObs(currentLevel)
song = level.getSong()
background = level.getBackground()
boss = level.getBoss()
enemies = level.getEnemies()
powerUps = level.getPowerUps()
# Create Player
player = Player()
if currentLevel == 4:
player.setSpeed(4)
# Load music
song.play()
print "song Starteddd"
# Load Level Objects with platforms
"""KEY FOR LEVELS
P = Platform
C = player starting position
F = Fire
E = Enemy
B = Boss
U = Power Up
"""
platforms = []
entities = pygame.sprite.Group()
powerUpSprites = pygame.sprite.Group()
fires = pygame.sprite.Group()
enemySprites = pygame.sprite.Group()
bossSprite = pygame.sprite.Group()
bulletSprites = pygame.sprite.Group()
x = 0
y = 0
level = open('levels/level' + str(currentLevel) + '.txt', 'r')
powerCounter = 0
enemyCounter = 0
for row in level:
for col in row:
if col == "P":
p = Platform(x, y) # Place a platform at the given x,y
platforms.insert(0, p) # Insert it into the platforms list
entities.add(p) # Add to entities so it appears on screen
if col == "C":
player.setXpos(x)
player.setYpos(y) # Set the player along with the x,y of the starting position
player.createRect()
entities.add(player)
if col == "F":
fire = Fire(x, y) # Load a fire at the x,y found
entities.add(fire) # Add the fire to the entities
fires.add(fire) # Add the fire to the spike sprite group for collison purposes
if col == "U":
pUp = powerUps[powerCounter] # Load a power up image at the given x,y
pUp.setXpos(x)
pUp.setYpos(y)
pUp.createRect()
entities.add(pUp) # Power up to the entities
powerUpSprites.add(pUp) # add power up to the powerUps sprite group
powerCounter+=1
if col == "E":
enemy = enemies[enemyCounter] # Load an enemy image at the given x,y
enemy.setXpos(x)
enemy.setYpos(y)
enemy.createRect()
entities.add(enemy) # Add the enemy to the entities
enemySprites.add(enemy) # add enemy to the enemies sprite group
enemyCounter+=1
if col == "B":
boss.setXpos(x) # Load a boss image at the given x,y
boss.setYpos(y)
boss.createRect()
entities.add(boss) # Add the boss to the entities
bossSprite.add(boss) # add boss to the boss sprite group
x += 32
y += 32
x = 0
# Load Background for level
background_rect = background.get_rect()
total_level_width = len('level'[0])*32
total_level_height = len('level')*32
camera = Camera(complex_camera, total_level_width, total_level_height)
bossDead = False
nextLevel = True
playerAlive = True
#animation needs
FPS = 60
cycletime = 0
newnr = 0
oldnr = -1
picnr = 0
interval = .075
#interaction needs
buttonPressed = False
upPressed = False
rightPressed = False
leftPressed = False
spacePressed = False
direction = True #True means right
while bossDead == False and playerAlive == True:
pygame.display.set_caption("Seven Deadly | " + " | FPS: " + str(int(timer.get_fps())))
asize = ((screen_rect.w // background_rect.w + 1) * background_rect.w, (screen_rect.h // background_rect.h + 1) * background_rect.h)
bg = pygame.Surface(asize)
# Create the background
for x in range(0, asize[0], background_rect.w):
for y in range(0, asize[1], background_rect.h):
screen.blit(background, (x, y))
# Load controls
for event in pygame.event.get():
if event.type == pygame.QUIT:
raise SystemExit()
if event.type == pygame.KEYDOWN:
buttonPressed = True
if event.key == K_RIGHT:
rightPressed = True
direction = True
if event.key == K_UP:
upPressed = True
if event.key == K_LEFT:
leftPressed = True
direction = False
if event.key == K_UP:
upPressed = True
if event.key == K_UP:
upPressed = True
if event.key == K_SPACE:
spacePressed = True
bullet = player.attackLaunch()
bulletSprites.add(bullet)
entities.add(bullet)
elif event.type == pygame.KEYUP:
if event.key == K_RIGHT:
rightPressed = False
if event.key == K_LEFT:
leftPressed = False
if event.key == K_UP:
upPressed = False
if rightPressed and leftPressed and upPressed and spacePressed == False:
buttonPressed = False
for joe in bulletSprites:
joe.update(screen, bg)
hitSprites = pygame.sprite.spritecollide(joe, entities, False, pygame.sprite.collide_mask)
for sprite in hitSprites:
for plats in platforms:
if sprite == plats:
entities.remove(joe)
bulletSprites.remove(joe)
for enemy in enemySprites:
if sprite == enemy:
bullet.attackHit(enemy)
entities.remove(joe)
bulletSprites.remove(joe)
if enemy.getHealth() <= 0:
entities.remove(enemy)
for boss in bossSprite:
if sprite == boss:
bullet.attackHit(boss)
entities.remove(joe)
bulletSprites.remove(joe)
if boss.getHealth() <= 0:
entities.remove(boss)
bossDead = True
#Animate
if buttonPressed:
milliseconds = timer.tick(FPS)
seconds = milliseconds / 1000.0
cycletime += seconds
if cycletime > interval:
cycletime = 0
newnr += 1
if upPressed:
picnr = newnr % len(player.jonJump)
if rightPressed:
picnr = newnr % len(player.jonWalkRight)
if upPressed:
picnr = newnr % len(player.jonJump)
if leftPressed:
picnr = newnr % len(player.jonWalkLeft)
if upPressed:
picnr = newnr % len(player.jonJump)
oldnr = newnr
# Play the level
for power in powerUpSprites:
if pygame.sprite.spritecollide(player, powerUpSprites, True, pygame.sprite.collide_mask):
power.collected(player)
print player.getHealth()
# Enemy attacking
for enemy in enemySprites:
if player.getDist(enemy) < 100:
enemy.attackLaunch(player)
hitSprite = pygame.sprite.spritecollide(player, enemySprites, False, pygame.sprite.collide_mask)
for sprite in hitSprite:
enemy.attackHit(player)
# Boss attacking
for boss in bossSprite:
x = 5
# Player range limits?
# Update healths?
# Player collision with fire; if true, 50 damage the player
if pygame.sprite.spritecollide(player, fires, False, pygame.sprite.collide_mask):
player.setHealth(player.getHealth()-50)
if player.getHealth() <= 0:
nextLevel = False
playerAlive = False
camera.update(player)
# Update the player and everything else
player.update(upPressed, leftPressed, rightPressed, platforms, screen, bg, direction, picnr)
for e in entities:
screen.blit(e.image, camera.apply(e))
pygame.display.update() # Update the display
if nextLevel == True:
currentLevel += 1
else:
currentLevel = currentLevel
pygame.display.update()
song.stop()
print "You Win"
quit()
##########################################################################################
# Load the title screen to start the game
titleScreen()