Skip to content

Commit 3686b86

Browse files
committed
move some code around and remove bloat
1 parent fbf4102 commit 3686b86

File tree

2 files changed

+32
-71
lines changed

2 files changed

+32
-71
lines changed

game/play.py

Lines changed: 31 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -103,16 +103,16 @@ def on_mouse_motion(self, x, y, dx, dy):
103103
self.shape_center_y = self.start_y + grid_row * (CELL_SIZE + OUTLINE_WIDTH)
104104

105105
if self.can_place_shape:
106-
collided_tile_positions = self.check_collisions_before(grid_col, grid_row)
106+
self.collided_tile_positions = self.check_collisions(grid_col, grid_row)
107107
else:
108-
collided_tile_positions = []
108+
self.collided_tile_positions = []
109109

110110
for row in range(ROWS):
111111
for col in range(COLS):
112112
if self.empty_grid[row][col]:
113113
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
114114
else:
115-
self.occupied[row][col].color = (*self.shape_color[:-1], 170) if (row, col) in collided_tile_positions else self.occupied[row][col].original_color
115+
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
116116

117117
self.mouse_shape.update(self.shape_to_place, self.shape_color, x, y)
118118

@@ -122,22 +122,25 @@ def setup_grid(self):
122122
self.empty_grid[row] = {}
123123

124124
for col in range(COLS):
125-
self.occupied[row][col] = 0
126-
127-
center_x = self.start_x + col * (CELL_SIZE + OUTLINE_WIDTH)
128-
center_y = self.start_y + row * (CELL_SIZE + OUTLINE_WIDTH)
129-
tile = arcade.SpriteSolidColor(
130-
width=CELL_SIZE,
131-
height=CELL_SIZE,
132-
color=arcade.color.GRAY,
133-
center_x=center_x,
134-
center_y=center_y
135-
)
136-
self.shape_list.append(tile)
137-
138-
self.empty_grid[row][col] = tile
139-
140-
def check_collisions_before(self, grid_col, grid_row):
125+
self.create_empty_tile(row, col)
126+
127+
def create_empty_tile(self, row, col):
128+
self.occupied[row][col] = 0
129+
130+
center_x = self.start_x + col * (CELL_SIZE + OUTLINE_WIDTH)
131+
center_y = self.start_y + row * (CELL_SIZE + OUTLINE_WIDTH)
132+
tile = arcade.SpriteSolidColor(
133+
width=CELL_SIZE,
134+
height=CELL_SIZE,
135+
color=arcade.color.GRAY,
136+
center_x=center_x,
137+
center_y=center_y
138+
)
139+
self.shape_list.append(tile)
140+
141+
self.empty_grid[row][col] = tile
142+
143+
def check_collisions(self, grid_col, grid_row):
141144
modified_grid = {row: {col: (1 if value else 0) for col, value in self.occupied[row].items()} for row in self.occupied}
142145

143146
for offset_col, offset_row in SHAPES[self.shape_to_place]:
@@ -158,62 +161,19 @@ def check_collisions_before(self, grid_col, grid_row):
158161
collided_tiles.append((row, col))
159162

160163
return collided_tiles
161-
162-
def check_collisions(self):
163-
for row_idx, row in self.occupied.items():
164-
if all(row.values()):
165-
for tile in row.values():
166-
self.shape_list.remove(tile)
167164

168-
for col in range(COLS):
169-
self.occupied[row_idx][col] = 0
170-
center_x = self.start_x + col * (CELL_SIZE + OUTLINE_WIDTH)
171-
center_y = self.start_y + row_idx * (CELL_SIZE + OUTLINE_WIDTH)
172-
tile = arcade.SpriteSolidColor(
173-
width=CELL_SIZE,
174-
height=CELL_SIZE,
175-
color=arcade.color.GRAY,
176-
center_x=center_x,
177-
center_y=center_y
178-
)
179-
self.shape_list.append(tile)
180-
self.empty_grid[row_idx][col] = tile
181-
182-
self.score += 25 + (10 * self.combo)
165+
def update_game(self):
166+
for row, col in self.collided_tile_positions:
167+
self.shape_list.remove(self.occupied[row][col])
183168

184-
break_sound.play()
185-
186-
self.combo += 1
187-
self.last_combo = time.perf_counter()
169+
self.create_empty_tile(row, col)
188170

189-
for col in range(COLS):
190-
column = [row[col] for row in self.occupied.values()]
191-
if all(column):
192-
for tile in column:
193-
self.shape_list.remove(tile)
194-
195-
for row_idx in range(ROWS):
196-
self.occupied[row_idx][col] = 0
197-
center_x = self.start_x + col * (CELL_SIZE + OUTLINE_WIDTH)
198-
center_y = self.start_y + row_idx * (CELL_SIZE + OUTLINE_WIDTH)
199-
tile = arcade.SpriteSolidColor(
200-
width=CELL_SIZE,
201-
height=CELL_SIZE,
202-
color=arcade.color.GRAY,
203-
center_x=center_x,
204-
center_y=center_y
205-
)
206-
self.shape_list.append(tile)
207-
self.empty_grid[row_idx][col] = tile
208-
209-
self.score += 25 + (10 * self.combo)
210-
211-
break_sound.play()
212-
self.combo += 1
213-
self.last_combo = time.perf_counter()
171+
self.score += 25 + (10 * self.combo)
172+
173+
break_sound.play()
214174

215-
def update_game(self):
216-
self.check_collisions()
175+
self.combo += 1
176+
self.last_combo = time.perf_counter()
217177

218178
self.score_label.text = f"Score: {self.score}" + (f" Combo: X{self.combo}" if self.combo else "")
219179

utils/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
ROWS = 8
1313
COLS = 8
1414
OUTLINE_WIDTH = 2
15+
1516
SHAPES = {
1617
"I": [(0, 0), (1, 0), (2, 0), (3, 0)],
1718
"I_R1": [(0, 0), (0, 1), (0, 2), (0, 3)],

0 commit comments

Comments
 (0)