-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame_view.py
More file actions
116 lines (81 loc) · 3.05 KB
/
game_view.py
File metadata and controls
116 lines (81 loc) · 3.05 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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import pygame
import random
class GameView:
def __init__(self, controller):
self.controller = controller
pygame.init()
self.width = 600
self.height = 600
self.screen = pygame.display.set_mode((self.width, self.height))
pygame.display.set_caption("AI System Live View")
self.clock = pygame.time.Clock()
self.grid_size = 10
self.cell_size = self.width // self.grid_size
self.agent_pos = [5, 5]
# =====================================================
# DRAW GRID
# =====================================================
def draw_grid(self):
for x in range(self.grid_size):
for y in range(self.grid_size):
rect = pygame.Rect(
x * self.cell_size,
y * self.cell_size,
self.cell_size,
self.cell_size
)
pygame.draw.rect(self.screen, (30, 30, 30), rect, 1)
# =====================================================
# DRAW AGENT
# =====================================================
def draw_agent(self):
rect = pygame.Rect(
self.agent_pos[0] * self.cell_size,
self.agent_pos[1] * self.cell_size,
self.cell_size,
self.cell_size
)
pygame.draw.rect(self.screen, (0, 200, 255), rect)
# =====================================================
# UPDATE FROM AI SYSTEM
# =====================================================
def update_from_system(self, output):
# crude mapping: reward drives movement
reward = sum(output["results"].values())
if reward > 0.6:
self.agent_pos[0] = min(self.grid_size - 1, self.agent_pos[0] + 1)
elif reward < 0.3:
self.agent_pos[1] = min(self.grid_size - 1, self.agent_pos[1] + 1)
else:
self.agent_pos[0] = max(0, self.agent_pos[0] - 1)
# =====================================================
# MAIN LOOP
# =====================================================
def run(self):
running = True
step = 0
while running:
self.screen.fill((0, 0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# -----------------------------
# RUN AI STEP
# -----------------------------
output = self.controller.run_cycle_live(
goal="visual_test",
input_text="observe"
)
# -----------------------------
# UPDATE VISUAL STATE
# -----------------------------
self.update_from_system(output)
# -----------------------------
# DRAW WORLD
# -----------------------------
self.draw_grid()
self.draw_agent()
pygame.display.flip()
self.clock.tick(2) # slow so you can see steps
step += 1
pygame.quit()