问题
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
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()
After running this code the pygame window crashes.
回答1:
You are missing a comma:
screen.blit(mouse_c(x,y))
should be
screen.blit(mouse_c, (x,y))
# ^
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.
来源:https://stackoverflow.com/questions/23462189/typeerror-pygame-surface-object-is-not-callable-and-pygame-window-crashes