Issue
Code:
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()
error:
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?
Solution
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.
Answered By - 0x5453
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.