Skip to content

Commit

Permalink
Fase 5
Browse files Browse the repository at this point in the history
  • Loading branch information
fccoelho committed May 23, 2021
1 parent 390c64f commit 2919d15
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 9 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,7 @@ Veja como o jogo ficou.
git checkout Fase_4
```

## Fase 5
Na fase 5 vamos adicionar tiros e colisões para completar o Jogo.

Os tiros serão implementados como uma classe.
Binary file added coronashooter/imagens/tiro.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
125 changes: 116 additions & 9 deletions coronashooter/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,22 @@
K_LEFT,
K_RIGHT,
QUIT,
K_ESCAPE, K_UP, K_DOWN
K_ESCAPE, K_UP, K_DOWN, K_RCTRL, K_LCTRL
)
from fundo import Fundo
from elementos import ElementoSprite
import random


class Jogo:
def __init__(self, size=(800, 800), fullscreen=False):
def __init__(self, size=(1000, 1000), fullscreen=False):
self.elementos = {}
pygame.init()
self.tela = pygame.display.set_mode(size)
self.fundo = Fundo()
self.jogador = None
self.interval = 0
self.nivel = 0
flags = DOUBLEBUF
if fullscreen:
flags |= FULLSCREEN
Expand All @@ -38,6 +40,17 @@ def manutenção(self):
enemy.set_pos([x, 0])
self.elementos["virii"].add(enemy)

def muda_nivel(self):
xp = self.jogador.get_pontos()
if xp > 10 and self.level == 0:
self.fundo = Fundo("tile2.png")
self.nivel = 1
self.jogador.set_lives(self.jogador.get_lives() + 3)
elif xp > 50 and self.level == 1:
self.fundo = Fundo("tile3.png")
self.nivel = 2
self.jogador.set_lives(self.player.get_lives() + 6)

def atualiza_elementos(self, dt):
self.fundo.update(dt)
for v in self.elementos.values():
Expand All @@ -48,6 +61,40 @@ def desenha_elementos(self):
for v in self.elementos.values():
v.draw(self.tela)

def verifica_impactos(self, elemento, list, action):
if isinstance(elemento, pygame.sprite.RenderPlain):
hitted = pygame.sprite.groupcollide(elemento, list, 1, 0)
for v in hitted.values():
for o in v:
action(o)
return hitted

elif isinstance(elemento, pygame.sprite.Sprite):
if pygame.sprite.spritecollide(elemento, list, 1):
action()
return elemento.morto

def ação_elemento(self):
self.verifica_impactos(self.jogador, self.elementos["tiros_inimigo"],
self.jogador.alvejado)
if self.jogador.morto:
self.run = False
return

# Verifica se o personagem trombou em algum inimigo
self.verifica_impactos(self.jogador, self.elementos["virii"],
self.jogador.colisão)
if self.jogador.morto:
self.run = False
return
# Verifica se o personagem atingiu algum alvo.
hitted = self.verifica_impactos(self.elementos["tiros"],
self.elementos["virii"],
Virus.alvejado)

# Aumenta a pontos baseado no número de acertos:
self.jogador.set_pontos(self.jogador.get_pontos() + len(hitted))

def trata_eventos(self):
event = pygame.event.poll()
if event.type == pygame.QUIT:
Expand All @@ -57,6 +104,9 @@ def trata_eventos(self):
key = event.key
if key == K_ESCAPE:
self.run = False
elif key in (K_LCTRL, K_RCTRL):
self.interval = 0
self.jogador.atira(self.elementos["tiros"])
elif key == K_UP:
self.jogador.accel_top()
elif key == K_DOWN:
Expand All @@ -66,25 +116,29 @@ def trata_eventos(self):
elif key == K_LEFT:
self.jogador.accel_left()

keys = pygame.key.get_pressed()
if self.interval > 10:
self.interval = 0
if keys[K_RCTRL] or keys[K_LCTRL]:
self.jogador.atira(self.elementos["tiros"])

def loop(self):
clock = pygame.time.Clock()
dt = 16
self.elementos['virii'] = pygame.sprite.RenderPlain(Virus([120, 50]))
# enemy = Virus([0, 0])
# enemy.set_pos([50, 50])
# self.elementos['virii'].add(enemy)
self.jogador = Jogador([200, 400], 5)
self.elementos['jogador'] = pygame.sprite.RenderPlain(self.jogador)
self.elementos['tiros'] = pygame.sprite.RenderPlain()
self.elementos['tiros_inimigo'] = pygame.sprite.RenderPlain()
while self.run:
clock.tick(1000 / dt)

self.trata_eventos()
self.ação_elemento()
self.manutenção()
# Atualiza Elementos
self.atualiza_elementos(dt)



# Desenhe no back buffer
self.desenha_elementos()
pygame.display.flip()
Expand All @@ -105,7 +159,18 @@ def set_lives(self, lives):
self.lives = lives

def colisão(self):
if self.get_lives() == 0:
if self.get_lives() <= 0:
self.kill()
else:
self.set_lives(self.get_lives() - 1)

def atira(self, lista_de_tiros, image=None):
s = list(self.get_speed())
s[1] *= 2
Tiro(self.get_pos(), s, image, lista_de_tiros)

def alvejado(self):
if self.get_lives() <= 0:
self.kill()
else:
self.set_lives(self.get_lives() - 1)
Expand All @@ -132,7 +197,7 @@ def accel_right(self):


class Virus(Nave):
def __init__(self, position, lives=0, speed=None, image=None, size=(100, 100)):
def __init__(self, position, lives=1, speed=None, image=None, size=(100, 100)):
if not image:
image = "virus.png"
super().__init__(position, lives, speed, image, size)
Expand Down Expand Up @@ -181,6 +246,48 @@ def get_pontos(self):
def set_pontos(self, pontos):
self.pontos = pontos

def atira(self, lista_de_tiros, image=None):
l = 1
if self.pontos > 10: l = 3
if self.pontos > 50: l = 5

p = self.get_pos()
speeds = self.get_fire_speed(l)
for s in speeds:
Tiro(p, s, image, lista_de_tiros)

def get_fire_speed(self, shots):
speeds = []

if shots <= 0:
return speeds

if shots == 1:
speeds += [(0, -5)]

if shots > 1 and shots <= 3:
speeds += [(0, -5)]
speeds += [(-2, -3)]
speeds += [(2, -3)]

if shots > 3 and shots <= 5:
speeds += [(0, -5)]
speeds += [(-2, -3)]
speeds += [(2, -3)]
speeds += [(-4, -2)]
speeds += [(4, -2)]

return speeds


class Tiro(ElementoSprite):
def __init__(self, position, speed=None, image=None, list=None):
if not image:
image = "tiro.png"
super().__init__(image, position, speed)
if list is not None:
self.add(list)


if __name__ == '__main__':
J = Jogo()
Expand Down

0 comments on commit 2919d15

Please sign in to comment.