Skip to content

Commit e877edc

Browse files
committed
Do not recreate tiles, just set colors and keep a different tile_grid variable to know which is empty and which is not, fix a pretty annoying bug which took a lot of time to figure out, make combos move-based, so you have 3 left before it is removed.
1 parent e7d9d67 commit e877edc

File tree

2 files changed

+71
-70
lines changed

2 files changed

+71
-70
lines changed

game/play.py

Lines changed: 70 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import os, arcade, arcade.gui, random, json, time
1+
import os, arcade, arcade.gui, random, json, time, copy
22

33
from game.sprites import Shape
4-
from utils.constants import SHAPES, CELL_SIZE, ROWS, COLS, OUTLINE_WIDTH, COLORS, COMBO_TIME, button_style
4+
from utils.constants import SHAPES, CELL_SIZE, ROWS, COLS, OUTLINE_WIDTH, COLORS, COMBO_MOVES, button_style
55
from utils.preload import button_texture, button_hovered_texture, click_sound, break_sound
66
class Game(arcade.gui.UIView):
77
def __init__(self, pypresence_client):
@@ -11,21 +11,22 @@ def __init__(self, pypresence_client):
1111

1212
self.pypresence_client = pypresence_client
1313

14-
self.occupied = {}
15-
self.shapes = []
14+
self.tile_grid = {}
15+
self.sprite_grid = {}
1616

1717
self.shape_to_place = random.choice(list(SHAPES.keys()))
1818
self.shape_color = random.choice(COLORS)
19-
2019
self.next_shape_to_place = random.choice(list(SHAPES.keys()))
2120
self.next_shape_color = random.choice(COLORS)
21+
self.shape_data = SHAPES[self.shape_to_place]
2222

23-
self.start_x = self.window.width / 2 - (COLS * (CELL_SIZE + OUTLINE_WIDTH)) / 2 + (CELL_SIZE / 2)
23+
self.start_x = self.window.width / 2 - (COLS * (CELL_SIZE + OUTLINE_WIDTH)) / 2
2424
self.start_y = self.window.height - (ROWS * (CELL_SIZE + OUTLINE_WIDTH)) - (CELL_SIZE / 2)
2525
self.shape_center_x = 0
2626
self.shape_center_y = 0
2727
self.can_place_shape = True
28-
self.empty_grid = {}
28+
self.is_game_over = False
29+
self.combo_moves_left = COMBO_MOVES
2930

3031
if os.path.exists("data.json"):
3132
with open("data.json", "r") as file:
@@ -34,7 +35,6 @@ def __init__(self, pypresence_client):
3435
self.high_score = 0
3536

3637
self.tiles_to_destroy: list[tuple[int, int]] = []
37-
self.tiles_to_destroy_classes: dict[tuple[int, int], arcade.SpriteSolidColor] = {}
3838

3939
self.score = 0
4040
self.combo = 0
@@ -60,7 +60,7 @@ def on_show_view(self):
6060
self.setup_grid()
6161

6262
self.mouse_shape = Shape(0, 0, self.shape_to_place, self.shape_color, self.mouse_shape_list)
63-
self.next_shape_ui = Shape(self.window.width - (CELL_SIZE * 3), self.window.height - (CELL_SIZE * 3), self.next_shape_to_place, self.next_shape_color, self.shape_list)
63+
self.next_shape_ui = Shape(self.window.width - (CELL_SIZE * 4), self.window.height - (CELL_SIZE * 4), self.next_shape_to_place, self.next_shape_color, self.shape_list)
6464

6565
self.score_box = self.anchor.add(arcade.gui.UIBoxLayout(space_between=10, vertical=False), anchor_x="center", anchor_y="top")
6666

@@ -92,11 +92,11 @@ def on_mouse_motion(self, x, y, dx, dy):
9292

9393
self.can_place_shape = True
9494
tile_positions = []
95-
for offset_col, offset_row in SHAPES[self.shape_to_place]:
95+
for offset_col, offset_row in self.shape_data:
9696
tile_col = grid_col + offset_col
9797
tile_row = grid_row + offset_row
9898

99-
if not (0 <= tile_row < ROWS and 0 <= tile_col < COLS) or self.occupied[tile_row][tile_col] or (tile_row, tile_col) in self.tiles_to_destroy:
99+
if not (0 <= tile_row < ROWS and 0 <= tile_col < COLS) or self.tile_grid[tile_row][tile_col] or (tile_row, tile_col) in self.tiles_to_destroy:
100100
self.can_place_shape = False
101101
break
102102

@@ -112,23 +112,23 @@ def on_mouse_motion(self, x, y, dx, dy):
112112

