TypeError: 'pygame.Surface' 对象不可调用并且 pygame 窗口崩溃

2022-01-12 00:00:00 python pygame crash

问题描述

我对 Pygame 和 Python 还很陌生,我刚刚编写了我的第一个代码,但不知何故我不断收到此错误:

I'm quite new to Pygame and Python and I've just made one of my first codes but somehow I keep getting this error:

TypeError: 'pygame.Surface' object is not callable

我不知道是代码有问题还是因为 Pygame/Python 没有正确安装.

I don't know whether there is something wrong in the code or just because Pygame/Python isn't installed correctly.

bif="bg.jpg"
mif="ball.png"

import pygame, sys
from pygame.locals import *

pygame.init()
screen=pygame.display.set_mode((640,360),0,32)

background=pygame.image.load(bif).convert()
mouse_c=pygame.image.load(mif).convert_alpha()

while True:
   for event in pygame.event.get():
     if event.type == QUIT:
        pygame.quit()
        sys.exit()

screen.blit(background, (0,0))

x,y = pygame.mouse.get_pos()
x -= mouse_c.get_width()/2
y -= mouse_c.get_height()/2

screen.blit(mouse_c(x,y))

pygame.display.update()

运行此代码后,pygame 窗口崩溃.

After running this code the pygame window crashes.


解决方案

你少了一个逗号:

screen.blit(mouse_c(x,y))

应该是

screen.blit(mouse_c, (x,y))
                 # ^

在第一个版本中,mouse_c(x, y) 被解释为尝试调用 mouse_c(这是一个pygame.Surface 并且因此不可调用)带有参数 xy,而它们实际上是单独的参数(sourcedest) 到 screen.blit.

In the first version, mouse_c(x, y) is interpreted as an attempt to call mouse_c (which is a pygame.Surface and thus not callable) with arguments x and y, when they are in fact separate arguments (source and dest) to screen.blit.

相关文章