-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhuman_play.py
More file actions
70 lines (59 loc) · 2.14 KB
/
Copy pathhuman_play.py
File metadata and controls
70 lines (59 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import os
import time
from frogger_env import FroggerEnv
def clear_screen():
os.system('clear' if os.name != 'nt' else 'cls')
def human_play(use_ascii=True):
"""Human player mode with real-time game updates. Renders by step / waits for user input; doesn't render continuously."""
env = FroggerEnv()
episode = 0
while True:
# Reset environment
state = env.reset()
episode += 1
done = False
total_reward = 0.0
while not done:
clear_screen()
print(f"Episode {episode}")
env.render(use_ascii=use_ascii)
print("Controls: w=up, s=down, a=left, d=right, q=quit")
action_key = input("Your move: ").strip().lower()
if action_key == 'q':
print("Quitting.")
return
# Map keys to actions
if action_key == 'w':
action = 0 # UP
elif action_key == 's':
action = 1 # DOWN
elif action_key == 'a':
action = 2 # LEFT
elif action_key == 'd':
action = 3 # RIGHT
elif action_key == 'e' or action_key == '':
action = 4 # STAY
else:
# Invalid key, so just treat as STAY
action = 4
_, reward, done, _ = env.step(action)
total_reward += reward
clear_screen()
env.render(use_ascii=use_ascii)
print(f"Last reward: {reward:.2f} Total reward: {total_reward:.2f}")
time.sleep(0.2)
if done:
if reward > 0:
print("You reached the goal!")
elif reward < 0:
print("You got hit by a car...")
else:
print("Episode ended (step limit).")
print(f"Episode return: {total_reward:.2f}")
cont = input("Play another episode? (y/n): ").strip().lower()
if cont != 'y':
print("Bye!")
return
break
if __name__ == "__main__":
human_play(use_ascii=True)