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

saf*_*fie 2 python crash pygame

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

TypeError: 'pygame.Surface' object is not callable
Run Code Online (Sandbox Code Playgroud)

我不知道代码是否有问题,或者只是因为Pygame/Python安装不正确。

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()
Run Code Online (Sandbox Code Playgroud)

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

jon*_*rpe 5

你少了一个逗号:

screen.blit(mouse_c(x,y))
Run Code Online (Sandbox Code Playgroud)

应该

screen.blit(mouse_c, (x,y))
                 # ^
Run Code Online (Sandbox Code Playgroud)

在第一个版本中,mouse_c(x, y)被解释为尝试使用参数和来调用 mouse_c(这是 a pygame.Surface,因此不可调用),而实际上它们是 的单独参数 (和) 。xysourcedestscreen.blit