Esempio n. 1
0
class TelaDeJogo(Tela):
    """ Tela responsavel pelo nivel em si

    Guarda algumas das variaveis utilizadas no jogo,
    enquanto outras pertencem ao mapa sendo utilizado

    @param superficie: superficie pygame
    @param nivel: nome do nivel a ser carregado
    @param slot: slot de jogo salvo selecionado
    """
    def __init__(self, superficie, nivel, slot):
        from jogador import Jogador
        from mapa import Mapa
        from poderes import Cinza
        from entidades import classes_instanciaveis, renderizar_hitbox, poderes_no_jogador
        self.__classes_instanciaveis = classes_instanciaveis
        global dicionaro_mapa
        super().__init__(superficie)
        (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('miriam', 48)
        self.__atrasofim = 0
        self.__nivel = nivel
        self.__slot = slot
        self.__sobreposicao = None

        ##### ENTRADAS DO JOGADOR #####
        self.__cima, self.__baixo, self.__direita, self.__esquerda = 0, 0, 0, 0
        self.__espaco = False
        self.__bola_fogo = False
        self.__troca_poder = False

        ##### MAPA #####
        self.__mapa = Mapa(superficie)
        poder_atual = Cinza()
        poder_armazenado = Cinza()
        slots = DAOJogo.saves
        slot_atual = slots[self.__slot]
        for item in poderes_no_jogador:
            if item.__name__ == slot_atual[2]:
                poder_atual = item()
            if item.__name__ == slot_atual[3]:
                poder_armazenado = item()
        self.__jogador = self.__mapa.iniciar(nivel, dicionaro_mapa,
                                             poder_atual, poder_armazenado,
                                             slot_atual[4])
        self.__comeco = pygame.time.get_ticks() / 1000
        if not pygame.mixer.music.get_busy(): pygame.mixer.music.play(-1)

    def salvar_jogo(self):
        "Salva o jogo ao ganhar ou perder"
        slots = DAOJogo.saves
        slots[self.__slot] = [
            self.__nivel, self.__nivel,
            type(self.__jogador.poder).__name__,
            type(self.__jogador.poder_armazenado).__name__,
            self.__jogador.paleta
        ]
        DAOJogo.saves = slots

    def atualizar(self, ciclo):
        '''Logica de jogo, envolvendo controles, colisao e renderizacao

        Deve ser chamada pela funcdao gerente 60 vezes por segundo

        @param ciclo: responsavel pelas frames de animacao do Rabisco
        
        @returns: Instancia da proxima tela a ser executada apos o nivel
                  [False] se o jogo for fechado,
                  [True] se o jogo continuar na frame seguinte
        '''
        if isinstance(self.__sobreposicao, Sobreposicao): pausado = True
        else: pausado = False
        if not pausado:
            for evento in pygame.event.get():
                if evento.type == pygame.QUIT:
                    self.salvar_jogo()
                    return [False]
                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.key == pygame.K_ESCAPE:
                        self.__sobreposicao = TelaPause(self)
                    if evento.key == pygame.K_TAB:
                        self.__troca_poder = 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.key == pygame.K_TAB:
                        self.__troca_poder = False
                if evento.type == pygame.MOUSEBUTTONDOWN:
                    self.__bola_fogo = True
                elif evento.type == pygame.MOUSEBUTTONUP:
                    self.__bola_fogo = False
        else:
            for evento in pygame.event.get():
                if evento.type == pygame.QUIT:
                    self.salvar_jogo()
                    return False
                if evento.type == pygame.KEYDOWN:
                    if evento.key == pygame.K_ESCAPE:
                        self.__sobreposicao = None
            self.__direita = 0
            self.__esquerda = 0
            self.__cima = 0
            self.__baixo = 0
            self.__espaco = False
            self.__bola_fogo = False
            self.__troca_poder = False

        ##### FILA DE RENDERIZACAO E ATUALIZACAO #####

        self.__mapa.atualizar(self.superficie, self.__campo_visivel,
                              self.superficie.get_size(), ciclo)

        # FAZER O JOGADOR RECEBER UM MAPA E SALVAR ONDE ELE TA
        if self.__atrasofim > 0:
            self.__direita = 0
            self.__esquerda = 0
            self.__espaco = not self.__mapa.ganhou
        else:
            self.__jogador.poderes(self.superficie, self.__mapa,
                                   self.__bola_fogo)
        self.__campo_visivel = self.__jogador.atualizar(
            self.superficie, self.__mapa, [
                self.__direita, self.__esquerda, self.__espaco,
                self.__troca_poder
            ])

        # PERDENDO POR MORRER
        if self.__jogador.vida <= 0 and not self.__mapa.ganhou:
            self.__jogador.vida_pra_zero()
            self.__atrasofim += 1
            if self.__atrasofim <= 1:
                self.__textin = self.__fonte.render("FIM DE JOGO", False,
                                                    (0, 0, 0))
                pygame.mixer.music.fadeout(2400)
                if self.__mapa.escala_tempo > 1:
                    self.__textin = pygame.font.SysFont(
                        'msminchomspmincho',
                        48).render("神の御名(みめい)においてしりそける", False, (0, 0, 0))
            else:
                self.__jogador.tipos_transparentes = self.__classes_instanciaveis
            self.superficie.blit(
                self.__textin,
                (self.__campo_visivel.w / 2 - self.__textin.get_size()[0] / 2,
                 self.__campo_visivel.h / 2 - self.__textin.get_size()[1] / 2))
            if self.__atrasofim >= 150:
                self.salvar_jogo()
                return [
                    FimDeJogo, [self.superficie, self.__nivel, self.__slot]
                ]

        ### VENCENDO ###
        if self.__mapa.ganhou:
            self.__atrasofim += 1
            #if self.__atrasofim <= 1:
            #pygame.mixer.music.fadeout(2400)
            self.__textin = self.__fonte.render("VITÓRIA", False, (0, 0, 0))
            self.superficie.blit(
                self.__textin,
                (self.__campo_visivel.w / 2 - self.__textin.get_size()[0] / 2,
                 self.__campo_visivel.h / 2 - self.__textin.get_size()[1] / 2))
            if self.__atrasofim >= 150:
                self.salvar_jogo()
                return [
                    TelaDeJogo,
                    [self.superficie, self.__mapa.proxima_fase, self.__slot]
                ] if self.__mapa.proxima_fase else [
                    MenuPrincipal, [self.superficie]
                ]

        ##### TELA DE PAUSE NO JOGO #####
        try:
            resultado = self.__sobreposicao.atualizar(ciclo)
            if not resultado:
                self.__sobreposicao = None
            elif resultado == "Fechar":
                self.salvar_jogo()
                pygame.mixer.music.fadeout(500)
                return [
                    FimDeJogo, [self.superficie, self.__nivel, self.__slot]
                ]
        except AttributeError:
            pass

        ### FLIP PARA PASSAR PARA A TELA, LOGICA DE TEMPO
        pygame.display.flip()
        self.__tempo_maximo += (1 - self.__mapa.escala_tempo) / 60
        tempo_decorrido = pygame.time.get_ticks() / 1000 - self.__comeco
        if not self.__mapa.ganhou:
            self.__mapa.tempo_restante = int(
                max(self.__tempo_maximo - tempo_decorrido, 0))

        ### PERDENDO POR TEMPO
        if self.__mapa.tempo_restante == 0:
            self.__jogador.vida_pra_zero()
        return [True]
Esempio n. 2
0
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