I cannot move objects in pygame, but from tutorials that I'm learning is everything okay. I am search it for very long time, so I am so frustrated. Can someone help? Here is my code:
xPlayer1 = 15
yPlayer1 = 250
xPlayer2 = 765
yPlayer2 = 250
width = 15
height = 100
velocity = 5
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.time.delay(10)
spriteColor = (255, 255, 255)
key = pygame.key.get_pressed()
def Player1():
pygame.draw.rect(screen, spriteColor, pygame.Rect(xPlayer1, yPlayer1, width, height))
if key[pygame.K_UP] and yPlayer1>0:
yPlayer1 -= velocity # Here is my problem (error "UnboundLocalError:
local variable 'yPlayer1' referenced before
assignment"
if key[pygame.K_DOWN] and yPlayer1<500-height:
yPlayer1 += velocity # Same problem as higher
pygame.display.update()
def Player2():
pygame.draw.rect(screen, spriteColor, pygame.Rect(xPlayer2, yPlayer2, width, height))
pygame.display.update()
def Ball():
pygame.draw.circle(screen, spriteColor, (400, 300), 5)
pygame.display.update()
I have called Player1, Player2 and Ball