'pygame.Rect' 对象不可调用

问题描述

代码

import pygame,sys,time,random
from pygame.locals import *

def enemie():
    global speed,ball
    ball.y += speed
    if ball.y == 602:
        ball = pygame.Rect(random.randint(0,450),20,20)

def blast():
    global bl,blast
    blast.y = bl


def player_animation():
    global player_speed,playerx
    player.x = player_speed



pygame.init()
running = True

clock = pygame.time.Clock()

speed = 7
bl = 1
player_speed = 1



ball = pygame.Rect(random.randint(0,20)
player = pygame.Rect(250,450,50,50)
blast = pygame.Rect(300,20)
screen = pygame.display.set_mode((500,500))


while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
                running = False
    enemie()
    player_animation()
    blast()
  
    screen.fill((255,255,255)) #color

    
    pygame.draw.ellipse(screen,[255,0],ball)
    pygame.draw.ellipse(screen,[0,255],player)
    pygame.draw.ellipse(screen,blast)
    
    if ball.colliderect(player):
        pygame.quit()
        sys.exit()
        

    keys=pygame.key.get_pressed()
    if keys[K_LEFT] and player.x !=  -4 :
        player_speed -= 5

    if keys[K_RIGHT] and player.x !=  451:
        player_speed += 5

    if event.type == pygame.MOUSEBUTTONDOWN:
        bl += 5
    pygame.display.flip()
    
    clock.tick(30)
pygame.quit()

错误

line 54,in <module>
    blast()
TypeError: 'pygame.Rect' object is not callable

我正在尝试创建一些会移动的球。当我点击时,绿色的球或爆炸应该会移动, 错误是否意味着有一个 ) 或一个 ( 不属于?但我找不到我的错误......我以前遇到过这个错误,我不记得我是如何解决它的。什么是错误是什么意思?我该如何解决?提前致谢!

解决方法

您的全局 blast 变量 shadows blast 函数。当您调用 blast() 时,blast 不再定义为函数,而是一个 pygame.Rect。该错误告诉您不能像函数一样调用 Rect

更改其中之一的名称,以免它们发生冲突。