简体   繁体   English

TypeError:参数1必须是pygame.Surface,而不是方法

[英]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.

相关问题 TypeError:参数1必须是pygame.Surface,而不是buildin_function_or_method - TypeError: argument 1 must be pygame.Surface, not builtin_function_or_method TypeError:参数1必须是pygame.Surface,而不是str - TypeError: argument 1 must be pygame.Surface, not str 类型错误:参数 1 必须是 pygame.Surface,而不是类型 - TypeError: argument 1 must be pygame.Surface, not type TypeError:参数1必须是pygame.Surface,而不是pygame.Rect - TypeError: argument 1 must be pygame.Surface, not pygame.Rect TypeError:参数1必须是pygame.Surface,而不是str [使用按钮的Pygame] - TypeError: argument 1 must be pygame.Surface, not str [Pygame using buttons] Pygame 错误“类型错误:参数 1 必须是 pygame.Surface,而不是列表” - Pygame error 'TypeError: argument 1 must be pygame.Surface, not list' 类型错误:参数 1 必须是 pygame.Surface,而不是列表及更多 - TypeError: argument 1 must be pygame.Surface, not list & more “类型错误:参数 1 必须是 pygame.Surface,而不是列表”:不理解 - ' TypeError: argument 1 must be pygame.Surface, not list ' : Not understanding 类型错误:参数 1 必须是 pygame.Surface,而不是列表 - 它不是列表 - TypeError: argument 1 must be pygame.Surface, not list - Its not a list 如何解决:TypeError:参数1必须是pygame.Surface,而不是buildin_function_or_method - How to resolve: TypeError: argument 1 must be pygame.Surface, not builtin_function_or_method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM