diff --git a/Testes/TESTE_2.py b/Exemplos/Testes/TESTE_2.py similarity index 100% rename from Testes/TESTE_2.py rename to Exemplos/Testes/TESTE_2.py diff --git a/Testes/TESTE_3.py b/Exemplos/Testes/TESTE_3.py similarity index 100% rename from Testes/TESTE_3.py rename to Exemplos/Testes/TESTE_3.py diff --git a/Testes/TESTE_4.py b/Exemplos/Testes/TESTE_4.py similarity index 100% rename from Testes/TESTE_4.py rename to Exemplos/Testes/TESTE_4.py diff --git a/Testes/spritesheet_boom.py b/Exemplos/Testes/spritesheet_boom.py similarity index 100% rename from Testes/spritesheet_boom.py rename to Exemplos/Testes/spritesheet_boom.py diff --git a/Exemplos/pygame-snippets-master/moving_background.py b/Exemplos/pygame-snippets-master/moving_background.py index 7efd92e..b1c867a 100644 --- a/Exemplos/pygame-snippets-master/moving_background.py +++ b/Exemplos/pygame-snippets-master/moving_background.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- + # -*- coding: utf-8 -*- # Importando as bibliotecas necessárias. import pygame diff --git a/UM BALAUM/__pycache__/data.cpython-38.pyc b/UM BALAUM/__pycache__/data.cpython-38.pyc index c844d63..424d537 100644 Binary files a/UM BALAUM/__pycache__/data.cpython-38.pyc and b/UM BALAUM/__pycache__/data.cpython-38.pyc differ diff --git a/UM BALAUM/__pycache__/final_screen.cpython-38.pyc b/UM BALAUM/__pycache__/final_screen.cpython-38.pyc index 97f0b4e..3935173 100644 Binary files a/UM BALAUM/__pycache__/final_screen.cpython-38.pyc and b/UM BALAUM/__pycache__/final_screen.cpython-38.pyc differ diff --git a/UM BALAUM/__pycache__/game_screen.cpython-38.pyc b/UM BALAUM/__pycache__/game_screen.cpython-38.pyc index 8a42c8e..3369a16 100644 Binary files a/UM BALAUM/__pycache__/game_screen.cpython-38.pyc and b/UM BALAUM/__pycache__/game_screen.cpython-38.pyc differ diff --git a/UM BALAUM/__pycache__/init_screen.cpython-38.pyc b/UM BALAUM/__pycache__/init_screen.cpython-38.pyc index f97dcfb..f78c34f 100644 Binary files a/UM BALAUM/__pycache__/init_screen.cpython-38.pyc and b/UM BALAUM/__pycache__/init_screen.cpython-38.pyc differ diff --git a/UM BALAUM/__pycache__/reference.cpython-38.pyc b/UM BALAUM/__pycache__/reference.cpython-38.pyc index 01bbde7..8471c7c 100644 Binary files a/UM BALAUM/__pycache__/reference.cpython-38.pyc and b/UM BALAUM/__pycache__/reference.cpython-38.pyc differ diff --git a/UM BALAUM/__pycache__/sprites.cpython-38.pyc b/UM BALAUM/__pycache__/sprites.cpython-38.pyc index ef02acd..13c7b80 100644 Binary files a/UM BALAUM/__pycache__/sprites.cpython-38.pyc and b/UM BALAUM/__pycache__/sprites.cpython-38.pyc differ diff --git a/UM BALAUM/__pycache__/start_screen.cpython-38.pyc b/UM BALAUM/__pycache__/start_screen.cpython-38.pyc index b574e0d..4f89557 100644 Binary files a/UM BALAUM/__pycache__/start_screen.cpython-38.pyc and b/UM BALAUM/__pycache__/start_screen.cpython-38.pyc differ diff --git a/UM BALAUM/coded.py b/UM BALAUM/coded.py index 4cde2d7..2676abd 100644 --- a/UM BALAUM/coded.py +++ b/UM BALAUM/coded.py @@ -10,7 +10,7 @@ pygame.init() pygame.mixer.init() - + # ----- Gera tela principal window = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption('UM BALAUM') diff --git a/UM BALAUM/game_screen.py b/UM BALAUM/game_screen.py index b1784e6..c6ee422 100644 --- a/UM BALAUM/game_screen.py +++ b/UM BALAUM/game_screen.py @@ -1,10 +1,27 @@ # --- Importações import pygame import time -from reference import load_assets, IMAGE, MUSIC, LIFE_FONT, SCORE_FONT, LIVES_FONT, EAGLE_SOUND, BOOM_SOUND, PLAYER_SHEET, BACOV_SOUND, GECOV_SOUND, LEVEL_UP_SOUND -from sprites import Covid, Balao, Life, Eagle1, Eagle2, Gel, load_spritesheet, Player +from random import * +from reference import load_assets, COVID_IMG, BALLOON_IMG, GEL_IMG, EAGLE1_IMG, EAGLE2_IMG, POP_SOUND, IMAGE, MUSIC, LIFE_FONT, SCORE_FONT, LIVES_FONT, EAGLE_SOUND, BOOM_SOUND, PLAYER_SHEET, BACOV_SOUND, GECOV_SOUND, LEVEL_UP_SOUND +from sprites import Covid, Balao, Life, EagleLeft, EagleRight, Gel, load_spritesheet, Player from data import WIDTH, HEIGHT, FPS, OVER, GAME, PASS, WIN +# assets = load_assets() +# #Cria função para inicializar musica +def toca_som(nome_do_som,boolean): + assets = load_assets() + som = assets[nome_do_som] + if boolean == True: + som.play(loops=-1) + else: + som.play() + +# #Cria função para pausar música +def pausa_som(nome_do_som): + assets = load_assets() + som = assets[nome_do_som] + som.stop() + # --- Cria um grupo de sprites geral e para cada obstáculo def game_screen(window, fase): @@ -31,14 +48,14 @@ def game_screen(window, fase): keys_down = {} # Evita possíveis bugs de teclas pressionadas # --- Cria o jogador (balão) - balao = Balao(groups, assets, init_balife) + balao = Balao(assets, BALLOON_IMG, groups, init_balife) all_sprites.add(balao) - balloon_life = Life(balao, assets) + balloon_life = Life(assets,LIFE_FONT,balao) all_sprites.add(balloon_life) # --- Cria as covides - covid = Covid(assets) + covid = Covid(assets,COVID_IMG) all_sprites.add(covid) covides.add(covid) @@ -52,8 +69,8 @@ def game_screen(window, fase): # --- Cria as águias e suas quantidades for i in range(eagles_qtd): - eagle1 = Eagle1(assets) - eagle2 = Eagle2(assets) + eagle1 = EagleLeft(assets,EAGLE1_IMG) + eagle2 = EagleRight(assets,EAGLE2_IMG) all_sprites.add(eagle1) all_sprites.add(eagle2) aguias.add(eagle1) @@ -68,7 +85,7 @@ def game_screen(window, fase): state = PLAYING # ===== LOOP PRRINCIPAL ===== - assets[MUSIC].play(loops=-1) + toca_som(MUSIC, True) while state != DONE: clock.tick(FPS) @@ -90,7 +107,7 @@ def game_screen(window, fase): if event.key == pygame.K_DOWN: balao.speedy += 3.5 if event.key == pygame.K_SPACE: # Atira álcool em gel - balao.shoot() + balao.shoot(assets,GEL_IMG) # Verifica se soltou alguma tecla if event.type == pygame.KEYUP: # Dependendo da tecla, altera a velocidade if event.key in keys_down and keys_down[event.key]: @@ -114,11 +131,11 @@ def game_screen(window, fase): balao.lives -= 1 score -= 10 if aguia == eagle1: - eagle1 = Eagle1(assets) + eagle1 = EagleLeft(assets,EAGLE1_IMG) all_sprites.add(eagle1) aguias.add(eagle1) elif aguia == eagle2: - eagle2 = Eagle2(assets) + eagle2 = EagleRight(assets,EAGLE2_IMG) all_sprites.add(eagle2) aguias.add(eagle2) @@ -128,7 +145,7 @@ def game_screen(window, fase): lives -= 1 balloon_life.kill() balao.kill() - assets[BOOM_SOUND].play() + toca_som(BOOM_SOUND, False) player = Player(balao.rect.center, assets['player_sheet']) all_sprites.add(player) state = EXPLODING @@ -138,8 +155,8 @@ def game_screen(window, fase): col_2 = pygame.sprite.spritecollide(balao, covides, True, pygame.sprite.collide_mask) for covid in col_2: - assets[BACOV_SOUND].play() - covid = Covid(assets) + toca_som(BACOV_SOUND, False) + covid = Covid(assets,COVID_IMG) all_sprites.add(covid) covides.add(covid) balao.lives -= 2 @@ -148,11 +165,11 @@ def game_screen(window, fase): col_3 = pygame.sprite.spritecollide(covid, gels, True, pygame.sprite.collide_mask) for gel in col_3: covid_lives -= 1 - assets[GECOV_SOUND].play() + toca_som(GECOV_SOUND, False) if covid_lives == 0: covid.kill() gel.kill() - covid = Covid(assets) + covid = Covid(assets,COVID_IMG) all_sprites.add(covid) covides.add(covid) covid_lives = 1 @@ -160,8 +177,8 @@ def game_screen(window, fase): score += 200 if score >= 1500: - assets[MUSIC].stop() - assets[LEVEL_UP_SOUND].play() + pausa_som(MUSIC) + toca_som(LEVEL_UP_SOUND, False) time.sleep(1.5) if fase < 3: state = PASS @@ -170,7 +187,7 @@ def game_screen(window, fase): return state elif state == EXPLODING: - assets[MUSIC].stop() + pausa_som(MUSIC) now = pygame.time.get_ticks() if now - explosion_tick > explosion_duration: if lives == 0: @@ -178,10 +195,10 @@ def game_screen(window, fase): return state else: state = PLAYING - assets[MUSIC].play() - balao = Balao(groups, assets, init_balife) + toca_som(MUSIC, False) + balao = Balao(assets, BALLOON_IMG, groups, init_balife) all_sprites.add(balao) - balloon_life = Life(balao, assets) + balloon_life = Life(assets,LIFE_FONT,balao) all_sprites.add(balloon_life) # --- Saídas @@ -217,5 +234,4 @@ def game_screen(window, fase): text_rect.bottomleft = (10, HEIGHT - 10) window.blit(text_surface, text_rect) - pygame.display.update() # Mostra o novo frame para o jogador - + pygame.display.update() # Mostra o novo frame para o jogador \ No newline at end of file diff --git a/UM BALAUM/sprites.py b/UM BALAUM/sprites.py index 3625303..545b2c2 100644 --- a/UM BALAUM/sprites.py +++ b/UM BALAUM/sprites.py @@ -2,23 +2,77 @@ from random import * from reference import COVID_IMG, BALLOON_IMG, GEL_IMG, EAGLE1_IMG, EAGLE2_IMG, POP_SOUND from data import WIDTH, HEIGHT, STILL +from abc import abstractmethod -# --- Cria a classe do balão, que será movimentado pelo jogador -class Balao(pygame.sprite.Sprite): - """ Seta o balão""" - def __init__(self,groups,assets,lives): - """Recebe e define dados iniciais do balão""" +# Superclasse abstrata que contém alguns dados em comum para a maioria das classes +class ConstrutorComum(pygame.sprite.Sprite): + """Faz os sprites e converte em máscaras""" + @abstractmethod + def __init__(self, assets, arquivo): pygame.sprite.Sprite.__init__(self) - - self.image = assets[BALLOON_IMG] + self.image = assets[arquivo] self.mask = pygame.mask.from_surface(self.image) self.rect = self.image.get_rect() + self.assets = assets + +# Superclasse abstrata das águias, que contêm dados em comum como rect.y, speed_x, speed_y +class Eagle(ConstrutorComum): + """Define velocidade das águias e posição em y no uso do update""" + @abstractmethod + def __init__(self,assets,arquivo): + super().__init__(assets,arquivo) + self.speed_x = randint(2, 6) + self.speed_y = randint(-7, 7) + @abstractmethod + def update(self): + self.rect.y = randint(200, 400) + self.speed_x = randint(2, 6) + self.speed_y = randint(-7, 7) + +class EagleLeft(Eagle): + """Seta as águias que surgirão do lado esquerdo da tela""" + def __init__(self, assets, arquivo): + """Recebe e define os dados iniciais dessas águias""" + super().__init__(assets, arquivo) + self.rect.x = -50 + self.rect.y = randint(200, 400) + + def update(self): + """Atualiza a posição das águias""" + self.rect.x += self.speed_x + self.rect.y += self.speed_y + + if self.rect.top > 600 or self.rect.left > 900: + self.rect.x = -50 + super().update() + +class EagleRight(Eagle): + """Seta as águias que surgirão do lado direito da tela""" + def __init__(self, assets, arquivo): + """Recebe e define os dados iniciais dessas águias""" + super().__init__(assets, arquivo) + self.rect.x = 950 + self.rect.y = randint(200, 400) + + def update(self): + """Atualiza a posição das águias""" + self.rect.x -= self.speed_x + self.rect.y += self.speed_y + + if self.rect.top > 600 or self.rect.right < -50: + self.rect.x = 950 + super().update() + +class Balao(ConstrutorComum): + """ Seta o balão""" + def __init__(self,assets,arquivo,groups,lives): + """Recebe e define dados iniciais do balão""" + super().__init__(assets,arquivo) self.rect.centerx = WIDTH / 2 self.rect.bottom = HEIGHT / 2 self.speedx = 0 self.speedy = 0 self.groups = groups - self.assets = assets self.lives = lives self.shot = pygame.time.get_ticks() @@ -38,14 +92,14 @@ def update(self): if self.rect.bottom > HEIGHT: self.rect.bottom = HEIGHT - def shoot(self): + def shoot(self,assets,arquivo): """ Álcool em gel criado logo abaixo do balão""" now = pygame.time.get_ticks() elapsed_ticks = now - self.shot if elapsed_ticks > self.shot_tick: self.shot = now - gel = Gel(self.assets, self.rect.bottom, self.rect.centerx) + gel = Gel(assets, arquivo, self.rect.bottom, self.rect.centerx) self.groups['all_sprites'].add(gel) self.groups['gels'].add(gel) self.assets['pop_sound'].play() @@ -53,13 +107,12 @@ def shoot(self): # --- Cria classe para a vida do balão, que se movimenta em função do balão class Life(pygame.sprite.Sprite): """Seta a vida do balão""" - def __init__(self, balao, assets): + def __init__(self,assets,arquivo,balao): """Recebe e define dados iniciais da vida do balão""" pygame.sprite.Sprite.__init__(self) - self.balao = balao self.assets = assets - life = self.assets['life_font'].render('{:04d}'.format(self.balao.lives), True, (255, 255, 0)) + life = self.assets[arquivo].render('{:04d}'.format(self.balao.lives), True, (255, 255, 0)) self.image = life self.rect = self.image.get_rect() self.rect.centerx = self.balao.rect.centerx @@ -67,74 +120,18 @@ def __init__(self, balao, assets): def update(self): """Atualiza a posição da vida do balão de acordo com a posição do balão""" - life = self.assets['life_font'].render('{:04d}'.format(self.balao.lives), True, (255, 255, 0)) self.image = life self.rect = self.image.get_rect() self.rect.centerx = self.balao.rect.centerx self.rect.bottom = self.balao.rect.bottom - 47 -# --- Cria as classes das águias, uma para cada águia dependendo do lado da tela em que surge -class Eagle1(pygame.sprite.Sprite): - """Seta as águias que surgirão do lado esquerdo da tela""" - def __init__(self, assets): - """Recebe e define os dados iniciais dessas águias""" - pygame.sprite.Sprite.__init__(self) - - self.image = assets[EAGLE1_IMG] - self.mask = pygame.mask.from_surface(self.image) - self.rect = self.image.get_rect() - self.rect.x = -50 - self.rect.y = randint(200, 400) - self.speed_x = randint(2, 6) - self.speed_y = randint(-7, 7) - - def update(self): - """Atualiza a posição dessas águias""" - self.rect.x += self.speed_x - self.rect.y += self.speed_y - - if self.rect.top > 600 or self.rect.left > 900: - self.rect.x = -50 - self.rect.y = randint(200, 400) - self.speed_x = randint(2, 6) - self.speed_y = randint(-7, 7) - -class Eagle2(pygame.sprite.Sprite): - """Seta as águias que surgirão do lado direito da tela""" - def __init__(self, assets): - """Recebe e define os dados iniciais dessas águias""" - pygame.sprite.Sprite.__init__(self) - - self.image = assets[EAGLE2_IMG] - self.mask = pygame.mask.from_surface(self.image) - self.rect = self.image.get_rect() - self.rect.x = 950 - self.rect.y = randint(200, 400) - self.speed_x = randint(2, 6) - self.speed_y = randint(-7, 7) - - def update(self): - """Atualiza a posição dessas águias""" - self.rect.x -= self.speed_x - self.rect.y += self.speed_y - - if self.rect.top > 600 or self.rect.right < -50: - self.rect.x = 950 - self.rect.y = randint(200, 400) - self.speed_x = randint(2, 6) - self.speed_y = randint(-7, 7) - # --- Cria a classe Covid, pensada como uma maneira do jogador interagir melhor com o jogo -class Covid(pygame.sprite.Sprite): +class Covid(ConstrutorComum): """ Seta a(s) covid(es)""" - def __init__(self, assets): + def __init__(self, assets,arquivo): """Recebe e define dados iniciais da covid""" - pygame.sprite.Sprite.__init__(self) - - self.image = assets[COVID_IMG] - self.mask = pygame.mask.from_surface(self.image) - self.rect = self.image.get_rect() + super().__init__(assets,arquivo) self.rect.x = randint(60, 520) self.rect.y = choice([800, 1000, 1200]) self.speed_y = randint(1, 3) @@ -149,16 +146,11 @@ def update(self): self.speed_y = randint(1, 3) # --- Cria classe para o gel, que pode matar o covid -class Gel(pygame.sprite.Sprite): +class Gel(ConstrutorComum): """Seta gotas de álcool em gel""" - def __init__(self, assets, bottom, centerx): + def __init__(self, assets, arquivo, bottom, centerx): """Recebe e define os dados iniciais dessas gotas""" - pygame.sprite.Sprite.__init__(self) - - self.image = assets[GEL_IMG] - self.mask = pygame.mask.from_surface(self.image) - self.rect = self.image.get_rect() - + super().__init__(assets, arquivo) self.rect.centerx = centerx self.rect.bottom = bottom self.speedy = 10