Skip to content

Commit

Permalink
Update the CarRacing main method to the new rendering API. (#3085)
Browse files Browse the repository at this point in the history
Additionally, the running application can be stopped not just by
closing the window, but also by pressing the Escape key.

Fixed #3082.
  • Loading branch information
foxik authored Sep 13, 2022
1 parent 05d52cf commit af14749
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions gym/envs/box2d/car_racing.py
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,7 @@ def close(self):
a = np.array([0.0, 0.0, 0.0])

def register_input():
global quit, restart
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
Expand All @@ -782,8 +783,9 @@ def register_input():
if event.key == pygame.K_DOWN:
a[2] = +0.8 # set 1.0 for wheels to block to zero rotation
if event.key == pygame.K_RETURN:
global restart
restart = True
if event.key == pygame.K_ESCAPE:
quit = True

if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
Expand All @@ -795,11 +797,13 @@ def register_input():
if event.key == pygame.K_DOWN:
a[2] = 0

env = CarRacing()
env.render()
if event.type == pygame.QUIT:
quit = True

isopen = True
while isopen:
env = CarRacing(render_mode="human")

quit = False
while not quit:
env.reset()
total_reward = 0.0
steps = 0
Expand All @@ -812,7 +816,6 @@ def register_input():
print("\naction " + str([f"{x:+0.2f}" for x in a]))
print(f"step {steps} total_reward {total_reward:+0.2f}")
steps += 1
isopen = env.render()
if terminated or truncated or restart or isopen is False:
if terminated or truncated or restart or quit:
break
env.close()

0 comments on commit af14749

Please sign in to comment.