113113
for row in range(ROWS):
114114
for col in range(COLS):
115-
if self.empty_grid[row][col]:
116-
self.empty_grid[row][col].color = (*self.shape_color[:-1], 170) if self.can_place_shape and (row, col) in tile_positions else arcade.color.GRAY
115+
if not self.tile_grid[row][col]:
116+
self.sprite_grid[row][col].color = (*self.shape_color[:-1], 170) if self.can_place_shape and (row, col) in tile_positions else arcade.color.GRAY
117117
else:
118-
self.occupied[row][col].color = (*self.shape_color[:-1], 170) if (row, col) in self.collided_tile_positions else self.occupied[row][col].original_color
118+
self.sprite_grid[row][col].color = (*self.shape_color[:-1], 170) if (row, col) in self.collided_tile_positions else self.sprite_grid[row][col].original_color
119119

120120
self.mouse_shape.update(self.shape_to_place, self.shape_color, x, y)
121121

122122
def setup_grid(self):
123123
for row in range(ROWS):
124-
self.occupied[row] = {}
125-
self.empty_grid[row] = {}
124+
self.tile_grid[row] = {}
125+
self.sprite_grid[row] = {}
126126

127127
for col in range(COLS):
128128
self.create_empty_tile(row, col)
129129

130130
def create_empty_tile(self, row, col):
131-
self.occupied[row][col] = 0
131+
self.tile_grid[row][col] = 0
132132

133133
center_x = self.start_x + col * (CELL_SIZE + OUTLINE_WIDTH)
134134
center_y = self.start_y + row * (CELL_SIZE + OUTLINE_WIDTH)
@@ -141,12 +141,12 @@ def create_empty_tile(self, row, col):
141141
)
142142
self.shape_list.append(tile)
143143

144-
self.empty_grid[row][col] = tile
144+
self.sprite_grid[row][col] = tile
145145

146146
def check_collisions(self, grid_col, grid_row):
147-
modified_grid = {row: {col: (1 if value else 0) for col, value in self.occupied[row].items()} for row in self.occupied}
147+
modified_grid = copy.deepcopy(self.tile_grid)
148148

149-
for offset_col, offset_row in SHAPES[self.shape_to_place]:
149+
for offset_col, offset_row in self.shape_data:
150150
tile_col = grid_col + offset_col
151151
tile_row = grid_row + offset_row
152152
modified_grid[tile_row][tile_col] = 1
@@ -165,111 +165,112 @@ def check_collisions(self, grid_col, grid_row):
165165

166166
return collided_tiles
167167

168-
def update_game(self):
169-
for row, col in self.collided_tile_positions:
170-
self.tiles_to_destroy.append((row, col))
171-
self.tiles_to_destroy_classes[(row, col)] = self.occupied[row][col]
172-
173-
self.score += 25 + (10 * self.combo)
174-
175-
break_sound.play()
176-
177-
self.combo += 1
178-
self.last_combo = time.perf_counter()
179-
168+
def on_update(self, _):
180169
self.score_label.text = f"Score: {self.score}" + (f" Combo: X{self.combo}" if self.combo else "")
181170

182171
if self.score > self.high_score:
183172
self.high_score = self.score
184173
self.high_score_label.text = f"High Score: {self.high_score}"
185174

186-
self.check_game_over()
187-
188-
def on_update(self, _):
189-
if time.perf_counter() - self.last_combo >= COMBO_TIME:
190-
self.combo = 0
191-
self.score_label.text = f"Score: {self.score}"
192-
193175
for row, col in self.tiles_to_destroy[:]:
194-
tile = self.tiles_to_destroy_classes.get((row, col))
176+
tile = self.sprite_grid[row][col]
195177
if not tile:
196178
self.tiles_to_destroy.remove((row, col))
197-
self.tiles_to_destroy_classes.pop((row, col), None)
198179
continue
199180

200181
tile.scale = (tile.scale_x - 0.05, tile.scale_y - 0.05)
201182

202183
if tile.scale_x <= 0.05:
184+
tile.color = arcade.color.GRAY
185+
tile.original_color = arcade.color.GRAY
186+
tile.scale = (1, 1)
187+
self.tile_grid[row][col] = 0
203188
self.tiles_to_destroy.remove((row, col))
204-
self.tiles_to_destroy_classes.pop((row, col))
205-
tile.remove_from_sprite_lists()
206-
del tile
207-
208-
self.create_empty_tile(row, col)
209189

210190
def check_game_over(self):
211191
for grid_row in range(ROWS):
212192
for grid_col in range(COLS):
213193
can_place = True
214194

215-
for offset_col, offset_row in SHAPES[self.shape_to_place]:
195+
for offset_col, offset_row in self.shape_data:
216196
tile_col = grid_col + offset_col
217197
tile_row = grid_row + offset_row
218198

219-
if not (tile_row, tile_col) in self.tiles_to_destroy and (not (0 <= tile_row < ROWS and 0 <= tile_col < COLS) or self.occupied[tile_row][tile_col]):
199+
if not (0 <= tile_row < ROWS and 0 <= tile_col < COLS):
220200
can_place = False
201+
break
202+
203+
if (tile_row, tile_col) in self.tiles_to_destroy:
204+
continue
205+
206+
if self.tile_grid[tile_row][tile_col]:
207+
can_place = False
208+
break
221209

