-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhuman_play_vizdoom.py
98 lines (81 loc) · 2.74 KB
/
human_play_vizdoom.py
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import gymnasium
from vizdoom import gymnasium_wrapper
from custom_doom import VizDoomCustom
import numpy as np
import pygame
import cv2
# Initialize environment
env = VizDoomCustom(verbose=True)
observation, info = env.reset()
# Initialize pygame
pygame.init()
WINDOW_WIDTH, WINDOW_HEIGHT = 800, 600
window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption("Doom Environment")
# Action mappings
action_map = {
"forward": 3,
"backward": 4,
"look_right": 2,
"look_left": 1,
"fire": 8,
"use": 7,
}
# Set up OpenCV window for additional screen output (if desired)
cv2.namedWindow("screen", cv2.WINDOW_NORMAL)
cv2.resizeWindow("screen", 640, 480)
total_score = 0
# Main game loop
running = True
while running:
# Event handling
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Key press handling
keys = pygame.key.get_pressed()
# Create action array based on key states (one-hot encoded)
if keys[pygame.K_w]: # Forward
current_action = action_map["forward"]
elif keys[pygame.K_s]: # Backward
current_action = action_map["backward"]
elif keys[pygame.K_a] or keys[pygame.K_LEFT]: # Look left
current_action = action_map["look_left"]
elif keys[pygame.K_d] or keys[pygame.K_RIGHT]: # Look right
current_action = action_map["look_right"]
elif keys[pygame.K_SPACE]: # Fire
current_action = action_map["fire"]
elif keys[pygame.K_e]: # use
current_action = action_map["use"]
else:
current_action = 0 # No action
# Apply the action to the environment and update the state
observation, reward, terminated, truncated, info = env.step(current_action)
total_score += reward
# Print reward for debugging
if reward != 0:
print(f"Reward: {reward}")
# Render the updated state (assuming observation["screen"] is the frame)
img = np.array(observation["screen"]).astype(np.uint8)
# Convert NumPy image to Pygame surface and blit it to the screen
surface = pygame.surfarray.make_surface(np.transpose(img, (1, 0, 2)))
surface = pygame.transform.scale(surface, (WINDOW_WIDTH, WINDOW_HEIGHT))
window.blit(surface, (0, 0))
# Display the screen using OpenCV as an option
cv2.imshow("screen", img)
cv2.waitKey(1)
# Update the display
pygame.display.update()
# Limit frame rate to 60 FPS
pygame.time.Clock().tick(60)
# Reset environment if done
if terminated or truncated:
print("Game Over!")
print(f"Final Score:", total_score)
break
observation, info = env.reset()
total_score = 0
# Quit everything properly
# env.close()
pygame.quit()
cv2.destroyAllWindows()