Spaces:
Sleeping
Sleeping
yuh
Browse files- tetris_env.py +41 -50
tetris_env.py
CHANGED
|
@@ -91,45 +91,41 @@ class TetrisEnv(gym.Env):
|
|
| 91 |
return self._get_obs(), reward, done, {}
|
| 92 |
|
| 93 |
def _get_obs(self):
|
| 94 |
-
if self.game.field is None:
|
| 95 |
-
raise ValueError("The field attribute in self.game is None.")
|
| 96 |
"""
|
| 97 |
Get the current state of the game as an observation
|
| 98 |
"""
|
|
|
|
|
|
|
| 99 |
return np.array(self.game.field)
|
| 100 |
|
| 101 |
def render(self, mode='human'):
|
| 102 |
-
|
| 103 |
-
|
| 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
|
|
@@ -137,51 +133,46 @@ class TetrisEnv(gym.Env):
|
|
| 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 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
for
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
pygame.draw.rect(self.screen,
|
| 168 |
-
colors[self.game.
|
| 169 |
rect.inflate(-2, -2))
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
if self.game.figure is not None:
|
| 173 |
-
for i in range(4):
|
| 174 |
-
for j in range(4):
|
| 175 |
-
p = i * 4 + j
|
| 176 |
-
if p in self.game.figure.image():
|
| 177 |
-
rect = pygame.Rect(self.x + self.zoom * (j + self.game.figure.x),
|
| 178 |
-
self.y + self.zoom * (i + self.game.figure.y),
|
| 179 |
-
self.zoom, self.zoom)
|
| 180 |
-
pygame.draw.rect(self.screen,
|
| 181 |
-
colors[self.game.figure.color],
|
| 182 |
-
rect.inflate(-2, -2))
|
| 183 |
-
|
| 184 |
-
pygame.display.flip()
|
| 185 |
|
| 186 |
def close(self):
|
| 187 |
"""
|
|
|
|
| 91 |
return self._get_obs(), reward, done, {}
|
| 92 |
|
| 93 |
def _get_obs(self):
|
|
|
|
|
|
|
| 94 |
"""
|
| 95 |
Get the current state of the game as an observation
|
| 96 |
"""
|
| 97 |
+
if self.game.field is None:
|
| 98 |
+
raise ValueError("The field attribute in self.game 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 |
pygame.draw.rect(self.screen,
|
| 124 |
colors[self.game.field[i][j]],
|
| 125 |
rect.inflate(-2, -2))
|
| 126 |
+
|
| 127 |
# Draw the current figure
|
| 128 |
if self.game.figure is not None:
|
|
|
|
|
|
|
| 129 |
for i in range(4):
|
| 130 |
for j in range(4):
|
| 131 |
p = i * 4 + j
|
|
|
|
| 133 |
rect = pygame.Rect(self.x + self.zoom * (j + self.game.figure.x),
|
| 134 |
self.y + self.zoom * (i + self.game.figure.y),
|
| 135 |
self.zoom, self.zoom)
|
|
|
|
|
|
|
| 136 |
pygame.draw.rect(self.screen,
|
| 137 |
colors[self.game.figure.color],
|
| 138 |
rect.inflate(-2, -2))
|
| 139 |
+
|
| 140 |
# Convert Pygame surface to RGB array
|
|
|
|
|
|
|
| 141 |
return pygame.surfarray.array3d(self.screen)
|
| 142 |
+
|
| 143 |
+
elif mode == 'human':
|
| 144 |
+
if self.screen is None:
|
| 145 |
+
pygame.init()
|
| 146 |
+
size = (self.x * 2 + self.zoom * self.width, self.y * 2 + self.zoom * self.height)
|
| 147 |
+
self.screen = pygame.display.set_mode(size)
|
| 148 |
+
pygame.display.set_caption("Tetris RL")
|
| 149 |
+
|
| 150 |
+
self.screen.fill((173, 216, 230)) # WHITE background
|
| 151 |
+
|
| 152 |
+
# Draw the game field
|
| 153 |
+
for i in range(self.game.height):
|
| 154 |
+
for j in range(self.game.width):
|
| 155 |
+
rect = pygame.Rect(self.x + self.zoom * j, self.y + self.zoom * i, self.zoom, self.zoom)
|
| 156 |
+
pygame.draw.rect(self.screen, (128, 128, 128), rect, 1) # Grid lines
|
| 157 |
+
if self.game.field[i][j] > 0:
|
| 158 |
+
pygame.draw.rect(self.screen,
|
| 159 |
+
colors[self.game.field[i][j]],
|
| 160 |
+
rect.inflate(-2, -2))
|
| 161 |
+
|
| 162 |
+
# Draw the current figure
|
| 163 |
+
if self.game.figure is not None:
|
| 164 |
+
for i in range(4):
|
| 165 |
+
for j in range(4):
|
| 166 |
+
p = i * 4 + j
|
| 167 |
+
if p in self.game.figure.image():
|
| 168 |
+
rect = pygame.Rect(self.x + self.zoom * (j + self.game.figure.x),
|
| 169 |
+
self.y + self.zoom * (i + self.game.figure.y),
|
| 170 |
+
self.zoom, self.zoom)
|
| 171 |
pygame.draw.rect(self.screen,
|
| 172 |
+
colors[self.game.figure.color],
|
| 173 |
rect.inflate(-2, -2))
|
| 174 |
+
|
| 175 |
+
pygame.display.flip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
|
| 177 |
def close(self):
|
| 178 |
"""
|