I have a pygame. in it there are a few nested while loops, running all of them one time accomplish one round, how do I keep rounds going and going, never stop?
Repeat{I have a pygame. in it there are a few nested while loops, running all of them one time accomplish one round, how do I keep rounds going and going, never stop? I have a pygame. in it there are a few nested while loops, running all of them one time accomplish one round, how do I keep rounds going and going, never stop? I have a pygame. in it there are a few nested while loops, running all of them one time accomplish one round, how do I keep rounds going and going, never stop? I have a pygame. in it there are a few nested while loops, running all of them one time accomplish one round, how do I keep rounds going and going, never stop? }Repeat
def running_game():
import pygame
pygame.init()
window = pygame.display.set_mode((640, 480))
robot = pygame.image.load("robot.png")
x = 0
y = 0
velocity = 5
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
window.fill((0, 0, 0))
window.blit(robot, (x, y))
pygame.display.flip()
print(x)
x = velocity
if x robot.get_width() >= 640:
#break
#print(x)
while True:
window.fill((0, 0, 0))
window.blit(robot, (x, y))
pygame.display.flip()
y = velocity
#clock.tick(60)
if y robot.get_height() >= 480:
#break
velocity = -velocity
while True:
window.fill((0, 0, 0))
window.blit(robot, (x, y))
pygame.display.flip()
x = velocity
if x <= 0:
#break
while True:
window.fill((0, 0, 0))
window.blit(robot, (x, y))
pygame.display.flip()
y = velocity
if y <= 0:
velocity = -velocity
break
clock.tick(120)
if __name__ == "__main__":
while True:
running_game()
CodePudding user response:
You have too much nesting there. Because all the inner while
loops never terminate, the program also never gets back to the pygame.event.get()
call, which is why you'll also find that the program doesn't respond to input (including closing the window). There is also a lot of duplicated code.
It's better to keep the robot's current direction in a variable, let's call it direction
, and change the value of that when it reaches the edge. To make the code easier to understand, you can define some constants at the top:
UP = 0
RIGHT = 1
DOWN = 2
LEFT = 3
def running_game():
import pygame
pygame.init()
window = pygame.display.set_mode((640, 480))
robot = pygame.image.load("robot.png")
x = 0
y = 0
velocity = 5
direction = RIGHT
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
window.fill((0, 0, 0))
window.blit(robot, (x, y))
pygame.display.flip()
if direction == UP:
y -= velocity
if y <= 0:
direction = RIGHT
elif direction == RIGHT:
x = velocity
if x robot.get_width() >= 640:
direction = DOWN
elif direction == DOWN:
y = velocity
if y robot.get_height() >= 480:
direction = LEFT
elif direction == LEFT:
x -= velocity
if x <= 0:
direction = UP
clock.tick(120)
if __name__ == "__main__":
while True:
running_game()