Solucionado (ver solução)
Solucionado
(ver solução)
3
respostas

Fantasma não aparece na tele

Fiz o código como o do instrutor, entretanto meu fantasma não apareceu na tele.

Código classe fantasma:

class Fanteasma(ElementoJogo):
    def __init__(self, cor, tamanho):
        self.coluna = 6.0
        self.linha = 8.0
        self.cor = cor
        self.tamanho = tamanho

    def pintar(self, surface):
        fatia = self.tamanho // 8
        px = int(self.coluna * self.tamanho)
        py = int(self.linha * self.tamanho)
        contorno = [(px, py+self.tamanho),
                    (px + fatia, py + fatia * 2),
                    (px + fatia * 2, py + fatia // 2),
                    (px + fatia * 3, py),
                    (px + fatia * 5, py),
                    (px + fatia * 6, py + fatia // 2),
                    (px + fatia * 7, py + fatia * 2),
                    (px + self.tamanho, py + self.tamanho)]
        pygame.draw.polygon(surface, self.cor, contorno, 0)

        olho_raio_ext = fatia
        olho_raio_int =  fatia // 2

        olho_e_x = int(px + fatia * 2.5)
        olho_e_y = int(px + fatia * 2.5)
        olho_d_x = int(px + fatia * 5.5)
        olho_d_y = int(px + fatia * 2.5)

        pygame.draw.circle(screen, BRANCO, (olho_e_x, olho_e_y), olho_raio_ext, 0)
        pygame.draw.circle(screen, PRETO, (olho_e_x, olho_e_y), olho_raio_int, 0)
        pygame.draw.circle(screen, BRANCO, (olho_d_x, olho_d_y), olho_raio_ext, 0)
        pygame.draw.circle(screen, PRETO, (olho_d_x, olho_d_y), olho_raio_int, 0)

Qual o problema? Não consegui encontrar.

3 respostas

Oii Rafael, como você está?

Sinto muito pela demora em obter um retorno.

Testei o trecho de código que você apresentou e o fantasma apareceu, entretanto, estava um pouco desconfigurado, veja na imagem abaixo:

image

Caso o problema seja esse, isso está ocorrendo por causa das seguintes coordenadas:

olho_e_x = int(px + fatia * 2.5)
olho_e_y = int(px + fatia * 2.5)
olho_d_x = int(px + fatia * 5.5)
olho_d_y = int(px + fatia * 2.5)

Observe que em olho_e_y e olho_d_y você utiliza a coordenada de x ou seja px, porém, como se trata de y, temos que utilizar py. Veja a correção:

olho_e_x = int(px + fatia * 2.5)
olho_e_y = int(py + fatia * 2.5)
olho_d_x = int(px + fatia * 5.5)
olho_d_y = int(py + fatia * 2.5)

Além disso, também é necessário implementar os métodos calcular_regras e processar_eventos dentro da classe Fantasma, pois são métodos derivados da classe abstrata ElementoJogo.

Abaixo, o código da classe com as correções:

class Fantasma(ElementoJogo):
    def __init__(self, cor, tamanho):
        self.coluna = 6.0
        self.linha = 8.0
        self.cor = cor
        self.tamanho = tamanho

    def pintar(self, surface):
        fatia = self.tamanho // 8
        px = int(self.coluna * self.tamanho)
        py = int(self.linha * self.tamanho)
        contorno = [(px, py + self.tamanho),
                    (px + fatia, py + fatia * 2),
                    (px + fatia * 2, py + fatia // 2),
                    (px + fatia * 3, py),
                    (px + fatia * 5, py),
                    (px + fatia * 6, py + fatia // 2),
                    (px + fatia * 7, py + fatia * 2),
                    (px + self.tamanho, py + self.tamanho)]
        pygame.draw.polygon(surface, self.cor, contorno, 0)

        olho_raio_ext = fatia
        olho_raio_int = fatia // 2

        olho_e_x = int(px + fatia * 2.5)
        olho_e_y = int(py + fatia * 2.5)
        olho_d_x = int(px + fatia * 5.5)
        olho_d_y = int(py + fatia * 2.5)

        pygame.draw.circle(screen, BRANCO, (olho_e_x, olho_e_y), olho_raio_ext, 0)
        pygame.draw.circle(screen, PRETO, (olho_e_x, olho_e_y), olho_raio_int, 0)
        pygame.draw.circle(screen, BRANCO, (olho_d_x, olho_d_y), olho_raio_ext, 0)
        pygame.draw.circle(screen, PRETO, (olho_d_x, olho_d_y), olho_raio_int, 0)

    def calcular_regras(self):
        pass

    def processar_eventos(self, evts):
        pass

Se ainda sim com essas correções o fantasma não aparecer na tela, compartilhe o código completo no drive ou github para que eu possa analisar ele como um todo, a princípio testei o código da aula modificando apenas o trecho que você disponibilizou da classe Fantasma.

Qualquer dúvida estou por aqui, tá bom?

Grande abraço!

Olá, a modificação não deu resultado. Vou disponibilizar o código nesse link do Drive: https://drive.google.com/file/d/1XbjSGPDg11yiK1XYLpzWoco-G_1qegi9/view?usp=sharing.

solução!

Rafael, analisei seu código.

O que está fazendo o fantasma não aparecer está no seguinte trecho do laço while True:

# Pintar a tela
screen.fill(PRETO)
# for elemento in objetos:
#     elemento.pintar(screen)
cenario.pintar(screen)
pacman.pintar(screen)
blinky.pintar(screen)
pygame.time.delay(100)

No trecho acima precisamos utilizar a instrução pygame.display.update() para que os elementos sejam atualizados/desenhados na tela. Veja como fica a correção:

# Pintar a tela
screen.fill(PRETO)
# for elemento in objetos:
#     elemento.pintar(screen)
cenario.pintar(screen)
pacman.pintar(screen)
blinky.pintar(screen)
pygame.display.update() # linha que foi adicionada
pygame.time.delay(100)

Após isso, tente executar novamente e veja se o fantasma aparecerá na tela.

Qualquer dúvida estou por aqui, tá bom?

Abraços!

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software