-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
601 lines (446 loc) · 18.5 KB
/
main.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
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
588
589
590
591
592
593
594
595
596
597
598
599
600
import pygame
import os
import random
from pygame import mixer
def main():
pygame.init()
SCREEN_WIDTH = 1000
SCREEN_HEIGHT = 600
win = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
game_speed = 20
# Sons utilizados
pygame.mixer.music.load("Assets/sounds/pixel_king_hall.mp3")
pygame.mixer.music.play(-1)
pygame.mixer.music.set_volume(0.15)
death_sound = pygame.mixer.Sound("Assets/sounds/death_sound.mp3")
pygame.mixer.Sound.set_volume(death_sound, 0.15)
collide_Sound = mixer.Sound('Assets/sounds/collision.wav')
pygame.mixer.Sound.set_volume(collide_Sound, 0.25)
collect_Sound = pygame.mixer.Sound('Assets/sounds/collect.wav')
pygame.mixer.Sound.set_volume(collect_Sound, 0.9)
bullet_Sound = mixer.Sound('Assets/sounds/laser1.wav')
pygame.mixer.Sound.set_volume(bullet_Sound, 0.25)
destruction_sound = mixer.Sound('Assets/sounds/explosion sound.wav')
pygame.mixer.Sound.set_volume(destruction_sound, 0.2)
# Imagens que serão utilizadas
run_animation = []
jump_animation = []
game_over_bg = pygame.image.load(os.path.join("Assets/gameover", "bg_menu4.png"))
background_image = pygame.image.load(os.path.join("Assets/background", "rua_da_aurora.png"))
background_image = pygame.transform.scale(background_image, (1000, 600)).convert()
projectile_image = pygame.image.load(os.path.join("Assets/projectile", "Aceito.png"))
obstacle_images = [pygame.image.load(os.path.join("Assets/barrier", "pixel_wrong.png")),
pygame.image.load(os.path.join("Assets/barrier", "pixel_runtime-error.png"))]
img_coins = [pygame.image.load(os.path.join("Assets/collectables", "pixel 0.png")),
pygame.image.load(os.path.join("Assets/collectables", "pixel 1.png"))]
diff_images = [pygame.image.load(os.path.join("Assets/diff", "easy.png")),
pygame.image.load(os.path.join("Assets/diff", "medium.png")), pygame.image.load(os.path.join("Assets/diff", "hard.png"))]
heart_image = pygame.image.load(os.path.join("Assets/collectables", "heart pixel art 64x64.png"))
slow_item = pygame.image.load(os.path.join("Assets/collectables", "slow passion fruit.png"))
up, down = pygame.image.load(os.path.join("Assets/enemy", "dikastis_up_pixel.png")), pygame.image.load(
os.path.join("Assets/enemy", "dikastis_down_pixel.png"))
up, down = pygame.transform.scale(up, (80, 130)), pygame.transform.scale(down, (100, 130))
dikastis_animation = [up, down]
destruction_animation = []
for i in os.listdir("Assets/destroy"):
destro = pygame.image.load(os.path.join("Assets/destroy", f"{i}"))
destro_scaled = pygame.transform.scale(destro, (80, 130))
destruction_animation.append(destro_scaled)
for i in os.listdir("Assets/player/run"):
run = pygame.image.load(os.path.join("Assets/player/run", f"{i}"))
run_scaled = pygame.transform.scale(run, (100, 125))
run_animation.append(run_scaled)
for i in os.listdir("Assets/player/jump"):
jump = pygame.image.load(os.path.join("Assets/player/jump", f"{i}"))
jump_scaled = pygame.transform.scale(jump, (100, 125))
jump_animation.append(jump_scaled)
class BG(object):
def __init__(self):
self.x = 0
self.y = 0
self.bg_img = background_image
self.bg_width = self.bg_img.get_width()
def update(self):
if self.x <= -self.bg_width:
self.x = 0
self.x -= game_speed
def draw(self, SCREEN):
SCREEN.blit(self.bg_img, (self.x, self.y))
SCREEN.blit(self.bg_img, (self.bg_width + self.x, self.y))
class player(object):
def __init__(self, y):
self.x = 50
self.y = y
self.run_img = run_animation
self.jump_img = jump_animation
self.stepindex = 1
self.isJump = False
self.isrun = True
self.stop = 0
self.image = self.run_img[0]
self.boy_rect = self.image.get_rect()
self.boy_rect.x = self.x
self.boy_rect.y = self.y
# Atualiza as informações do player a cada loop
def update(self, userInput):
if self.isrun:
self.run()
if self.isJump:
self.jump()
if self.isJump:
self.isrun = False
# Animação de corrida
def run(self):
self.image = self.run_img[self.stepindex // 2]
self.boy_rect.x = self.x
self.boy_rect.y = self.y
if self.stepindex < len(run_animation):
self.stepindex += 1
else:
self.stepindex = 0
# Animação do pulo
def jump(self):
self.image = self.jump_img[self.stepindex // 2]
self.boy_rect.x = self.x
self.boy_rect.y = self.y
if self.stepindex < len(jump_animation):
self.stepindex += 1
else:
self.stepindex = 0
def draw(self, SCREEN):
SCREEN.blit(self.image, (self.boy_rect.x, self.boy_rect.y))
class projectile(object):
def __init__(self, x, y):
self.x = x
self.y = y
self.vel = 40
self.image = projectile_image
self.proj_rect = self.image.get_rect()
self.proj_rect.x = self.x
self.proj_rect.y = self.y
def draw(self, SCREEN):
SCREEN.blit(self.image, (self.proj_rect.x, self.proj_rect.y))
# Super-classe dos obstáculos
class obstacle:
def __init__(self, image, type, category):
self.image = image
self.type = type
self.category = category
self.rect = self.image[self.type].get_rect()
self.rect.x = 1000
def update(self):
self.rect.x -= game_speed
if self.rect.x < -self.rect.width:
obstacles.pop()
def draw(self, SCREEN):
SCREEN.blit(self.image[self.type], (self.rect.x, self.rect.y))
class unacceptable(obstacle):
def __init__(self, image):
self.type = random.randint(0, 1)
self.category = "obstacle"
super().__init__(image, self.type, self.category)
self.rect.y = 445
class dikastis(obstacle):
def __init__(self, image):
self.type = 0
self.category = "enemy"
super().__init__(image, self.type, self.category)
self.rect.y = random.randint(220, 340)
self.index = 0
def draw(self, SCREEN):
SCREEN.blit(self.image[self.index // 2], self.rect)
if self.index > 1:
self.index = 0
self.index += 1
#classe das explosões
class explosion:
def __init__(self, x, y):
#lista das imagens das explosões
self.images = []
for i in range(71):
if i <= 9:
img = pygame.image.load(os.path.join("Assets/destroy", f"frame000{i}.png"))
else:
img = pygame.image.load(os.path.join("Assets/destroy", f"frame00{i}.png"))
pygame.display.set_icon(img)
img = pygame.transform.scale(img, (200,200))
#add todas as imagens da pasta na lista
self.images.append(img)
self.index = 0
self.image = self.images[self.index]
self.rect = self.image.get_rect()
self.rect.center = [x, y]
self.counter = 0
def update(self, lista):
explosion_speed = 0.001
self.counter += 1
if self.counter >= explosion_speed and self.index < len(self.images) - 1:
self.counter = 0
self.index += 1
self.image = self.images[self.index]
self.rect.x -= game_speed
#limpar a lista das explosões e não pesar no código
if self.index >= 70:
lista.clear()
def draw(self, SCREEN):
SCREEN.blit(self.image, (self.rect.x, self.rect.y))
# Super-classe para os coletáveis
class collecty:
def __init__(self, image, type, category):
self.image = image
self.type = type
self.category = category
#se o obj for apenas uma imagem self.type recebe None
if self.type == None:
self.rect = self.image.get_rect()
#se o obj for uma lista de imagens, self.type receberá um valor que representa o index da lista
else:
self.rect = self.image[self.type].get_rect()
self.rect.x = 1000
#coloquei um parâmetro lista pois inicialmente a função se baseava apenas na lista collectable
def update(self, lista):
self.rect.x -= game_speed
if self.rect.x < -self.rect.width:
try:
lista.pop()
except:
None
def draw(self, SCREEN):
if self.type == None:
SCREEN.blit(self.image, (self.rect.x, self.rect.y))
else:
SCREEN.blit(self.image[self.type], (self.rect.x, self.rect.y))
# Classe dos 0's e 1's
class coin(collecty):
def __init__(self, image):
self.type = random.randint(0, 1)
self.category = "coin"
super().__init__(image, self.type, self.category)
self.rect.y = random.randint(230, 350)
#classe dos corações s
class life_heart(collecty):
def __init__(self, image):
self.type = None
self.category = "heart"
super().__init__(image, self.type, self.category)
self.rect.y = obstacle.rect.y
self.rect.x = obstacle.rect.x + 30
#classe do item que desacelera o jogo (é um maracujá)
class slow_item_passion(collecty):
def __init__(self, image):
self.type = None
self.category = "maracuja"
super().__init__(image, self.type, self.category)
self.rect.y = obstacle.rect.y
self.rect.x = obstacle.rect.x + 30
jumpcount = 10
pygame.display.set_caption("Boy do Bit")
font = pygame.font.Font('Assets/font/zorque.otf', 26)
# Criando objeto boy --> (player)
boy = player(400)
background = BG()
bullets = []
obstacles = []
collectable = []
hearts = []
passion_fruit = []
explosion_effect = []
wait_animation_player = 0
wait_animation_enemy = 0
time = 0
lives = 3
zeros = 0
ones = 0
run = True
while run:
# Desenha o background
background.draw(win)
background.update()
pygame.time.delay(50)
time += 1
# Aumenta a dificuldade (velocidade dos objetos) conforme o tempo passa
if time % 50 == 0:
game_speed += 0.6
# Condições de dificuldade (exibir na tela easy, medium ou hard feito o Dikastis)
if game_speed < 25:
win.blit(diff_images[0], (818,70))
elif game_speed < 35:
win.blit(diff_images[1], (818,70))
else:
win.blit(diff_images[2], (818,70))
# Exclui o check quando ele sai da visão
for bullet in bullets:
if bullet.x < 1000 and bullet.x > 0:
bullet.x += bullet.vel
bullet.proj_rect.x += bullet.vel
else:
bullets.pop(bullets.index(bullet))
# Fecha o jogo quando o player clica no X
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
userInput = pygame.key.get_pressed()
# Atira um check quando o player aperta ESPAÇO
if userInput[pygame.K_SPACE]:
if len(bullets) < 1:
bullets.append(projectile(165, boy.y + 55))
bullet_Sound.play()
# Pula quando o player aperta W
if not (boy.isJump):
if userInput[pygame.K_w]:
boy.isJump = True
else:
if jumpcount >= -10:
neg = 1
if jumpcount < 0:
neg = -1
boy.y -= 0.40 * jumpcount ** 2 * neg
jumpcount -= 1
else:
boy.isJump = False
boy.isrun = True
jumpcount = 10
boy.draw(win)
boy.update(userInput)
for bullet in bullets:
bullet.draw(win)
# Cria um obstáculo aleatório
if len(obstacles) == 0:
if random.randint(0, 1) == 0:
obstacles.append(unacceptable(obstacle_images))
else:
obstacles.append(dikastis(dikastis_animation))
for obstacle in obstacles:
obstacle.draw(win)
obstacle.update()
# O player perde uma vida quando bate em algum obstáculo
if boy.boy_rect.colliderect(obstacle.rect):
obstacles.pop(obstacles.index(obstacle))
collide_Sound.play()
lives -= 1
# Destrói um Dikastis se o player o atingir com um check
for bullet in bullets:
if bullet.proj_rect.colliderect(obstacle.rect) and obstacle.category == "enemy":
# Quando um Dikastis é destruído, existe 5% de chance de surgir um coração em seu lugar
if random.randint(1, 100) <= 5:
hearts.append(life_heart(heart_image))
#Pode ter a chance de aparecer uma fruta que diminui velocidade
elif 5 < random.randint(1, 100) <= 15:
passion_fruit.append(slow_item_passion(slow_item))
try:
obstacles.pop(obstacles.index(obstacle))
destruction_sound.play()
explosion_effect.append(explosion(obstacle.rect.x + 60, obstacle.rect.y + 60))
except:
None
bullets.pop(bullets.index(bullet))
bullet.draw(win)
obstacle.draw(win)
# Cria 0's e 1's
if len(collectable) == 0:
collectable.append(coin(img_coins))
# Aumenta a pontuação conforme o usuário coleta 0's e 1's
for collect in collectable:
collect.draw(win)
collect.update(collectable)
if boy.boy_rect.colliderect(collect.rect):
collect_Sound.play()
#collect_Sound.play()
if collect.type == 0:
zeros += 1
if collect.type == 1:
ones += 1
collectable.pop(collectable.index(collect))
# Ganha +1 vida ao colidir com um coração
for heart in hearts:
heart.draw(win)
heart.update(hearts)
if boy.boy_rect.colliderect(heart.rect):
collect_Sound.play()
lives += 1
if lives > 3:
collect_Sound.play()
lives = 3
hearts.pop(hearts.index(heart))
#se pegar um maracujá (slow passion fruit) a velocidade do jogo diminui
for maracuja in passion_fruit:
maracuja.draw(win)
maracuja.update(passion_fruit)
if boy.boy_rect.colliderect(maracuja.rect):
collect_Sound.play()
game_speed -= 1.2
passion_fruit.pop(passion_fruit.index(maracuja))
#cria as explosões
for explo_effect in explosion_effect:
explo_effect.draw(win)
explo_effect.update(explosion_effect)
pygame.draw.rect(win, (0,0,0), pygame.Rect(20, 40, 150, 100), 0, 3)
# Mostra na tela os status de vidas e bits coletados
text0 = font.render('Lives: ' + str(lives), True, (255, 255, 255))
text1 = font.render('0: ' + str(zeros), True, (255, 255, 255))
text2 = font.render('1: ' + str(ones), True, (255, 255, 255))
win.blit(text0, (50, 50))
win.blit(text1, (50, 80))
win.blit(text2, (50, 100))
pygame.display.update()
if lives == 0:
death_sound.play()
Game_Over( game_over_bg, zeros, ones)
break
pygame.quit()
def Game_Over(bg, zeros=0, ones=0):
pygame.init()
pygame.mixer.music.stop()
win = pygame.display.set_mode((1000, 600))
pygame.display.set_caption("Boy do Bit")
font = pygame.font.Font('Assets/font/zorque.otf', 20)
run = True
while run:
pygame.time.delay(50)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
userInput = pygame.key.get_pressed()
if userInput[pygame.K_SPACE]:
main()
break
win.fill((255, 255, 255))
#background do Game Over
win.blit(bg, (0,0))
# Mostra o menu de fim de jogo
text0 = font.render('GAME OVER', True, (255, 255, 255))
text1 = font.render("press 'space' to restart", True, (0, 0, 0))
text_zeros = font.render("0's collected:" + str(zeros), True, (255, 255, 255))
text_ones = font.render("1's collected:" + str(ones), True, (255, 255, 255))
text0_rect = text0.get_rect(center=(500, 200))
text1_rect = text1.get_rect(center=(500, 240))
text_zeros_rect = text_zeros.get_rect(center=(500, 300))
text_ones_rect = text_ones.get_rect(center=(500, 340))
pygame.draw.rect(win,(201,34,46), pygame.Rect(360, 140, 290, 270), 0, 3)
win.blit(text0, text0_rect)
win.blit(text1, text1_rect)
win.blit(text_zeros, text_zeros_rect)
win.blit(text_ones, text_ones_rect)
pygame.display.update()
pygame.quit()
def menu():
pygame.init()
win = pygame.display.set_mode((1000, 600))
bg_menu = pygame.image.load(os.path.join("Assets/menu", "bg_intro.png"))
pygame.display.set_caption("Boy do Bit")
run = True
while run:
pygame.time.delay(50)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
userInput = pygame.key.get_pressed()
if userInput[pygame.K_SPACE]:
main()
break
win.fill((255, 255, 255))
# background da tela inicial
win.blit(bg_menu, (0, 0))
pygame.display.update()
pygame.quit()
menu()