簡體   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