class Tela_De_Jogo(Tela): def __init__(self, superficie, nivel): self.__superficie = superficie self.__background_colour = (150, 220, 255) # Cor do fundo (width, height) = superficie.get_size() self.__campo_visivel = pygame.Rect(0, 0, width, height) self.__comeco = 0 self.__tempo_maximo = 350 self.__fonte = pygame.font.SysFont('Arial', 20) self.__atrasofim = 0 ##### ENTRADAS DO JOGADOR ##### self.__cima, self.__baixo, self.__direita, self.__esquerda = 0, 0, 0, 0 self.__atrito = 0.5 self.__espaco = False self.__bola_fogo = False ###### INSTANCIAS DE OBJETOS ###### self.__jogador = Jogador('mario', 200, 550, 0, 1) ##### MAPA ##### self.__mapa = Mapa((width, height)) self.__mapa.iniciar([nivel[0].copy(), nivel[1].copy()]) self.__comeco = pygame.time.get_ticks() / 1000 def atualizar(self, ciclo): for evento in pygame.event.get(): if evento.type == pygame.QUIT: return 0 if evento.type == pygame.KEYDOWN: if evento.key == pygame.K_w: self.__cima = 5 if evento.key == pygame.K_s: self.__baixo = 5 if evento.key == pygame.K_d: self.__direita = 0.5 if evento.key == pygame.K_a: self.__esquerda = 0.5 if evento.key == pygame.K_SPACE or evento.key == pygame.K_w: self.__espaco = True if evento.type == pygame.KEYUP: if evento.key == pygame.K_w: self.__cima = 0 if evento.key == pygame.K_s: self.__baixo = 0 if evento.key == pygame.K_d: self.__direita = 0 if evento.key == pygame.K_a: self.__esquerda = 0 if evento.key == pygame.K_SPACE or evento.key == pygame.K_w: self.__espaco = False if evento.type == pygame.MOUSEBUTTONDOWN: self.__bola_fogo = True elif evento.type == pygame.MOUSEBUTTONUP: self.__bola_fogo = False ##### FILA DE RENDERIZACAO ##### self.__superficie.fill( self.__background_colour) # Preenaa a com o a cor de fundo self.__mapa.atualizar(self.__superficie, self.__campo_visivel, self.__superficie.get_size()) # FAZER O JOGADOR RECEBER UM MAPA E SALVAR ONDE ELE TA if self.__atrasofim > 0: self.__direita = 0 self.__esquerda = 0 self.__espaco = 0 self.__jogador.mover(self.__direita, self.__esquerda, self.__espaco, self.__superficie.get_size(), self.__mapa, self.__atrito) self.__jogador.poderes(self.__superficie, self.__mapa, self.__bola_fogo) self.__campo_visivel = self.__jogador.atualizar( self.__superficie, self.__campo_visivel, int(ciclo / 6)) # PERDENDO POR MORRER if self.__jogador.vida <= 0 and not self.__mapa.ganhou: self.__jogador.vida_pra_zero() self.__atrasofim += 1 textin = self.__fonte.render("PERDEU", 0, (0, 0, 0)) self.__superficie.blit(textin, (500, 300)) if self.__atrasofim >= 150: return 1 ### VENCENDO ### if self.__mapa.ganhou: self.__atrasofim += 1 textin = self.__fonte.render("VENCEU", 0, (0, 0, 0)) self.__superficie.blit(textin, (500, 300)) if self.__atrasofim >= 150: return 3 ##### RENDERIZACAO DA TELA ##### pygame.display.flip() tempo_decorrido = int((pygame.time.get_ticks() / 1000) - self.__comeco) self.__mapa.conta = self.__tempo_maximo - tempo_decorrido ##### PASSANDO A VIDA PRO DISPLAY #####d self.__mapa.vida_jogador = self.__jogador.vida ### PERDENDO POR TEMPO if self.__mapa.conta == 0: self.__jogador.vida_pra_zero() return 2
except: print("Falha ao carregar recursos.") else: jogando = True relogio = time.Clock() jogador.get_rect().centerx = 475 jogador.get_rect().centery = 640 while fecha_tela: relogio.tick(50) # velocidade que a bala vai for evento in event.get(): if evento.type == QUIT: fecha_tela = False if evento.type == KEYDOWN: # pega alguem evento do teclado if evento.key == K_LEFT: jogador.mover('esquerda') elif evento.key == K_RIGHT: jogador.mover('direita') elif evento.key == K_SPACE: jogador.disparar() elif evento.key == K_ESCAPE: fecha_tela = False imagemfundo.colocar(tela) for bala in jogador.get_disparos(): bala.colocar(tela) bala.mover('cima') for inimigo in inimigos: if bala.get_rect().colliderect(inimigo.get_rect()): jogador.remove_disparo(bala) inimigo.destruir()