-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameLogic.py
490 lines (441 loc) · 17.2 KB
/
GameLogic.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
import pygame
import GraphicsLib as GLib
from Util import *
import random
SECSPERPOINT=18
EnemyPositionList=[50,100,150,200,250,300,350,400,450,500,550,600]
# a great example of an object that can move on the screen
class damagetaken:
def __init__(self):
self.update(0)
self.x = 300
self.y = 350
def update(self, damagetext):
myfont=pygame.font.SysFont('Calibri',70,bold=True)
self.img=myfont.render(str(damagetext),1,GLib.RED)
class Score:
def __init__(self):
self.update(0)
self.x = 1095
self.y = 5
def update(self,scoreNum):
myfont=pygame.font.SysFont('Calibri',40,bold=True)
self.img=myfont.render(str(scoreNum),1,GLib.RED)
class Ammo:
def __init__(self):
self.update(20)
self.x=10
self.y=5
def update(self,bulletNum):
myfont=pygame.font.SysFont('Calibri',20,bold=True)
self.img=myfont.render("Ammo:"+str(bulletNum),1,GLib.BLACK)
#original basic boy
class enemy1():
def __init__(self):
self.enemyAnim = [GLib.enemyF,GLib.enemy2]
self.img = GLib.enemyF
self.x = 1225
self.vx = -8
self.y=EnemyPositionList[random.randint(0,len(EnemyPositionList)-1)]
self.t =0
def update(self):
self.x += self.vx
self.img = self.enemyAnim[(self.t // 2 )% 2]
self.t = self.t + 1
# big boy
class enemyW():
def __init__(self):
self.enemyAnimA = [GLib.enemy3, GLib.enemy32]
self.img = GLib.enemy3
self.x = 1225
self.vx= -1
self.y =EnemyPositionList[random.randint(0,len(EnemyPositionList)-1)]
self.t=0
def update(self):
self.x += self.vx
self.img = self.enemyAnimA[(self.t // 2)% 2]
self.t = self.t + 1
# drag boy
class enemyQ():
def __init__(self):
self.enemyAnimaT = [GLib.enemy4, GLib.enemy42]
self.img = GLib.enemy4
self.x = 1225
self.vx = -10
self.y = EnemyPositionList[random.randint(0,len(EnemyPositionList)-1)]
self.t = 0
def update(self):
self.x += self.vx
self.img = self.enemyAnimaT[(self.t // 2)% 2]
self.t = self.t + 1
class enemyK():
def __init__(self):
self.enemyAnim = [GLib.enemy5,GLib.enemy52]
self.img = GLib.enemy5
self.x = 1225
self.vx = -6
self.vy = 3
self.y=EnemyPositionList[random.randint(0,len(EnemyPositionList)-1)]
self.t =0
def update(self):
self.x += self.vx
self.y += self.vy
self.img = self.enemyAnim[(self.t // 2 )% 2]
self.t = self.t + 1
bounceIn(self,-100,50,1300,650)
class enemyT():
def __init__(self):
self.enemyAnim = [GLib.enemy6,GLib.enemy62]
self.img = GLib.enemy6
self.x = 1225
self.vx = -10
self.y=EnemyPositionList[random.randint(0,len(EnemyPositionList)-1)]
self.t =0
def update(self):
self.x += self.vx
self.img = self.enemyAnim[(self.t // 2 )% 2]
self.t = self.t + 1
#wtf another original basic boy??
class Bullet:
# self.sprite.x , self.sprite.y
def __init__(self, x, y):
self.x=x-3
self.y=y+19
self.img=GLib.bullet
def update(self):
self.x += 35
class Sprite:
def __init__(self):
# ------------------------
# [REQUIRED PART] for any class that will be drawn on the screen
# Grab the surface that Graphics people worked very hard on
self.img = GLib.character1
# Set the initial coordinate of this object
self.x = 100
self.y = 50
self.pastx = self.x
self.pasty = self.y
# ------------------------
# TODO: add more properties to Sprite based on your game
self.vx = 0
self.vy = 0
self.lives=10
self.bullets=20
# an example of updating position of the object
def update(self):
# TODO: what else Sprite is going to do in each frame
bounceIn(self, 0, 0, 1225, 700)
def MovementDetection(self):
CharacterAnimationL = [GLib.character1]
if self.x != self.pastx or self.y != self.pasty:
CharacterAnimationL= GLib.characterAnimationL
self.pastx = self.x
self.pasty = self.y
return CharacterAnimationL
# the minimum class for an object that can be displaced on the screen
class Game:
def __init__(self):
# initialize the timer to zero. This is like a little clock
self.timer = 0
self.reloadTimer=0
self.damageTimer=0
self.sprite = Sprite()
self.enemyList = []
self.enemy2List = []
self.enemy3List = []
self.enemy4List = []
self.enemy5List = []
self.bulletList=[]
self.score = Score()
self.ammo=Ammo()
self.damagetext = damagetaken()
self.LastBulletShot=0
self.Lives=10
self.EWlives=3
self.EQlives=2
# self.ball = Ball(250, 250, GLib.ballSpriteBLUE)
# TODO: add any variables you think will be needed as a property of Game
# ...
# ..
# .
# TODO: add any objects that you would like to be drawn on the screen
# Make sure that all of those objects has x, y and img defined as their property
#self.objectsOnScreen = [self.enemyList, self.bulletList, self.score, self.ammo, self.sprite]
#self.objectsOnScreen = [self.enemyList, self.bulletList, self.score, self.ammo, self.sprite]
self.objectsOnScreen = []
def shoot(self):
if self.reloadTimer < self.timer:
if self.timer-self.LastBulletShot>10 and self.sprite.bullets>0:
self.bulletList.append(Bullet(self.sprite.x , self.sprite.y ))
self.LastBulletShot=self.timer
self.sprite.bullets-=1
# Try to update all the elements
# if you want to add another to the screen: self.objectsOnScreen.add(x)
# if you want to remove a object from the screen: self.objectsOnScreen.remove(x)
# if you want to switch to another the state return 10 --> this switches to state 10
# if you want to bring a object to the front
# just remove it first and then added it back
# the last added object is going to be on the top
def updateInState(self, state):
# increment the timer
# check what state the game is at
if state == "Normal" or state == "Attack":
self.timer += 1
if self.timer > 0 and self.timer < 500:
if self.timer % 80 == 0:
e=enemy1()
self.enemyList.append(e)
if self.timer > 499 and self.timer < 1000:
if self.timer % 80 == 0:
e=enemy1()
self.enemyList.append(e)
if self.timer % 80 == 0:
e=enemy1()
self.enemyList.append(e)
if self.timer > 999 and self.timer < 1500:
if self.timer % 80 == 0:
e=enemy1()
self.enemyList.append(e)
if self.timer % 80 == 0:
e=enemyQ()
self.enemy3List.append(e)
if self.timer > 1499 and self.timer < 2000:
if self.timer % 80 == 0:
e=enemy1()
self.enemyList.append(e)
if self.timer % 80 == 0:
e=enemyQ()
self.enemy3List.append(e)
if self.timer % 80 == 0:
e=enemyW()
self.enemy2List.append(e)
if self.timer > 1999 and self.timer < 2500:
if self.timer % 80 == 0:
e=enemy1()
self.enemyList.append(e)
if self.timer % 80 == 0:
e=enemyK()
self.enemy5List.append(e)
if self.timer > 2499 and self.timer < 3000:
if self.timer % 110 == 0:
e=enemy1()
self.enemyList.append(e)
if self.timer % 110 == 0:
e=enemy1()
self.enemyList.append(e)
if self.timer % 110 == 0:
e=enemyW()
self.enemy2List.append(e)
if self.timer % 110 == 0:
e=enemyQ()
self.enemy3List.append(e)
if self.timer > 2999:
if self.timer % 150 == 0:
e=enemy1()
self.enemyList.append(e)
if self.timer % 150 == 0:
e=enemyW()
self.enemy2List.append(e)
if self.timer % 150 == 0:
e=enemyW()
self.enemy2List.append(e)
if self.timer % 150 == 0:
e=enemyQ()
self.enemy3List.append(e)
if self.timer % 150 == 0:
e=enemyK()
self.enemy5List.append(e)
for e in self.enemyList:
e.update()
if e.x<=-50:
if e in self.enemy2List:
self.enemy2List.remove(e)
if e in self.enemy3List:
self.enemy3List.remove(e)
self.enemyList.remove(e)
self.Lives -= 1
self.damageTimer=self.timer+10
if self.damageTimer > self.timer:
self.damagetext.update("Damage Taken!")
else:
self.damagetext.update(" ")
for e in self.enemy2List:
e.update()
if e.x<=-50:
if e in self.enemyList:
self.enemyList.remove(e)
if e in self.enemy3List:
self.enemy3List.remove(e)
self.enemy2List.remove(e)
self.Lives-=5
self.damageTimer=self.timer+10
if self.damageTimer > self.timer:
self.damagetext.update("Damage Taken!")
else:
self.damagetext.update(" ")
for e in self.enemy3List:
e.update()
if e.x<=-50:
if e in self.enemyList:
self.enemyList.remove(e)
if e in self.enemy2List:
self.enemy2List.remove(e)
self.enemy3List.remove(e)
self.Lives-=1
self.damageTimer=self.timer+10
if self.damageTimer > self.timer:
self.damagetext.update("Damage Taken!")
else:
self.damagetext.update(" ")
for e in self.enemy5List:
e.update()
if e.x<=-50:
self.Lives-=1
self.enemy5List.remove(e)
self.damageTimer=self.timer+10
if self.damageTimer > self.timer:
self.damagetext.update("Damage Taken!")
else:
self.damagetext.update(" ")
for i in self.bulletList:
i.update()
if i.x>=1200:
if i in self.bulletList:
self.bulletList.remove(i)
self.score.update(self.timer//SECSPERPOINT)
self.ammo.update(self.sprite.bullets)
if self.reloadTimer==self.timer:
self.sprite.bullets=20
for i in self.bulletList:
for e in self.enemyList:
if hasCollideRect(i, e):
if i in self.bulletList:
self.bulletList.remove(i)
if e in self.enemyList:
self.enemyList.remove(e)
break
for e in self.enemy2List:
if hasCollideRect(i, e):
if i in self.bulletList:
self.bulletList.remove(i)
self.EWlives-=1
if self.EWlives==0:
if e in self.enemy2List:
self.enemy2List.remove(e)
self.EWlives=3
break
for e in self.enemy3List:
if hasCollideRect(i, e):
if i in self.bulletList:
self.bulletList.remove(i)
self.EQlives-=1
if self.EQlives==0:
if e in self.enemy3List:
self.enemy3List.remove(e)
self.EQlives=2
break
for e in self.enemy5List:
if hasCollideRect(i, e):
if i in self.bulletList:
self.bulletList.remove(i)
if e in self.enemy5List:
self.enemy5List.remove(e)
break
for e in self.enemyList:
if hasCollideRect(self.sprite, e):
if e in self.enemyList:
self.enemyList.remove(e)
self.Lives -= 1
self.damageTimer=self.timer+10
if self.damageTimer > self.timer:
self.damagetext.update("Damage Taken!")
else:
self.damagetext.update(" ")
for e in self.enemy2List:
if hasCollideRect(self.sprite, e):
if e in self.enemy2List:
self.enemy2List.remove(e)
self.Lives -= 5
self.damageTimer=self.timer+10
if self.damageTimer > self.timer:
self.damagetext.update("Damage Taken!")
else:
self.damagetext.update(" ")
for e in self.enemy3List:
if hasCollideRect(self.sprite, e):
if e in self.enemy3List:
self.enemy3List.remove(e)
self.Lives -= 1
self.damageTimer=self.timer+10
if self.damageTimer > self.timer:
self.damagetext.update("Damage Taken!")
else:
self.damagetext.update(" ")
for e in self.enemy5List:
if hasCollideRect(self.sprite, e):
if e in self.enemy5List:
self.enemy5List.remove(e)
self.Lives -= 1
self.damageTimer=self.timer+10
if self.damageTimer > self.timer:
self.damagetext.update("Damage Taken!")
else:
self.damagetext.update(" ")
if self.Lives < 1:
self.objectsOnScreen =[]
return "Died"
if state == "Normal":
showAnimationOn(self.sprite, self.sprite.MovementDetection(), self.timer )
else:
showAnimationOn(self.sprite, GLib.ShootingSprite, self.timer/2)
self.sprite.update()
elif state == "Startscreen2":
pass
elif state == "Startscreen3":
pass
elif state == "Startscreen4":
pass
elif state == "Startscreen":
pass
elif state == "ControlScreen":
pass
elif state == "Died":
pass
else:
print("Undefined game state " + str(state))
exit()
# return the same state if you decided not to switch a state
return state
# A method that does all the drawing for you.
def draw(self,state, screen):
# The first line clear the screen
# TODO: if you want a differnt background,
# you can replace the next line
if state == "Normal":
screen.blit(GLib.Realbackground, (-25, -15))
screen.blit(GLib.Lives,(96.5,1))
myfont=pygame.font.SysFont('Calibri',20,bold=True)
img=myfont.render(str(self.Lives),1,GLib.WHITE)
screen.blit(img,(110,10))
elif state == "Startscreen2":
screen.blit(GLib.Startscreen2,(0,0))
elif state == "Startscreen3":
screen.blit(GLib.Startscreen3,(0,0))
elif state == "Startscreen4":
screen.blit(GLib.Startscreen4,(0,0))
elif state == "Startscreen":
screen.blit(GLib.Startscreen,(0,0))
elif state == "ControlScreen":
screen.blit(GLib.ControlScreen,(0,0))
if state == "Attack":
screen.blit(GLib.Realbackground,(-25,-15))
if state == "Died":
screen.blit(GLib.GameoverS,(0,0))
for obj in self.objectsOnScreen:
if type(obj) is list:
for i in obj:
screen.blit(i.img, (i.x, i.y))
else:
screen.blit(obj.img, (obj.x, obj.y))
if state == "StartScreen":
screen.blit(GLib.Startscreen,(0,0))