Reputation: 569
I'm brand new to pygame, and trying to create a simple program that allows me to move an image around the screen using the keyboard. I'm getting the error in title when trying to get the image on the screen using the appearance method. I have a working version that I wrote without using classes, but would like to understand classes so I can implement them in the future.
Here is my code:
import pygame, sys
from pygame.locals import *
pygame.init()
FPS = 30
fpsClock = pygame.time.Clock()
DISPLAYSURF = pygame.display.set_mode((600, 500), 0, 32)
pygame.display.set_caption('Animation')
class Hero():
def __init__(self):
posx = 10
posy = 10
def appearance():
return pygame.image.load('C:\\Users\\admin\\Desktop\\right.png')
def move_right(x):
posx += 10
def move_left(x):
posx -= 10
def move_up(y):
posy -= 10
def move_down(y):
posy += 10
new_hero = Hero() #create a Hero
while True:
item = new_hero.appearance
DISPLAYSURF.blit(item, (posx, posy)) #error
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
fpsClock.tick(FPS)
Upvotes: 1
Views: 295
Reputation: 311348
Your item
is a reference to the new_hero.appearance
method. In order to invoke the method and get the surface it should return, you need to use ()
:
item = new_hero.appearance()
# Here -------------------^
Upvotes: 1
Reputation: 4866
You are assigning the method, not the return value:
item = new_hero.appearance
What you should do instead is:
item = new_hero.appearance()
Upvotes: 1