-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestingTwoAgents.py
More file actions
63 lines (51 loc) · 1.86 KB
/
testingTwoAgents.py
File metadata and controls
63 lines (51 loc) · 1.86 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
import gymnasium as gym
import pygame
import time
# Initialize Pygame
pygame.init()
pygame.display.init()
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Lunar Lander - Two Agents")
# Create the environment
from GymLunarLander import GymLunarLander
env = GymLunarLander(render_mode="human")
raw_env = env.unwrapped # Bypass Gymnasium's wrappers
try:
obs, info = raw_env.reset()
print("Controls:")
print("Agent 0: LEFT=left thruster, UP=main engine, RIGHT=right thruster")
print("Agent 1: A=left, W=up, D=right")
print("Press ESC to quit")
clock = pygame.time.Clock()
running = True
while running:
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False
# Get keyboard state for BOTH agents
keys = pygame.key.get_pressed()
# Agent 0 controls (arrow keys)
action0 = 0 # No action
if keys[pygame.K_LEFT]: action0 = 1
elif keys[pygame.K_UP]: action0 = 2
elif keys[pygame.K_RIGHT]: action0 = 3
elif keys[pygame.K_DOWN]: action0 = 4
# Step the environment with BOTH actions
obs, reward, done, truncated, info = raw_env.step(action0, 0)
print(reward)
obs, reward, done, truncated, info = raw_env.step(0, 1)
obs, reward, done, truncated, info = raw_env.step(0, 2)
obs, reward, done, truncated, info = raw_env.step(0, 3)
raw_env.render()
if done:
print(f"Episode finished! Reward: {reward}")
obs, info = raw_env.reset()
pass
clock.tick(60) # 60 FPS
finally:
raw_env.close()
pygame.quit()