Pygame类型错误:'int'对象不可调用。

4

这几行Python 3.2代码使用最新的pygame模块来移动图像。它与此示例相关:http://www.pygame.org/docs/tut/MoveIt.html

以下是我的代码:

import pygame
import sys 

pygame.init()
screen = pygame.display.set_mode((600,400))
bg = pygame.image.load('bg.jpg')
player = pygame.image.load('player.jpg')
pos = player.get_rect()

while True:
    for i in pygame.event.get():
       if i.type() == pygame.QUIT:
            sys.exit()
    screen.blit(bg,(0,0))
    screen.blit(player,(i,i))
    pygame.display.update()

运行时出现以下错误:

Traceback (most recent call last):
File "C:/Users/Grounds Keeper Willy/Desktop/Python Code/test.py", line 12, in
if i.type() == pygame.QUIT:
TypeError: 'int' object is not callable

我发现有类似的问题,但答案指出问题是将函数名用作变量,而这不是我的情况。我似乎无法弄清楚问题出在哪里。

1个回答

5
应该这样写:

应该阅读:

if i.type == pygame.QUIT:

改为:

if i.type() == pygame.QUIT:

由于 Event 类的 type 成员不是一个函数,因此您不需要/无法使用 () 来调用它。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接