-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlevel3.py
More file actions
391 lines (390 loc) · 16.7 KB
/
level3.py
File metadata and controls
391 lines (390 loc) · 16.7 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
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
#################
#level3.py citation comment
# Lines 12 - 285 Original Code
# Lines 286 - 290 Taken from stackoverflow link below, same technique used whenever you see a transparent rectangle
# https://stackoverflow.com/questions/6339057/draw-a-transparent-rectangle-in-pygame
# Lines 291 - 308 Original code
# 309 - 315 Lukas Pereze 112 Blog
# 316 - 391 Original code placed in event handler inspired by https://github.com/OrionDark7/pyweek26/blob/master/main.py
################
import module_manager
module_manager.ignore_module('starterScreen.py')
module_manager.ignore_module('display.py')
module_manager.ignore_module('characters.py')
module_manager.review()
import pygame
import random
import math
from characters import TrafficLights, Car
from display import *
from starterScreen import PauseScreen, WinnerScreen
#from levels import level1Board
import images
pygame.init()
pygame.font.init()
pygame.font.get_fonts()
####################################
fps = 100
width = 1280
height = 700
green = (0,100,0)
red = (255,0,0)
black = (0,0,0)
beige = (250,250,210)
#data.scoreFont = pygame.font.SysFont("verdana", 25)
screen = pygame.display.set_mode((width, height))
clock = pygame.time.Clock()
def init(data):
data.test = True
data.outtaTime = False
data.collision = False
data.scoreFont = pygame.font.SysFont("verdana", 25)
data.pauseFont = pygame.font.SysFont("verdana", 56)
data.resumeFont = pygame.font.SysFont("verdana", 35)
data.gameOverFont = pygame.font.SysFont("bauhaus93", 56)
data.gameOverFont.set_bold(True)
data.idleTime = 160
data.stop = False
data.score = 0
data.timeLeft = 60
data.gameOver = False
data.isPaused = False
data.gameMode = "starter"
data.margin = 40
data.imageWidth = 30
data.imageHeight = 50
data.goal = 25
data.width = width
data.height = height
data.trafficLights = [ TrafficLights(data.width//2 - 85, data.height - data.height//2 + 85, 170,20, \
"Horizontal", True, "north"),
TrafficLights(data.width//2 - 120, data.height//2 - 85, 170,20, "Vertical", False, "east"),
TrafficLights(data.width//2 + 110, data.height//2 - 85, 170, 20, "Vertical", False, "west"),
TrafficLights(data.width//2 - 85, data.height - data.height//2 - 105, 170,20, "Horizontal", True, "south")
]
data.objects = []
data.spriteObjects = pygame.sprite.Group()
data.vehicleOptions = [ "Car" ]
data.spawnPoint1 = ((data.width//2 + 25),(data.height - data.imageHeight//2),"NORTH")
data.spawnPoint2 = (data.width//2 - 50 ,data.imageHeight - 25 , "SOUTH")
data.spawnPoint3 = (data.imageWidth//2, data.height//2 + 35, "EAST")
data.spawnPoint4 = ((data.width - data.imageHeight//2),data.height//2 - 45, "WEST")
data.spawnPoints = [data.spawnPoint1, data.spawnPoint2, data.spawnPoint3, data.spawnPoint4]
data.blue = 25,25,112
data.yellow = 255,255,0
data.red = 255,0,0
data.black = 255,255,255
data.current = None
data.colors = ["blue", "red", "pink", "green"]
#Make a dictionary of colors with their images already in it
data.images = { "blue":['blueCar.png', 'blueCarSouth.png','blueCarWest.png', 'blueCarEast.png'],
"red": ['redCarNorth.png', 'redCarSouth.png', 'redCarWest.png', 'redCarEast.png'],
"green": ['greenCarNorth.png', 'greenCarSouth.png', 'greenCarWest.png', 'greenCarEast.png'],
'pink': ['pinkCarNorth.png', 'pinkCarSouth.png', 'pinkCarWest.png', 'pinkCarEast.png']
}
data.pauseScreen = PauseScreen(data.width, data.height, data.score, data.timeLeft, 3)
data.finishedScreen = CompletedScreen(data.width,data.height, data.score, data.timeLeft, 3)
data.isComplete = False
def lightFacing(data, direction):
for light in data.trafficLights:
if light.whatCardinal() == direction:
return light
def timerFiredNewCharacter(data):
takenSpots = [False,False,False,False]
for object in data.objects:
if object.getDirection() == "Direction NORTH":
if (data.height - data.imageHeight) - object.getCordinates()[1] < 45:
takenSpots[0] = True #not takenSpots[0]
else:
takenSpots[0] = False # not takenSpots[0]
if object.getDirection() == "Direction SOUTH":
if object.getCordinates()[1] - (data.imageHeight) < 45:
takenSpots[1] = True # not takenSpots[1]
else:
takenSpots[1] = False #not takenSpots[1]
if object.getDirection() == "Direction EAST":
if object.getCordinates()[0] - (data.imageWidth//2) < 55:
takenSpots[2] = True #not takenSpots[2]
else:
takenSpots[2] = False #not takenSpots[2]
if object.getDirection() == "Direction WEST":
if (data.width - data.imageHeight//2) - object.getCordinates()[0] < 55:
takenSpots[3] = True #not takenSpots[3]
else:
takenSpots[3] = False #not takenSpots[3]
for i in range(len(takenSpots)):
if takenSpots[i] == False:
if i == 0:
start = data.spawnPoints[0]
elif i == 1:
start = data.spawnPoints[1]
elif i == 2:
start = data.spawnPoints[2]
else:
start = data.spawnPoints[3]
destination = (0,0)
color = random.choice(data.colors)
newCar = Car(start[0], start[1], data.images[color], color, destination[0],destination[1], start[2])
data.objects.append(newCar)
data.spriteObjects.add(newCar)
else:
pass
def timerFiredMovement(data):
if data.timeLeft == 0 and data.score < 25:
data.gameOver = True
data.outtaTime = True
if data.score >= 25 and data.timeLeft > 0:
#data.winnerScreen.updateNums(data.score,data.timeLeft)
data.isComplete = True
data.gameOver = True
for object in data.objects:
cordinates = object.getCordinates()
test = object.getDirection()[10:]
light = lightFacing(data, test.lower())
if cordinates[0] < 0 or cordinates[0] > data.width or cordinates[1] < 0\
or cordinates[1] > data.height:
#data.current = data.objects[0]
data.objects.remove(object)
object.kill()
data.score += 1
if object.getDirection() == "Direction NORTH":
if object.idle() > data.idleTime:
object.moveUp()
if object.getCordinates()[1] < light.getPosition()[1]:
object.moveUp()
elif light.currentStatus() == True:
object.moveUp()
object.timeStopped = 0
else:
object.stop()
object.addTime()
if object.getDirection() == "Direction SOUTH":
if object.idle() > data.idleTime:
object.moveDown()
if object.getCordinates()[1] > light.getPosition()[1]:
object.moveDown()
elif light.currentStatus() == True:
object.moveDown()
object.timeStopped = 0
else:
object.stop()
object.addTime()
if object.getDirection() == "Direction EAST":
if object.idle() > data.idleTime:
object.moveRight()
if object.getCordinates()[0] > light.getPosition()[0]:
object.moveRight()
elif light.currentStatus() == True:
object.moveRight()
object.timeStopped = 0
else:
object.stop()
object.addTime()
if object.getDirection() == "Direction WEST":
if object.idle() > data.idleTime:
object.moveLeft()
if object.getCordinates()[0] < light.getPosition()[0]:
object.moveLeft()
elif light.currentStatus() == True:
object.moveLeft()
object.timeStopped = 0
else:
object.stop()
object.addTime()
object.updateRect()
if object.checkCollisions(data.objects):
data.gameOver = True
object.stop()
data.collision = True
def mousePressed(data, x,y):
# for i in range(len(data.objects)):
# pos = data.objects[i].getCordinates()
# dimensions = data.objects[i].getDimensions()
# boundsX = pos[0] + dimensions[0]
# boundsY = pos[1] + dimensions[1]
# if pos[0] < x < boundsX and pos[1] < y < boundsY:
# return i
if 130 < x < 210 and 5 < y < 30:
# print("YUH")
data.isPaused = True
data.gameOver = True
# print(data.isPaused)
# print(data.gameOver)
return False
for light in data.trafficLights:
if light.orientation == "Horizontal":
attributes = light.getPosition()
boundsX = attributes[0] + attributes[2]
boundsY = attributes[1] + attributes[3]
if attributes[0] < x < boundsX and attributes[1] < y < boundsY:
light.changeLight()
if light.orientation == "Vertical":
attributes = light.getPosition()
boundsX = attributes[0] + attributes[3]
boundsY = attributes[1] + attributes[2]
if attributes[0] < x < boundsX and attributes[1] < y < boundsY:
light.changeLight()
def keyPressed(data, key):
if data.gameOver == True:
if key == pygame.K_h:
#displayRun()
return "starter"
elif key == pygame.K_r:
# print("BOI")
#init(data)
return "Level 3"
else:
return "Level 3"
# elif data.gameOver == False:
# if key == pygame.K_p:
# data.isPaused = not data.isPaused
# #data.gameOver = True
# return 'paused'
if data.isPaused == True:
data.pauseScreen.keyPressed()
return 'Level 3'
else:
pass
def reDrawAllLevel3(data,screen):
print("Level 3 Check")
# test = pygame.surface.Surface((data.width,data.height))
# test.fill((255,255,255))
pygame.draw.rect(screen, (65,105,225), (0,0, width,height))
pygame.draw.rect(screen, green, (data.margin,data.margin, (width - data.margin*2),(height - data.margin*2)))
pygame.draw.rect(screen, (0,0,0), (width//2 - 85,0,170, height))
pygame.draw.rect(screen, (0,0,0), (0,height//2 - 85, width, 170))
pygame.draw.rect(screen,(255,255,0), (width//2 - 10,0, 10, height))
pygame.draw.rect(screen, (255,255,0), (width//2 + 10,0, 10, height))
pygame.draw.rect(screen, (255,255,0), (0,height//2 - 10,width,10))
pygame.draw.rect(screen, (255,255,0), (0,height//2 + 10 ,width,10))
pygame.draw.rect(screen, (0,0,0),(width//2 - 65, height//2 -65, 145, 145) )
score = data.scoreFont.render("Score: " + str(data.score), False, (0,0,0))
time = data.scoreFont.render("Time: " + str(data.timeLeft) + " secs", False, (0,0,0))
cars = data.scoreFont.render("Cars Remaing " + str(data.goal - data.score), False, (0,0,0))
screen.blit(cars, (900, 5))
levelName = data.scoreFont.render("LEVEL 3", False, (0,0,0))
screen.blit(levelName, (300, 5))
screen.blit(time, (data.width - 110, 5))
pygame.draw.rect(screen, (0,0,0), (130, 5, 80, 25))
pygame.draw.rect(screen, (255,255,255), (135, 9, 70, 15))
pauseButton = data.scoreFont.render("PAUSE", False, (0,0,0))
screen.blit(pauseButton, (140, 8))
for object in data.objects:
#print(object)
#print(object.getPositionX, object.getPositionY)
facing = object.getDirection
object.drawMe(screen, facing)
for light in data.trafficLights:
# print("Scary Hours")
light.drawLight(screen)
if data.gameOver == False and data.isComplete == False:
screen.blit(score, (5 ,5))
if data.gameOver == True and data.isPaused == False and data.isComplete == False:
s = pygame.Surface((data.width,data.height)) # the size of your rect
s.set_alpha(128) # alpha level
s.fill((255,255,255)) # this fills the entire surface
screen.blit(s, (0,0))
pygame.draw.rect(screen,(205,201,201), (data.width//2 - 220, data.height//2 - 300, 500, 200))
if data.collision:
reason = data.scoreFont.render("Uh Oh. A collision Occured!", False, (0,0,0))
screen.blit(reason, (data.width//2 - 100, data.height//2 - 290))
if data.outtaTime:
reason = data.scoreFont.render("Too Slow. You ran outta time :/", False, (0,0,0))
screen.blit(reason, (data.width//2 - 100, data.height//2 - 290))
message = data.gameOverFont.render("GAME OVER :(", False, (255,0,0))
screen.blit(message, (data.width//2 - 140, data.height//2 - 270))
screen.blit(score, (data.width//2 - 25, data.height//2 - 215))
restart = data.scoreFont.render("Press the 'r' key to restart OR the 'h' key to go home", False, (0,0,0))
screen.blit(restart, (data.width//2 - 160, data.height //2 - 180))
if data.isPaused == True and data.gameOver == True:
data.pauseScreen.pauseReDrawAll(screen)
if data.isComplete == True and data.gameOver == True:
data.finishedScreen.reDrawAllCompletedScreen(screen)
def level3Run():
running = True
class Struct(object): pass
data = Struct()
timer = 0 #seconds
fps = 50
init(data)
timerFiredNewCharacter(data)
data.current = data.objects[0]
# print(data.gameOver)
# print(data.isPaused)
pygame.time.set_timer(pygame.USEREVENT, 1000)
while running:
#print(data.gameOver)
# print("Is Complete: " + str(data.isComplete))
# print("Game Over: " + str(data.gameOver))
# print("Is Paused: " + str(data.isPaused))
time = clock.tick(fps)
# if data.test == True:
# # print("Never Fold")
if data.isComplete == True and data.gameOver == True:
#timerFiredMovement(data)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
choice = data.finishedScreen.mousePressed(*(event.pos))
if choice == "starter":
return "starter"
if choice == "Restart":
init(data)
return "Level 3"
if choice == "survival":
return "survival"
if event.type == pygame.MOUSEMOTION:
data.finishedScreen.mouseMotion(*(event.pos))
if event.type == pygame.KEYDOWN:
pass
if data.gameOver == False and data.isPaused == False:
# print("WE HERE")
timer += 1
if timer % 100 == 0:
timerFiredNewCharacter(data)
timerFiredMovement(data)
for event in pygame.event.get():
if event.type == pygame.USEREVENT:
data.timeLeft -= 1
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
mousePressed(data, *event.pos)
break
# if event.type == pygame.KEYDOWN:
# return keyPressed(data, event.key)
# #break
elif data.isPaused == True and data.gameOver == True:
data.pauseScreen.updateNums(data.score,data.timeLeft)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
if data.pauseScreen.mousePressed(*(event.pos)) == False:
data.isPaused = False
data.gameOver = False
break
#return "Level 1"
if data.pauseScreen.mousePressed(*(event.pos)) == "Restart":
init(data)
return "Level 3"
if data.pauseScreen.mousePressed(*(event.pos)) == "starter":
return "starter"
if event.type == pygame.MOUSEMOTION:
data.pauseScreen.mouseMotion(*(event.pos))
elif data.isPaused == False and data.gameOver == True:
# print("Steady")
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
response = keyPressed(data, event.key)
if response == "Level 3":
init(data)
return response
# else:
# # print("HELLLOOOOO")
reDrawAllLevel3(data, screen)
pygame.display.update()
pygame.quit()