-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.update6.py
More file actions
587 lines (489 loc) · 18.9 KB
/
game.update6.py
File metadata and controls
587 lines (489 loc) · 18.9 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
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
import pygame
import sys
import os
'''
Game developed by:
- Adriano Lopes Martins - TIA: 31857647
- Luca Scattolin Manuguerra - TIA:31844405
Jogos Digitais - Sistemas de Informação - Turma: 04J
Universidade Presbiteriana Mackenzie
Python 3.7 / Pygame 1.9.6
'''
'''
OBJECTS
'''
class Player(pygame.sprite.Sprite):
'''
Spawn a player
'''
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.movex = 0
self.movey = 0
self.frame = 0
self.health = 100
self.images = []
self.score = 0
#Variables to set if the player is with some item or not
#catch 0=Nothing 1=Bottle 2=Can 3=Paper 4=Organic
self.catch = 0
for i in range(1,5):
img = pygame.image.load(os.path.join('images','hero' + str(i) + '.png')).convert()
img.convert_alpha() #optimise alpha
img.set_colorkey(ALPHA) #set alpha
self.images.append(img)
self.image = self.images[0]
self.rect = self.image.get_rect()
def control(self,x,y):
'''
control player movement
'''
self.movex += x
self.movey += y
def update(self):
'''
update sprite position
'''
# Collision with the trash
# Collision with can
if player.catch == 0:
hit_list_can = pygame.sprite.spritecollide(self, can_list, True)
for can in hit_list_can:
print('PEEEEEEEEEEEEEEGOUUUUUUUUUUU A CAN')
canpick.play()
player.catch = 2
print('player.catch = ', player.catch)
if player.catch == 2:
can_top = Trash(10,10,'can.bmp')
can_list.add(can_top)
else:
hit_list_can = pygame.sprite.spritecollide(self, can_list, False)
for can in hit_list_can:
world.blit(text_ocupada,(290,340))
# Collision with bottle
if player.catch == 0:
hit_list_bottle = pygame.sprite.spritecollide(self, bottle_list, True)
for bottle in hit_list_bottle:
print('PEEEEEEEEEEEEEEGOUUUUUUUUUUU O BOTTLE')
bottlepick.play()
player.catch = 1
if player.catch == 1:
bottle_top = Trash(10,10,'bottle.bmp')
bottle_list.add(bottle_top)
else:
hit_list_bottle = pygame.sprite.spritecollide(self, bottle_list, False)
for bottle in hit_list_bottle:
world.blit(text_ocupada,(290,340))
# Collision with paper
if player.catch == 0:
hit_list_paper = pygame.sprite.spritecollide(self, paper_list, True)
for paper in hit_list_paper:
print('PEEEEEEEEEEEEEEGOUUUUUUUUUUU O PAPER')
paperpick.play()
player.catch = 3
if player.catch == 3:
paper_top = Trash(10,10,'paper.bmp')
paper_list.add(paper_top)
else:
hit_list_paper = pygame.sprite.spritecollide(self, paper_list, False)
for paper in hit_list_paper:
world.blit(text_ocupada,(290,340))
# Collision with organic
if player.catch == 0:
hit_list_organic = pygame.sprite.spritecollide(self, organic_list, True)
for organic in hit_list_organic:
print('PEEEEEEEEEEEEEEGOUUUUUUUUUUU O ORGANIC')
print(player.movex)
applepick.play()
player.catch = 4
if player.catch == 4:
organic_top = Trash(10,10,'organic.bmp')
organic_list.add(organic_top)
else:
hit_list_organic = pygame.sprite.spritecollide(self, organic_list, False)
for organic in hit_list_organic:
world.blit(text_ocupada,(290,340))
# Collision with red trashcan
hit_list_trashred = pygame.sprite.spritecollide(self,trashred_list, False)
for trashred in hit_list_trashred:
if self.catch == 1:
print('ENCOSTOU NO LIXO COM O BOTTLE')
coin.play()
player.catch = 0
bottle_list.empty()
self.score += 1
elif self.catch > 1:
world.blit(text_errado, (300,340))
print('LIXO ERRADO')
else:
print('PEGUE UM LIXO')
world.blit(text_pegue, (305,340))
# Collision with yellow trashcan
hit_list_trashyellow = pygame.sprite.spritecollide(self,trashyellow_list, False)
for trashyellow in hit_list_trashyellow:
if self.catch == 2:
self.catch = 0
print('ENCOSTOU NO LIXO COM O CAN')
coin.play()
player.catch = 0
can_list.empty()
print('player.catch = ', player.catch)
self.score += 1
elif self.catch > 2 or self.catch == 1:
world.blit(text_errado, (300,340))
print('LIXO ERRADO')
else:
print('PEGUE UM LIXO')
world.blit(text_pegue, (305,340))
# Collision with blue trashcan
hit_list_trashblue = pygame.sprite.spritecollide(self,trashblue_list, False)
for trashblue in hit_list_trashblue:
if self.catch == 3:
self.catch = 0
print('ENCOSTOU NO LIXO COM O PAPER')
coin.play()
player.catch = 0
paper_list.empty()
print('player.catch = ', player.catch)
self.score += 1
elif self.catch > 3 or self.catch == 1 or self.catch == 2:
world.blit(text_errado, (300,340))
print('LIXO ERRADO')
else:
print('PEGUE UM LIXO')
world.blit(text_pegue, (305,340))
# Collision with grey trashcan
hit_list_trashgrey = pygame.sprite.spritecollide(self,trashgrey_list, False)
for trashgrey in hit_list_trashgrey:
if self.catch == 4:
self.catch = 0
print('ENCOSTOU NO LIXO COM O ORGANIC')
coin.play()
player.catch = 0
organic_list.empty()
print('player.catch = ', player.catch)
self.score += 1
print("score: ", self.score)
elif self.catch < 4 and self.catch > 0:
world.blit(text_errado, (300,340))
print('LIXO ERRADO')
else:
print('PEGUE UM LIXO')
world.blit(text_pegue, (305,340))
# Collision woth the enemies
hit_list = pygame.sprite.spritecollide(self, enemy_list, False)
for enemy in hit_list:
self.health -= 1
hit.play()
oof.play()
print(self.health)
self.rect.x = self.rect.x + self.movex
self.rect.y = self.rect.y + self.movey
# moving left
if self.movex < 0:
self.frame += 1
if self.frame > 3*ani:
self.frame = 1
self.image = self.images[self.frame//ani]
# moving right
if self.movex > 0:
self.frame += 1
if self.frame > 3*ani:
self.frame = 1
self.image = self.images[self.frame//ani]
# moving up
if self.movey < 0:
self.frame += 1
if self.frame > 3*ani:
self.frame = 1
self.image = self.images[self.frame//ani]
# moving down
if self.movey > 0:
self.frame += 1
if self.frame > 3*ani:
self.frame = 1
self.image = self.images[self.frame//ani]
# player dying
if player.rect.top < 340 or player.rect.top>610 or player.rect.right<0 or player.rect.left>800:
player.health = 0
if player.health <= 0:
self.score = 0
player.movex = 0
player.movey = 0
world.blit(text_lose, (300,320))
world.blit(text_spawn, (225,355))
#wining game
if self.score >= 4:
world.blit(text_won,(300,320))
world.blit(text_spawn, (225,355))
player.movex = 0
player.movey = 0
class Enemy(pygame.sprite.Sprite):
'''
Spawn an enemy
'''
def __init__(self,x,y):
ALPHA = (0,255,0)
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(os.path.join('images','crab.bmp'))
self.image.convert_alpha()
self.image.set_colorkey(ALPHA)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.counter = 0 # counter move variable
def move(self):
'''
enemy movement
'''
distance = 40
speed = 1
if self.counter >= 0 and self.counter <= distance:
self.rect.x = self.rect.x + 1
elif self.counter >= distance and self.counter <= distance*2:
self.rect.x = self.rect.x - 1
else:
self.counter = 1
self.counter += 1
class Trashcan(pygame.sprite.Sprite):
'''
spawn trash can
'''
def __init__(self,x,y,img):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(os.path.join('images',img))
self.image.set_colorkey(ALPHA)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
class Trash(pygame.sprite.Sprite):
'''
spawn trash can
'''
def __init__(self,x,y,img):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(os.path.join('images',img))
self.image.set_colorkey(ALPHA)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
class Balloon(pygame.sprite.Sprite):
'''
spawn text balloon
'''
def __init__(self,x,y,img):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(os.path.join('images',img))
self.image.set_colorkey(ALPHA)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
class Level():
def bad(lvl,eloc):
if lvl == 1:
enemy = Enemy(eloc[0],eloc[1]) # spawn enemy
enemy_list = pygame.sprite.Group() # create enemy group
enemy_list.add(enemy)
enemy_list.add(enemy2) # add enemy to group
if lvl == 2:
print("Level " + str(lvl) )
return enemy_list
'''
Setup
'''
ALPHA = (0,255,0) #chromakey color
worldx = 800
worldy = 600
fps = 45 # frame rate
ani = 5 # animation cycle
clock = pygame.time.Clock()
pygame.init()
main = True
world = pygame.display.set_mode([worldx,worldy])
backdrop = pygame.image.load(os.path.join('images','stage.png')).convert()
backdropbox = world.get_rect()
inventory_x=900
inventory_y=900
inventory = pygame.image.load(os.path.join('images','bottle.bmp')).convert()
#spawning player
player = Player() # spawn player
player.rect.x = 70 # set player position x
player.rect.y = 490 # set player position y
player_list = pygame.sprite.Group() #add player to a sprite group
player_list.add(player)
steps = 2 #how many pixels the player move
#spawning enemies
enemy = Enemy(250,500)# spawn enemy
enemy2 = Enemy(500,450)# spawn enemy
enemy_list = pygame.sprite.Group() # create enemy group
enemy_list.add(enemy) # add enemy to group
#spawning trash cans
trashred = Trashcan(10,380,'lixo_red.bmp')
trashred_list = pygame.sprite.Group()
trashred_list.add(trashred)
trashyellow = Trashcan(10,435,'lixo_yellow.bmp')
trashyellow_list = pygame.sprite.Group()
trashyellow_list.add(trashyellow)
trashblue = Trashcan(10,490,'lixo_blue.bmp')
trashblue_list = pygame.sprite.Group()
trashblue_list.add(trashblue)
trashgrey = Trashcan(10,545,'lixo_grey.bmp')
trashgrey_list = pygame.sprite.Group()
trashgrey_list.add(trashgrey)
#spawning trashes
bottle = Trash(350,470,'bottle.bmp')
bottle_list = pygame.sprite.Group()
bottle_list.add(bottle)
can = Trash(600,510,'can.bmp')
can_list = pygame.sprite.Group()
can_list.add(can)
paper = Trash(650,450,'paper.bmp')
paper_list = pygame.sprite.Group()
paper_list.add(paper)
organic = Trash(190,550,'organic.bmp')
organic_list = pygame.sprite.Group()
organic_list.add(organic)
#spawning text balloons
balloon1 = Balloon(125,35,'balloon1.jpg')
balloon2 = Balloon(125,35,'balloon2.jpg')
menu1 = Balloon(0,0,'menu2.0.jpg')
menu_list = pygame.sprite.Group()
menu_list.add(menu1)
balloon_list1 = pygame.sprite.Group()
balloon_list1.add(balloon1)
balloon_list2 = pygame.sprite.Group()
balloon_list2.add(balloon2)
#spawning scoreboard
scoreboard = Trash(732,532,'scoreboard.jpg')
scoreboard_list = pygame.sprite.Group()
scoreboard_list.add(scoreboard)
#inicializando fontes
pygame.font.init()
font_default = pygame.font.get_default_font() #variable to store system default font
font_snap = pygame.font.SysFont('snapitc', 45)
font_lose = pygame.font.SysFont(font_default, 45)
text_won = font_lose.render('Você Ganhou', 1, (255, 255, 255))
text_lose = font_lose.render('Você Perdeu', 1, (255, 255, 255))
text_spawn = font_lose.render('Clique para recomeçar', 1, (255, 255, 255))
text_errado = font_lose.render('Lixeira Errada', 1,(255,255,255))
text_pegue = font_lose.render('Pegue um Lixo',1,(255,255,255))
text_ocupada = font_lose.render('Mãos ocupadas',1,(255,255,255))
#COLORS
BLUE = (25,25,200)
BLACK = (23,23,23 )
WHITE = (254,254,254)
eloc = []
eloc = [220,500]
enemy_list = Level.bad( 1, eloc )
#AUDIOS
bgsong = pygame.mixer.Sound('bgsong.ogg')
hit = pygame.mixer.Sound('hit.ogg')
oof = pygame.mixer.Sound('oof.ogg')
coin = pygame.mixer.Sound('coin.ogg')
paperpick = pygame.mixer.Sound('paperpick.ogg')
applepick = pygame.mixer.Sound('applepick.ogg')
canpick = pygame.mixer.Sound('canpick.ogg')
bottlepick = pygame.mixer.Sound('bottlepick.ogg')
#BACKGROUND MUSIC START
bgsong.play()
welcome = 1
welcome2 = 0
welcome3 = 0
'''
Game-Loop
'''
while main == True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit(); sys.exit()
main = False
if event.type == pygame.KEYDOWN:
if event.key == ord('q'):
pygame.quit()
sys.exit()
main = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT or event.key == ord('a'):
print('left')
player.control(-steps,0)
if event.key == pygame.K_RIGHT or event.key == ord('d'):
print('right')
player.control(steps,0)
if event.key == pygame.K_UP or event.key == ord('w'):
print('up')
player.control(0,-steps)
if event.key == pygame.K_DOWN or event.key == ord('s'):
print('down')
player.control(0,steps)
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == ord('a'):
print('left_stop')
player.control(steps,0)
if event.key == pygame.K_RIGHT or event.key == ord('d'):
print('right_stop')
player.control(-steps,0)
if event.key == pygame.K_UP or event.key == ord('w'):
print('up_stop')
player.control(0,steps)
if event.key == pygame.K_DOWN or event.key == ord('s'):
print('down_stop')
player.control(0,-steps)
if player.health <= 0:
world.blit(text_lose, (215,190))
while player.health <= 0 or player.score >= 4: #player dying or wining
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
print('mouse button down')
player.catch = 0
can_list.empty() #limpando inventário
bottle_list.empty()
paper_list.empty()
organic_list.empty()
player.rect.x = 70 #restore player position
player.rect.y = 490
player.health = 100 #restore health'''
player.score = 0 #restore score
bottle = Trash(350,470,'bottle.bmp')
bottle_list = pygame.sprite.Group()
bottle_list.add(bottle)
can = Trash(600,510,'can.bmp')
can_list = pygame.sprite.Group()
can_list.add(can)
paper = Trash(650,450,'paper.bmp')
paper_list = pygame.sprite.Group()
paper_list.add(paper)
organic = Trash(190,550,'organic.bmp')
organic_list = pygame.sprite.Group()
organic_list.add(organic)
if welcome == 1:
if event.type == pygame.MOUSEBUTTONDOWN:
print('BUTTON DOWN')
balloon_list1.empty()
elif event.type == pygame.MOUSEBUTTONUP:
print('BUTTONUP')
welcome = 2
elif welcome == 2:
if event.type == pygame.MOUSEBUTTONDOWN:
balloon_list2.empty()
world.blit(backdrop, backdropbox) #bliting the background in the game
trashred_list.draw(world) #draw red trashcan
trashyellow_list.draw(world) #draw blue trash can
trashblue_list.draw(world) #draw blue trashcan
trashgrey_list.draw(world) #draw grey trash can
bottle_list.draw(world) #draw bottles
can_list.draw(world) #draw cans
paper_list.draw(world) #draw paper
organic_list.draw(world) #draw organic waste
enemy_list.draw(world) #draw enemies
for e in enemy_list:
e.move()
player.update() #update player position
player_list.draw(world) #draw player
balloon_list2.draw(world) #draw the welcoming balloon
balloon_list1.draw(world)
#menu_list.draw(world)
scoreboard_list.draw(world)
text_score = font_snap.render(str(player.score), 1, (255, 255, 255))
world.blit(text_score, (752,540))
pygame.display.flip() #refreshing the game
clock.tick(fps) #advance the game's clock