[英]TypeError: argument 1 must be pygame.Surface, not method
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. 我是pygame的新手,并尝试创建一个简单的程序,使我可以使用键盘在屏幕上移动图像。 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)
You are assigning the method, not the return value: 您正在分配方法,而不是返回值:
item = new_hero.appearance
What you should do instead is: 相反,您应该做的是:
item = new_hero.appearance()
Your item
is a reference to the new_hero.appearance
method. 您的
item
是对new_hero.appearance
方法的引用。 In order to invoke the method and get the surface it should return, you need to use ()
: 为了调用该方法并获取它应该返回的表面,您需要使用
()
:
item = new_hero.appearance()
# Here -------------------^
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.