I am trying to make a program where the user can press the left key on their keyboard in order to animate the ball, but instead of doing that my software just closes the pygame window.
When I run the code below, instead of animating a ball that bounces around the screen when the left arrow key is pressed, my software just closes the window. Why is this happening?
import pygame
import random
import time
x = 750
y = 350
xdir = 5
ydir = 5
r = random.randint(10,255)
g = random.randint(10,255)
b = random.randint(10,255)
pygame.display.set_caption("The Bouncing Ball")
screen = pygame.display.set_mode((1100,700))
pygame.draw.circle(screen, (r, g, b), (x,y), 25, 0)
while True:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
while True:
while True:
screen = pygame.display.set_mode((1100,700))
pygame.draw.circle(screen, (r, g, b), (x,y), 25, 0)
pygame.display.update()
time.sleep(0.0093286)
if y >= 700:
ydir = -5
r = random.randint(10,255)
g = random.randint(10,255)
b = random.randint(10,255)
if x >= 1100:
xdir = -5
r = random.randint(10,255)
g = random.randint(10,255)
b = random.randint(10,255)
if x <= 0:
xdir = 5
r = random.randint(10,255)
g = random.randint(10,255)
b = random.randint(10,255)
if y <= 0:
ydir = 5
r = random.randint(10,255)
g = random.randint(10,255)
b = random.randint(10,255)
if x == 0 and y == 0:
break
if x == 0 and y == 700:
break
if x == 1100 and y == 700:
break
if x == 1100 and y == 0:
break
x+=xdir
y+=ydir
t_end = time.time() + 6.4
while time.time() < t_end:
screen = pygame.display.set_mode((1100,700))
pygame.draw.circle(screen, (r, g, b), (x,y), 25, 0)
pygame.display.update()
time.sleep(0.00934)
if y >= 700:
ydir = -5
if x >= 1100:
xdir = -5
if x <= 0:
xdir = 5
if y <= 0:
ydir = 5
r = random.randint(10,255)
g = random.randint(10,255)
b = random.randint(10,255)
x+=xdir
y+=ydir