Spaces:
Sleeping
Sleeping
Update tetris_env.py
Browse files- tetris_env.py +43 -30
tetris_env.py
CHANGED
@@ -99,42 +99,55 @@ class TetrisEnv(gym.Env):
|
|
99 |
return np.array(self.game.field)
|
100 |
|
101 |
def render(self, mode='human'):
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
|
111 |
-
|
112 |
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
119 |
pygame.draw.rect(self.screen,
|
120 |
-
colors[self.game.
|
121 |
rect.inflate(-2, -2))
|
122 |
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
p = i * 4 + j
|
128 |
-
if p in self.game.figure.image():
|
129 |
-
rect = pygame.Rect(self.x + self.zoom * (j + self.game.figure.x),
|
130 |
-
self.y + self.zoom * (i + self.game.figure.y),
|
131 |
-
self.zoom, self.zoom)
|
132 |
-
pygame.draw.rect(self.screen,
|
133 |
-
colors[self.game.figure.color],
|
134 |
-
rect.inflate(-2, -2))
|
135 |
|
136 |
-
# Convert Pygame surface to RGB array
|
137 |
-
return pygame.surfarray.array3d(self.screen)
|
138 |
|
139 |
elif mode == 'human':
|
140 |
if self.screen is None:
|
|
|
99 |
return np.array(self.game.field)
|
100 |
|
101 |
def render(self, mode='human'):
|
102 |
+
"""
|
103 |
+
Render the game state as an RGB array or display it.
|
104 |
+
"""
|
105 |
+
if mode == 'rgb_array':
|
106 |
+
if self.screen is None:
|
107 |
+
pygame.init()
|
108 |
+
size = (self.x * 2 + self.zoom * self.width, self.y * 2 + self.zoom * self.height)
|
109 |
+
self.screen = pygame.Surface(size)
|
110 |
|
111 |
+
self.screen.fill((173, 216, 230)) # WHITE background
|
112 |
|
113 |
+
# Check if the game field is initialized
|
114 |
+
if self.game.field is None:
|
115 |
+
raise ValueError("Game field is None.")
|
116 |
+
|
117 |
+
# Draw the game field
|
118 |
+
for i in range(self.game.height):
|
119 |
+
for j in range(self.game.width):
|
120 |
+
rect = pygame.Rect(self.x + self.zoom * j, self.y + self.zoom * i, self.zoom, self.zoom)
|
121 |
+
pygame.draw.rect(self.screen, (128, 128, 128), rect, 1) # Grid lines
|
122 |
+
if self.game.field[i][j] > 0:
|
123 |
+
if self.game.field[i][j] not in colors:
|
124 |
+
raise ValueError(f"Color for field value {self.game.field[i][j]} is not defined.")
|
125 |
+
pygame.draw.rect(self.screen,
|
126 |
+
colors[self.game.field[i][j]],
|
127 |
+
rect.inflate(-2, -2))
|
128 |
+
|
129 |
+
# Draw the current figure
|
130 |
+
if self.game.figure is not None:
|
131 |
+
if self.game.figure.image() is None:
|
132 |
+
raise ValueError("Figure's image is None.")
|
133 |
+
for i in range(4):
|
134 |
+
for j in range(4):
|
135 |
+
p = i * 4 + j
|
136 |
+
if p in self.game.figure.image():
|
137 |
+
rect = pygame.Rect(self.x + self.zoom * (j + self.game.figure.x),
|
138 |
+
self.y + self.zoom * (i + self.game.figure.y),
|
139 |
+
self.zoom, self.zoom)
|
140 |
+
if self.game.figure.color not in colors:
|
141 |
+
raise ValueError(f"Color for figure value {self.game.figure.color} is not defined.")
|
142 |
pygame.draw.rect(self.screen,
|
143 |
+
colors[self.game.figure.color],
|
144 |
rect.inflate(-2, -2))
|
145 |
|
146 |
+
# Convert Pygame surface to RGB array
|
147 |
+
if self.screen is None:
|
148 |
+
raise ValueError("Screen is None when trying to create RGB array.")
|
149 |
+
return pygame.surfarray.array3d(self.screen)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
150 |
|
|
|
|
|
151 |
|
152 |
elif mode == 'human':
|
153 |
if self.screen is None:
|