AtributeError in Pygame

4

In the process of executing this Pygame code, the following error appears on my terminal regardless of the corrections made to the code:

AttributeError: 'naveEspacial' object has no attribute 'dibujar'

How can I correct my code of this error and finally manage to execute the image? But more importantly, how can I prevent this from happening?

Thank you in advance.

import pygame,sys
from pygame.locals import *
# global variables
ancho = 900
alto = 480

class naveEspacial(pygame.sprite.Sprite):
    """Class for the thumb up ."""

def __init__(self):
    pygame.sprite.Sprite.__init__(self)
    self.ImagenNave = pygame.image.load('emojione-png/1f44d.png')   

    self.rect = self.ImagenNave.get_rect()
    self.rect.centerx = ancho/2
    self.rect.centery = alto-30

    self.listaDisparo= []
    self.Vida = True

def disparar(self):
    pass    

def dibujar(self, superficie):
    superficie.blit(self.ImagenNave, self.rect)

def Spaceinvader():
    pygame.init()
    venta = pygame.display.set_mode((ancho,alto))
    pygame.display.set_caption("faceinvader")

    ImagenFondo = pygame.image.load("Image/fondo.jpg")

    jugador = naveEspacial()

    while True:
        for evento in pygame.event.get():
            if evento.type == QUIT:
                pygame.quit()   
                sys.exit()

        venta.blit (ImagenFondo, (0,0))
        jugador.dibujar(venta)
        pygame.display.update()

Spaceinvader()

UPDATE:

    
asked by G. Michel 08.02.2016 в 14:06
source

1 answer

3

As mentioned in the comments, the problem is the indentation , the solution is very simple:

import pygame,sys
from pygame.locals import *
# global variables
ancho = 900
alto = 480

class naveEspacial(pygame.sprite.Sprite):
    """Class for the thumb up ."""

    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.ImagenNave = pygame.image.load('emojione-png/1f44d.png')   

        self.rect = self.ImagenNave.get_rect()
        self.rect.centerx = ancho/2
        self.rect.centery = alto-30

        self.listaDisparo= []
        self.Vida = True

    def disparar(self):
        pass    

    def dibujar(self, superficie):
        superficie.blit(self.ImagenNave, self.rect)


def Spaceinvader():
    pygame.init()
    venta = pygame.display.set_mode((ancho,alto))
    pygame.display.set_caption("faceinvader")

    ImagenFondo = pygame.image.load("Image/fondo.jpg")

    jugador = naveEspacial()

    while True:
        for evento in pygame.event.get():
            if evento.type == QUIT:
                pygame.quit()   
                sys.exit()

        venta.blit (ImagenFondo, (0,0))
        jugador.dibujar(venta)
        pygame.display.update()

Spaceinvader()

Note that now that you have indented a level to __init__ , disparar and dibujar , Python considers them as methods of the class naveEspacial .

In your code, Python considered them as functions within the scope of your script since they were on the first level of indentation and your class naveEspacial did not find the dibujar method that you called in this line:

jugador.dibujar(venta)

I invite you to read the PEP 0008 - Style Guide for Python Code which is like the Python bible about the code style.

Recommended readings:

Your error is a very common error in people who just start in Python, I recommend you take a look at the following links:

answered by 08.02.2016 в 14:35