TypeError:'pygame.Surface'对象不可调用并且pygame窗口崩溃 [英] TypeError: 'pygame.Surface' object is not callable and pygame window crashes

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

问题描述

我对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 (因此不可调用),其参数为 x y ,而实际上它们是单独的参数( source 和<代码>目标)到 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窗口崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