Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion Exemplos/pygame-snippets-master/moving_background.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-

# Importando as bibliotecas necessárias.
import pygame
Expand Down
Binary file modified UM BALAUM/__pycache__/data.cpython-38.pyc
Binary file not shown.
Binary file modified UM BALAUM/__pycache__/final_screen.cpython-38.pyc
Binary file not shown.
Binary file modified UM BALAUM/__pycache__/game_screen.cpython-38.pyc
Binary file not shown.
Binary file modified UM BALAUM/__pycache__/init_screen.cpython-38.pyc
Binary file not shown.
Binary file modified UM BALAUM/__pycache__/reference.cpython-38.pyc
Binary file not shown.
Binary file modified UM BALAUM/__pycache__/sprites.cpython-38.pyc
Binary file not shown.
Binary file modified UM BALAUM/__pycache__/start_screen.cpython-38.pyc
Binary file not shown.
2 changes: 1 addition & 1 deletion UM BALAUM/coded.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
64 changes: 40 additions & 24 deletions UM BALAUM/game_screen.py
Original file line number Diff line number Diff line change
@@ -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):

Expand All @@ -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)

Expand All @@ -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)
Expand All @@ -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)

Expand All @@ -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]:
Expand All @@ -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)

Expand All @@ -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
Expand All @@ -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
Expand All @@ -148,20 +165,20 @@ 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
balao.lives += 3
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
Expand All @@ -170,18 +187,18 @@ 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:
state = OVER
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
Expand Down Expand Up @@ -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
152 changes: 72 additions & 80 deletions UM BALAUM/sprites.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -38,103 +92,46 @@ 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()

# --- 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
self.rect.bottom = self.balao.rect.bottom - 47

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)
Expand All @@ -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
Expand Down