TypeError: 'pygame.Surface' 对象不可调用并且

时间:2023-04-26
本文介绍了TypeError: 'pygame.Surface' 对象不可调用并且 pygame 窗口崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我对 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.

这篇关于TypeError: 'pygame.Surface' 对象不可调用并且 pygame 窗口崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:为什么我在 Mac 上键入括号时 Python 的 IDLE 会崩溃 下一篇:为什么 PyQt5 QPixmap 会导致 python 崩溃?

相关文章

最新文章