-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
498 lines (424 loc) · 12.7 KB
/
game.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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
# IMPORTS
import pygame, math, random, copy
import media, collisions, text
pygame.init()
# GLOBAL VAR SETUP
global gameW, gameH, gameIW, presets, controls, mouse, screenid, inConstruction
gameW, gameH, gameIW = 900, 600, 700
presets = {'keyW':pygame.K_w,'keyA':pygame.K_a,'keyS':pygame.K_s,'keyD':pygame.K_d,'keySpace':pygame.K_SPACE}
controls = {'keyW':False,'keyA':False,'keyS':False,'keyD':False,'keySpace':False}
mouse = {'pos':pygame.mouse.get_pos(),'left_click':False,'left_held':False,'right_click':False,'right_held':False}
screenid = 0
gravity = 0.75
inConstruction = True
# PYGAME WINDOW SETUP
ctx = pygame.display.set_mode((gameW,gameH))
pygame.display.set_caption("Albert")
clock = pygame.time.Clock()
# CLASSES
class Entity(object):
def __init__(self,x,y,w,h,color=media.black):
self.x = x
self.y = y
self.w = w
self.h = h
self.color = color
def pos(self):
pass
def draw(self):
pygame.draw.rect(ctx,self.color,(self.x,self.y,self.w,self.h))
def go(self):
self.pos()
self.draw()
class Goal(Entity):
def __init__(self,x,y,w,h):
super().__init__(x,y,w,h,media.blueOG)
def draw(self):
# pygame.draw.rect(ctx,self.color,(self.x,self.y,self.w,self.h),2)
drawDahsedRect(self.x, self.y, self.w, self.h, 2, 3, 10, self.color)
class RedZone(Entity):
def __init__(self,x,y,w,h,alpha=128):
super().__init__(x,y,w,h,media.red)
self.surface = pygame.Surface((w,h))
self.surface.set_alpha(alpha)
self.surface.fill(self.color)
def draw(self):
ctx.blit(self.surface, (self.x,self.y))
class Selection(Entity):
def __init__(self,x,y,w,h,color,selectionID,strokeWidth=0):
super().__init__(x,y,w,h,color)
self.selectionID = selectionID
self.strokeWidth = strokeWidth
def draw(self):
if self.strokeWidth > 0:
pygame.draw.rect(ctx,self.color,(self.x,self.y,self.w,self.h),self.strokeWidth)
else:
super().draw()
class Material(Entity):
def __init__(self,type):
if type == 0:
w, h = 100, 50
elif type == 1:
w, h = 50, 50
elif type == 2:
w, h = 25, 50
else:
print("Material type " + str(type) + " does not exist")
super().__init__(0,0,w,h,media.mediumBlue)
self.exists = False
self.moving = False
self.type = type
def canPlace(self):
for mb in materialButtons:
if collisions.rectangles(self,mb):
return False
return True
def pos(self):
self.x = mouse['pos'][0]-self.w/2
self.y = mouse['pos'][1]-self.h/2
def go(self):
if self.exists:
if not mouse['left_held']:
if self.canPlace():
self.moving = False
else:
resetMaterial(self)
if self.moving:
self.pos()
self.draw()
class Actor(Entity):
def __init__(self,x,y,w,h,color,spd):
super().__init__(x,y,w,h,color)
self.spd = spd
def lockToGround(self,verticalSpeed):
for i in range(abs(int(verticalSpeed)), -1, -1):
self.y += i
check = self.canMove()
self.y -= i
if check:
return i
return 0
def isOnGround(self):
self.y += 1
check = self.canMove()
self.y -= 1
if check:
return False
else:
return True
class Enemy(Actor):
def __init__(self):
pass
class Player(Actor):
def __init__(self,x,y,w,h,spdX,img,level):
super().__init__(x,y,w,h,media.blueOG,[spdX,0])
self.img = img
self.level = level
def canMove(self):
global inConstruction
if inConstruction:
return False
if self.y > gameH:
inConstruction = True
for o in obstructions:
if collisions.rectangles(self,o):
return False
for u in usedMaterials:
for uu in u:
if collisions.rectangles(self,uu):
return False
if self.y < 0 or self.x < 0 or self.x + self.w > gameIW:
return False
return True
def pos(self):
if controls['keyA'] == True:
self.x -= self.spd[0]
if not self.canMove():
self.x += self.spd[0]
if controls['keyD'] == True:
self.x += self.spd[0]
if not self.canMove():
self.x -= self.spd[0]
self.y -= self.spd[1]
if not self.canMove():
self.y += self.spd[1]
self.y += self.lockToGround(self.spd[1])
self.y = math.ceil(self.y)
self.spd[1] = 0
elif not self.isOnGround():
self.spd[1] -= gravity
if self.isOnGround() and (controls['keyW'] == True or controls['keySpace'] == True):
self.spd[1] = 15
def draw(self):
ctx.blit(self.img[self.level],(self.x,self.y))
# STATIC OBJECT SETUP
levelRects = []
perRow, perColumn = 5, 3
w, h = 150, 125
nowID = 3
for column in range(perColumn):
for row in range(perRow):
levelRects.append(Selection(25 + (w + 25)*row, 25 + (h + 25)*column, w, h, media.blueOG, nowID, 2))
nowID += 1
instructionButton = Selection(250,475,400,100,media.blueOG,1,2)
text.buildLevelSelectText(levelRects, instructionButton)
returnButton = Selection(725,25,150,80,media.darkBlue,2)
materialButtons = [Selection(725,200,100,50,media.mediumBlue,0),
Selection(725,275,50,50,media.mediumBlue,1),
Selection(725,350,25,50,media.mediumBlue,2)]
goButton = Selection(725,475,150,80,media.mediumBlue,-1)
# DYNAMIC OBJECT SETUP
goalLocation = Goal(0,0,30,60)
global obstructions, materials, usedMaterials, redZones, enemies
obstructions = []
materials = [[],[],[]]
usedMaterials = [[],[],[]]
redzones = []
enemies = []
daniel= Player(10, 520, 30, 60, 5, media.playerBody, 0)
class Level(object):
def __init__(self,start,goal,materials,obstructions,redzones=[],enemies=[]):
self.start = start
self.goal = goal
self.obstructions = obstructions
self.redzones = redzones
self.enemies = enemies
self.materials = []
typeIndex = 0
for m in materials:
self.materials.append([])
for i in range(0,m):
self.materials[typeIndex].append(Material(typeIndex))
typeIndex += 1
def load(self):
daniel.x = self.start[0]
daniel.y = self.start[1]
goalLocation.x = self.goal[0]
goalLocation.y = self.goal[1]
global obstructions, materials, usedMaterials
obstructions = copy.deepcopy(self.obstructions)
materials = copy.deepcopy(self.materials)
usedMaterials = [[],[],[]]
# COMMON LEVEL ELEMENTS ARE PREBUILT HERE
ground = Entity(0,580,gameIW,20,media.darkBlue)
startPlatform = Entity(0,580,100,20,media.darkBlue)
# LEVELS ARE BUILT AND ADDED HERE; first 3 empty indeces to comply with opening screens
levels = [0,0,0,
# 1
Level((10,520),(660,10),[4,0,0],
[ground]),
# 2
Level((10,520),(660,10),[1,2,1],
[ground,Entity(345,80,10,520,media.darkBlue)]),
# 3
Level((10,520),(660,10),[0,1,4],
[ground,Entity(195,150,10,450,media.darkBlue),Entity(395,0,10,450,media.darkBlue)]),
# 4
Level((10,520),(660,10),[0,0,4],
[startPlatform]),
# 5
Level((10,520),(660,10),[1,0,3],
[startPlatform,Entity(295,300,10,300,media.darkBlue),Entity(395,0,10,450,media.darkBlue)]),
# 6
Level((10,520),(660,10),[0,1,4],
[startPlatform,Entity(0,470,300,10,media.darkBlue)]),
# End of Designed Levels
# 7
Level((10,520),(660,10),[0,1,4],
[startPlatform,Entity(0,470,300,10,media.darkBlue)]),
# 8
Level((10,520),(660,10),[0,1,4],
[startPlatform,Entity(0,470,300,10,media.darkBlue)]),
# 9
Level((10,520),(660,10),[0,1,4],
[startPlatform,Entity(0,470,300,10,media.darkBlue)]),
# 10
Level((10,520),(660,10),[0,1,4],
[startPlatform,Entity(0,470,300,10,media.darkBlue)]),
# 11
Level((10,520),(660,10),[0,1,4],
[startPlatform,Entity(0,470,300,10,media.darkBlue)]),
# 12
Level((10,520),(660,10),[0,1,4],
[startPlatform,Entity(0,470,300,10,media.darkBlue)]),
# 13
Level((10,520),(660,10),[0,1,4],
[startPlatform,Entity(0,470,300,10,media.darkBlue)]),
# 14
Level((10,520),(660,10),[0,1,4],
[startPlatform,Entity(0,470,300,10,media.darkBlue)]),
# 15
Level((10,520),(660,10),[0,1,4],
[startPlatform,Entity(0,470,300,10,media.darkBlue)]),
]
# HELPER FUNCTIONS
def pickupMaterial(selectionID):
materials[selectionID][0].pos()
materials[selectionID][0].exists, materials[selectionID][0].moving = True, True
usedMaterials[selectionID].append(materials[selectionID][0])
materials[selectionID].remove(materials[selectionID][0])
text.refreshCounter(selectionID, len(materials[selectionID]))
def resetMaterial(material):
material.pos()
material.exists, material.moving = False, False
materials[material.type].append(material)
usedMaterials[material.type].remove(material)
text.refreshCounter(material.type, len(materials[material.type]))
def switchMode():
global inConstruction
inConstruction = not inConstruction
daniel.x = levels[screenid].start[0]
daniel.y = levels[screenid].start[1]
def drawDahsedRect(x, y, w, h, sW, sG, sA, color):
# sW = stroke width (thickness), sG = stroke gap (blank space to left and right of dash in each sA),
# sA = stroke area (the amount of pixels equal to the sum of one stroke length and two stroke gaps)
for dx in range(0,w,sA):
pygame.draw.line(ctx,color,(x + dx + sG, y),(x + dx + sA-sG, y),sW)
pygame.draw.line(ctx,color,(x + dx + sG, y + h-sW/2),(x + dx + sA-sG, y + h-sW/2),sW)
for dy in range(0,h,sA):
pygame.draw.line(ctx,color,(x, y + dy + sG),(x, y + dy + sA-sG),2)
pygame.draw.line(ctx,color,(x + w-sW/2, y + dy + sG),(x + w-sW/2, y + dy + sA-sG),sW)
# SCREEN TYPE FUNCTIONS
def titleScreen():
ctx.fill(media.lightGrey)
ctx.blit(text.title,text.titleRECT)
ctx.blit(text.footer,text.footerRECT)
def instructions():
ctx.fill(media.lightGrey)
ctx.blit(text.instructionsHeader,text.instructionsHeaderRECT)
for l in text.instructions:
ctx.blit(l[0],l[1])
ctx.blit(text.footer,text.footerRECT)
def levelSelect():
ctx.fill(media.lightGrey)
# LEVEL BUTTONS AND TEXT
for l in levelRects:
l.go()
for t in text.levelText:
ctx.blit(t[0],t[1])
# RETURN BUTTON AND TEXT
instructionButton.go()
ctx.blit(text.returnToInstructions,text.returnToInstructionsRECT)
def level():
# BACKGROUNDS
# ctx.fill(media.lightGrey)
ctx.blit(media.background, (0,0))
pygame.draw.rect(ctx, media.levelGrey,(gameIW,0,200,gameH))
pygame.draw.line(ctx, media.black,(gameIW-1,0),(gameIW-1,gameH),2)
pygame.draw.rect(ctx, media.darkBlue,(gameIW-1,580,200+1,20)) # ground below right panel for symmetry
# BUTTONS
returnButton.go()
ctx.blit(text.returnToLevels,text.returnToLevelsRECT)
goButton.go()
if(inConstruction):
ctx.blit(text.goButton,text.goButtonRECT)
else:
ctx.blit(text.stopButton,text.stopButtonRECT)
for mb in materialButtons:
mb.go()
for c in text.counters:
ctx.blit(c[0],c[1])
# PLATFORMING ELEMENTS
for o in obstructions:
o.go()
for u in usedMaterials:
for uu in u:
uu.go()
# PLAYER ELEMENTS
goalLocation.go()
daniel.go()
# LEVEL COMPLETION
if collisions.rectangles(goalLocation,daniel):
global screenid
screenid = 2
def main():
global screenid, inConstruction
running = True
while running:
# EVENT HANDLING - MOUSE
info = pygame.mouse.get_pressed()
if info[0] == True:
if mouse['left_held'] == False:
mouse['left_click'] = True
mouse['left_held'] = True
else:
mouse['left_click'] = False
else:
mouse['left_click'] = False
mouse['left_held'] = False
if info[2] == True:
if mouse['right_held'] == False:
mouse['right_click'] = True
mouse['right_held'] = True
else:
mouse['right_click'] = False
else:
mouse['right_click'] = False
mouse['right_held'] = False
mouse['pos'] = pygame.mouse.get_pos()
# EVENT HANDLING - KEYS
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
for p in presets:
if event.key == presets[p]:
controls[p] = True
elif event.type == pygame.KEYUP:
for p in presets:
if event.key == presets[p]:
controls[p] = False
# CLICK ACTIONS
if mouse['left_click']:
# INTRO SCREENS
if screenid < 2:
screenid += 1
# LEVEL SELECT
elif screenid == 2:
for l in levelRects:
if collisions.pointRect(mouse['pos'],l):
inConstruction = True
screenid = l.selectionID
levels[screenid].load()
for c in range(0,3):
text.refreshCounter(c,len(materials[c]))
if collisions.pointRect(mouse['pos'],instructionButton):
screenid -= 1
# IN-LEVEL ACTIONS
else:
if collisions.pointRect(mouse['pos'],returnButton):
screenid = 2
if inConstruction:
for u in usedMaterials:
for uu in u:
if collisions.pointRect(mouse['pos'],uu):
resetMaterial(uu)
for mb in materialButtons:
if collisions.pointRect(mouse['pos'],mb):
if len(materials[mb.selectionID]) != 0:
pickupMaterial(mb.selectionID)
else:
pass #print("NO MORE OF ID " + str(mb.selectionID))
# SWITCH IN/OUT OF CONSTRUCTION
if collisions.pointRect(mouse['pos'], goButton):
switchMode()
if mouse['right_click']:
if screenid > 2:
switchMode()
# SCREEN STATUS
if screenid == 0:
titleScreen()
elif screenid == 1:
instructions()
elif screenid == 2:
levelSelect()
else:
level()
# DEBUG
fps = media.mulismall.render(str(round(clock.get_fps(),1)),True,media.black)
fpsRECT = fps.get_rect()
ctx.blit(fps,(5,0))
pygame.display.update()
clock.tick(60)
main()
pygame.quit()