【问题标题】:'pygame.Rect' object is not callable'pygame.Rect' 对象不可调用
【发布时间】:2021-04-20 15:41:14
【问题描述】:

代码:

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

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

【问题讨论】:

    标签: python python-3.x pygame


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2022-11-26
      • 2021-08-21
      • 2022-08-19
      • 2018-09-30
      • 2021-02-18
      • 1970-01-01
      • 1970-01-01
      • 2016-10-01
      • 2015-01-29
      相关资源
      最近更新 更多