Spaces:
Sleeping
Sleeping
Update tetris_env.py
Browse files- tetris_env.py +56 -56
tetris_env.py
CHANGED
@@ -101,75 +101,35 @@ class TetrisEnv(gym.Env):
|
|
101 |
def render(self, mode='human'):
|
102 |
"""
|
103 |
Render the game state as an RGB array or display it.
|
104 |
-
|
105 |
-
|
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:
|
154 |
pygame.init()
|
155 |
size = (self.x * 2 + self.zoom * self.width, self.y * 2 + self.zoom * self.height)
|
156 |
-
self.screen = pygame.
|
157 |
-
|
158 |
-
|
159 |
self.screen.fill((173, 216, 230)) # WHITE background
|
160 |
-
|
|
|
|
|
|
|
|
|
161 |
# Draw the game field
|
162 |
for i in range(self.game.height):
|
163 |
for j in range(self.game.width):
|
164 |
rect = pygame.Rect(self.x + self.zoom * j, self.y + self.zoom * i, self.zoom, self.zoom)
|
165 |
pygame.draw.rect(self.screen, (128, 128, 128), rect, 1) # Grid lines
|
166 |
if self.game.field[i][j] > 0:
|
|
|
|
|
167 |
pygame.draw.rect(self.screen,
|
168 |
colors[self.game.field[i][j]],
|
169 |
rect.inflate(-2, -2))
|
170 |
-
|
171 |
# Draw the current figure
|
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
|
@@ -177,11 +137,51 @@ class TetrisEnv(gym.Env):
|
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
185 |
|
186 |
def close(self):
|
187 |
"""
|
|
|
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
|
|
|
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:
|
154 |
+
pygame.init()
|
155 |
+
size = (self.x * 2 + self.zoom * self.width, self.y * 2 + self.zoom * self.height)
|
156 |
+
self.screen = pygame.display.set_mode(size)
|
157 |
+
pygame.display.set_caption("Tetris RL")
|
158 |
+
|
159 |
+
self.screen.fill((173, 216, 230)) # WHITE background
|
160 |
+
|
161 |
+
# Draw the game field
|
162 |
+
for i in range(self.game.height):
|
163 |
+
for j in range(self.game.width):
|
164 |
+
rect = pygame.Rect(self.x + self.zoom * j, self.y + self.zoom * i, self.zoom, self.zoom)
|
165 |
+
pygame.draw.rect(self.screen, (128, 128, 128), rect, 1) # Grid lines
|
166 |
+
if self.game.field[i][j] > 0:
|
167 |
+
pygame.draw.rect(self.screen,
|
168 |
+
colors[self.game.field[i][j]],
|
169 |
+
rect.inflate(-2, -2))
|
170 |
+
|
171 |
+
# Draw the current figure
|
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 |
"""
|