'pygame.Rect' 对象不可调用 [英] 'pygame.Rect' object is not callable
问题描述
代码:
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), 0 ,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, 450), 0 ,20,20)
player = pygame.Rect(250, 450, 50, 50)
blast = pygame.Rect(300, 0 ,20,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, 0], ball)
pygame.draw.ellipse(screen, [0, 0, 255], player)
pygame.draw.ellipse(screen, [0, 255, 0], 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
我正在尝试创建一些会移动的球.当我点击时,绿色的球或爆炸应该会移动,错误是否意味着有一个 ) 或一个 ( 不属于?但我找不到我的错误......我以前遇到过这个错误,我不记得我是如何解决它的.什么是错误是什么意思?我该如何解决?提前致谢!
I am trying to create some balls that move. and the green ball or blast is supposed to move when I click, Does the error mean that there is a ) or a ( that doesn't belong? But I can't find my mistake... I've had this error before and I can't remember how I solved it. what does the error mean? and how can I fix this? thanks in advance!
推荐答案
你的全局 blast
变量 阴影 blast
功能.当您调用 blast()
时,blast
不再定义为函数,而是一个 pygame.Rect
.该错误告诉您不能像函数一样调用 Rect
.
Your global blast
variable shadows the blast
function. By the time you call blast()
, blast
is no longer defined as a function, but is a pygame.Rect
instead. The error is telling you that you can't call a Rect
like a function.
更改其中之一的名称,以免它们发生冲突.
Change the name of one of them so they don't collide.
这篇关于'pygame.Rect' 对象不可调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!