222210
if can_place:
223211
return
224212

225-
self.game_over_window = self.anchor.add(arcade.gui.UIBoxLayout(), anchor_x="center", anchor_y="center")
226-
self.game_over_window._bg_color = arcade.color.BLACK
227-
self.game_over_label = self.game_over_window.add(arcade.gui.UILabel(text="GAME OVER", font_name="Roboto", font_size=80, text_color=arcade.color.WHITE))
213+
self.is_game_over = True
214+
215+
self.game_over_box = self.anchor.add(arcade.gui.UIBoxLayout(), anchor_x="center", anchor_y="center")
216+
self.game_over_box._bg_color = arcade.color.BLACK
217+
218+
self.game_over_label = self.game_over_box.add(arcade.gui.UILabel(text="GAME OVER", font_name="Roboto", font_size=80, text_color=arcade.color.WHITE))
228219

229220
self.mouse_shape_list.clear()
230221

231222
self.window.set_mouse_visible(True)
232223

233224
def on_key_press(self, symbol: int, modifiers: int) -> bool | None:
234-
super().on_key_press(symbol, modifiers)
235225
if symbol == arcade.key.ESCAPE:
236226
self.main_exit()
237227

238228
def on_mouse_press(self, x: int, y: int, button: int, modifiers: int) -> bool | None:
239-
super().on_mouse_press(x, y, button, modifiers)
240-
241229
grid_col = int((x - self.start_x + (CELL_SIZE / 2)) // (CELL_SIZE + OUTLINE_WIDTH))
242230
grid_row = int((y - self.start_y + (CELL_SIZE / 2)) // (CELL_SIZE + OUTLINE_WIDTH))
243231

244232
if self.can_place_shape:
245233
if self.settings_dict.get("sfx", True):
246234
click_sound.play(volume=self.settings_dict.get("sfx_volume", 50) / 100)
247235

248-
shape = Shape(self.shape_center_x, self.shape_center_y, self.shape_to_place, self.shape_color, self.shape_list)
249-
self.shapes.append(shape)
250-
251-
n = 0
252-
253-
for offset_col, offset_row in SHAPES[self.shape_to_place]:
236+
for offset_col, offset_row in self.shape_data:
254237
tile_col = grid_col + offset_col
255238
tile_row = grid_row + offset_row
256-
self.occupied[tile_row][tile_col] = shape.tiles[n]
257-
self.occupied[tile_row][tile_col].original_color = shape.shape_color
258-
self.shape_list.remove(self.empty_grid[tile_row][tile_col])
259-
self.empty_grid[tile_row][tile_col] = None
260239

261-
n += 1
240+
self.tile_grid[tile_row][tile_col] = 1
241+
self.sprite_grid[tile_row][tile_col].color = self.shape_color
242+
self.sprite_grid[tile_row][tile_col].original_color = self.shape_color
262243

263244
self.score += 5
264245

246+
for row, col in self.collided_tile_positions:
247+
self.tiles_to_destroy.append((row, col))
248+
249+
self.score += 25 + (10 * self.combo)
250+
251+
break_sound.play()
252+
253+
if self.collided_tile_positions:
254+
self.combo += 1
255+
self.combo_moves_left = COMBO_MOVES
256+
self.last_combo = time.perf_counter()
257+
else:
258+
self.combo_moves_left -= 1
259+
260+
if self.combo_moves_left == 0:
261+
self.combo = 0
262+
265263
self.shape_to_place = self.next_shape_to_place
266264
self.shape_color = self.next_shape_color
265+
self.shape_data = SHAPES[self.shape_to_place]
267266

268-
self.next_shape_to_place = random.choice(list(SHAPES.keys()))
269-
self.next_shape_color = random.choice(COLORS)
270-
self.next_shape_ui.update(self.next_shape_to_place, self.next_shape_color)
267+
if not self.is_game_over:
268+
self.check_game_over()
271269

272-
self.update_game()
270+
if not self.is_game_over: # This check makes sure to not re-create the next shape if its game over. This confused me, cause the shape it showed could have been placed.
271+
self.next_shape_to_place = random.choice(list(SHAPES.keys()))
272+
self.next_shape_color = random.choice(COLORS)
273+
self.next_shape_ui.update(self.next_shape_to_place, self.next_shape_color)
273274

274275
def on_draw(self):
275276
self.window.clear()

utils/constants.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
log_dir = 'logs'
88
discord_presence_id = 1360953272843632680
99

10-
COMBO_TIME = 5
10+
COMBO_MOVES = 3
1111
CELL_SIZE = 80
1212
ROWS = 8
1313
COLS = 8

0 commit comments

Comments
 (0)