繁体   English   中英

TypeError:“ pygame.Surface”对象不可调用[Pygame模块]

[英]TypeError: 'pygame.Surface' object is not callable [Pygame module]

我最近在学校学习课程时就一直在设计游戏,并认为这对取得成功很有帮助。

我遇到了错误

Traceback (most recent call last):
  File "C:\Users\Jake\Documents\Dungeon Crawler 2.1\Main.py", line 100, in <module>
    town()    
  File "C:\Users\Jake\Documents\Dungeon Crawler 2.1\Main.py", line 85, in town
    houseSpr1(0,250)
TypeError: 'pygame.Surface' object is not callable

在执行此代码时。

def town ():
    global outTown
    while not outTown:
            for event in pygame.event.get():
                    if event.type == pygame.MOUSEBUTTONDOWN:
                            print (event)
                            mx, my = pygame.mouse.get_pos()
                            mx = mx + my
                            if 375 <= mx <= 448:
                                    outTown = True
                                    print ("OK!")
                    if event.type == pygame.QUIT:
                            pygame.quit()
                            quit()

            houseSpr1 = pygame.image.load ('houseD.png') # default house img
            houseSpr1(0,250)
            gameDisplay.fill(green) # background
            pygame.draw.rect(gameDisplay,grey,(0,400,1280,50)) # main path
            pygame.draw.rect(gameDisplay,grey,(200,125,50,280)) # branch path
            player(x,y)
            pygame.display.update()
            clock.tick(60)

def houseSpr1(a,b):
    gameDisplay.blit(houseSpr1, (0,250))

def house1():
    global outHouse
    while not outHouse:
            gameDisplay.fill(black)
town()
houseSpr1()
gameIntro()
house1()
pygame.quit()
quit()

我了解这段代码在某些方面可能是低效的,如果您希望以此来提供见解,我也很高兴知道我也可以对其进行改进。


任何与此错误的帮助将不胜感激。 是的,我阅读了我之前的其余问题,我发现这主要归因于印刷错误,但我看不到这里的任何问题。

您将名称houseSpr1用于两件事:

  • 全局函数def houseSpr1(a,b): ...
  • 您加载的图像的局部变量: houseSpr1 = pygame.image.load ('houseD.png')

这两个名字不是独立的 函数只是Python中对象的另一种类型,它们就像其他任何变量一样被存储。

当您使用表达式houseSpr1(0,250) ,Python houseSpr1(0,250)其视为:

  1. 加载名为houseSpr1的对象
  2. 0250 (两个整数对象)中的结果
  3. 调用在步骤1中加载的对象,并传递步骤2的结果。

因为您在town()函数中给名称houseSpr1分配了一些东西,所以Python尝试调用的是该对象 (即加载的图像)。 PyGame使用名为Surface的对象类型加载图像,该错误告诉您您尝试调用该对象,但该对象不支持被调用。

解决方案是不要使用相同的名称:

houseSpr1Image = pygame.image.load('houseD.png')
houseSpr1(0, 250)

您还必须调整houseSpr1函数:

def houseSpr1(a,b):
    gameDisplay.blit(houseSpr1Image, (0,250))

您在这里还有其他问题; houseSpr1Image不是全局的,因此houseSpr1()函数找不到它,给您一个NameError异常。 您也将忽略该函数的ab参数,在那里将0,250硬编码。 您必须解决这些问题才能使代码正常工作。 也许您可以采用第三个参数image

def houseSpr1(image, a, b):
    gameDisplay.blit(image, (a, b))

并传递到图像表面:

houseSpr1Image = pygame.image.load('houseD.png')
houseSpr1(houseSpr1Image, 0, 250)

Martijn Pieters已经彻底解释了为什么您的代码不起作用。 这是程序的工作简化版,向您展示程序的外观。

实际上, blit_houseSpr1函数,因为您还可以在主循环中调用gameDisplay.blit(houseSpr1, (80, 250)) ,但是我只是想演示如何使用函数并将一些参数传递给它。 。

import pygame

pygame.init()
gameDisplay = pygame.display.set_mode((640, 480))
clock = pygame.time.Clock()

green = pygame.Color('green4')
grey = pygame.Color('grey50')
# Load the images once, because loading them from the hard disk is slow.
# And use the convert or convert_alpha methods to improve the performance.
houseSpr1 = pygame.image.load('houseD.png').convert()


def town():
    outTown = False
    while not outTown:
        for event in pygame.event.get():
            if event.type == pygame.MOUSEBUTTONDOWN:
                mx, my = pygame.mouse.get_pos()
                mx = mx + my
                if 375 <= mx <= 448:
                    outTown = True
            elif event.type == pygame.QUIT:
                outTown = True

        gameDisplay.fill(green) # background
        pygame.draw.rect(gameDisplay,grey,(0,400,1280,50)) # main path
        pygame.draw.rect(gameDisplay,grey,(200,125,50,280)) # branch path
        # Pass the image and the coordinates to the function.
        blit_houseSpr1(houseSpr1, 80, 250)

        pygame.display.update()
        clock.tick(60)


def blit_houseSpr1(image, a, b):
    # Now use the passed image and coordinates.
    gameDisplay.blit(image, (a, b))


town()
pygame.quit()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM