TypeError: 'pygame.Surface' object is not callable and pygame window crashes

送分小仙女□ 提交于 2021-02-17 02:43:00

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!