mirror of
https://github.com/csd4ni3l/shatterstack.git
synced 2026-01-01 12:33:48 +01:00
move some code around and remove bloat
This commit is contained in:
100
game/play.py
100
game/play.py
@@ -103,16 +103,16 @@ class Game(arcade.gui.UIView):
|
|||||||
self.shape_center_y = self.start_y + grid_row * (CELL_SIZE + OUTLINE_WIDTH)
|
self.shape_center_y = self.start_y + grid_row * (CELL_SIZE + OUTLINE_WIDTH)
|
||||||
|
|
||||||
if self.can_place_shape:
|
if self.can_place_shape:
|
||||||
collided_tile_positions = self.check_collisions_before(grid_col, grid_row)
|
self.collided_tile_positions = self.check_collisions(grid_col, grid_row)
|
||||||
else:
|
else:
|
||||||
collided_tile_positions = []
|
self.collided_tile_positions = []
|
||||||
|
|
||||||
for row in range(ROWS):
|
for row in range(ROWS):
|
||||||
for col in range(COLS):
|
for col in range(COLS):
|
||||||
if self.empty_grid[row][col]:
|
if self.empty_grid[row][col]:
|
||||||
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
|
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
|
||||||
else:
|
else:
|
||||||
self.occupied[row][col].color = (*self.shape_color[:-1], 170) if (row, col) in collided_tile_positions else self.occupied[row][col].original_color
|
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
|
||||||
|
|
||||||
self.mouse_shape.update(self.shape_to_place, self.shape_color, x, y)
|
self.mouse_shape.update(self.shape_to_place, self.shape_color, x, y)
|
||||||
|
|
||||||
@@ -122,22 +122,25 @@ class Game(arcade.gui.UIView):
|
|||||||
self.empty_grid[row] = {}
|
self.empty_grid[row] = {}
|
||||||
|
|
||||||
for col in range(COLS):
|
for col in range(COLS):
|
||||||
self.occupied[row][col] = 0
|
self.create_empty_tile(row, col)
|
||||||
|
|
||||||
center_x = self.start_x + col * (CELL_SIZE + OUTLINE_WIDTH)
|
def create_empty_tile(self, row, col):
|
||||||
center_y = self.start_y + row * (CELL_SIZE + OUTLINE_WIDTH)
|
self.occupied[row][col] = 0
|
||||||
tile = arcade.SpriteSolidColor(
|
|
||||||
width=CELL_SIZE,
|
|
||||||
height=CELL_SIZE,
|
|
||||||
color=arcade.color.GRAY,
|
|
||||||
center_x=center_x,
|
|
||||||
center_y=center_y
|
|
||||||
)
|
|
||||||
self.shape_list.append(tile)
|
|
||||||
|
|
||||||
self.empty_grid[row][col] = tile
|
center_x = self.start_x + col * (CELL_SIZE + OUTLINE_WIDTH)
|
||||||
|
center_y = self.start_y + row * (CELL_SIZE + OUTLINE_WIDTH)
|
||||||
|
tile = arcade.SpriteSolidColor(
|
||||||
|
width=CELL_SIZE,
|
||||||
|
height=CELL_SIZE,
|
||||||
|
color=arcade.color.GRAY,
|
||||||
|
center_x=center_x,
|
||||||
|
center_y=center_y
|
||||||
|
)
|
||||||
|
self.shape_list.append(tile)
|
||||||
|
|
||||||
def check_collisions_before(self, grid_col, grid_row):
|
self.empty_grid[row][col] = tile
|
||||||
|
|
||||||
|
def check_collisions(self, grid_col, grid_row):
|
||||||
modified_grid = {row: {col: (1 if value else 0) for col, value in self.occupied[row].items()} for row in self.occupied}
|
modified_grid = {row: {col: (1 if value else 0) for col, value in self.occupied[row].items()} for row in self.occupied}
|
||||||
|
|
||||||
for offset_col, offset_row in SHAPES[self.shape_to_place]:
|
for offset_col, offset_row in SHAPES[self.shape_to_place]:
|
||||||
@@ -159,61 +162,18 @@ class Game(arcade.gui.UIView):
|
|||||||
|
|
||||||
return collided_tiles
|
return collided_tiles
|
||||||
|
|
||||||
def check_collisions(self):
|
|
||||||
for row_idx, row in self.occupied.items():
|
|
||||||
if all(row.values()):
|
|
||||||
for tile in row.values():
|
|
||||||
self.shape_list.remove(tile)
|
|
||||||
|
|
||||||
for col in range(COLS):
|
|
||||||
self.occupied[row_idx][col] = 0
|
|
||||||
center_x = self.start_x + col * (CELL_SIZE + OUTLINE_WIDTH)
|
|
||||||
center_y = self.start_y + row_idx * (CELL_SIZE + OUTLINE_WIDTH)
|
|
||||||
tile = arcade.SpriteSolidColor(
|
|
||||||
width=CELL_SIZE,
|
|
||||||
height=CELL_SIZE,
|
|
||||||
color=arcade.color.GRAY,
|
|
||||||
center_x=center_x,
|
|
||||||
center_y=center_y
|
|
||||||
)
|
|
||||||
self.shape_list.append(tile)
|
|
||||||
self.empty_grid[row_idx][col] = tile
|
|
||||||
|
|
||||||
self.score += 25 + (10 * self.combo)
|
|
||||||
|
|
||||||
break_sound.play()
|
|
||||||
|
|
||||||
self.combo += 1
|
|
||||||
self.last_combo = time.perf_counter()
|
|
||||||
|
|
||||||
for col in range(COLS):
|
|
||||||
column = [row[col] for row in self.occupied.values()]
|
|
||||||
if all(column):
|
|
||||||
for tile in column:
|
|
||||||
self.shape_list.remove(tile)
|
|
||||||
|
|
||||||
for row_idx in range(ROWS):
|
|
||||||
self.occupied[row_idx][col] = 0
|
|
||||||
center_x = self.start_x + col * (CELL_SIZE + OUTLINE_WIDTH)
|
|
||||||
center_y = self.start_y + row_idx * (CELL_SIZE + OUTLINE_WIDTH)
|
|
||||||
tile = arcade.SpriteSolidColor(
|
|
||||||
width=CELL_SIZE,
|
|
||||||
height=CELL_SIZE,
|
|
||||||
color=arcade.color.GRAY,
|
|
||||||
center_x=center_x,
|
|
||||||
center_y=center_y
|
|
||||||
)
|
|
||||||
self.shape_list.append(tile)
|
|
||||||
self.empty_grid[row_idx][col] = tile
|
|
||||||
|
|
||||||
self.score += 25 + (10 * self.combo)
|
|
||||||
|
|
||||||
break_sound.play()
|
|
||||||
self.combo += 1
|
|
||||||
self.last_combo = time.perf_counter()
|
|
||||||
|
|
||||||
def update_game(self):
|
def update_game(self):
|
||||||
self.check_collisions()
|
for row, col in self.collided_tile_positions:
|
||||||
|
self.shape_list.remove(self.occupied[row][col])
|
||||||
|
|
||||||
|
self.create_empty_tile(row, col)
|
||||||
|
|
||||||
|
self.score += 25 + (10 * self.combo)
|
||||||
|
|
||||||
|
break_sound.play()
|
||||||
|
|
||||||
|
self.combo += 1
|
||||||
|
self.last_combo = time.perf_counter()
|
||||||
|
|
||||||
self.score_label.text = f"Score: {self.score}" + (f" Combo: X{self.combo}" if self.combo else "")
|
self.score_label.text = f"Score: {self.score}" + (f" Combo: X{self.combo}" if self.combo else "")
|
||||||
|
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ CELL_SIZE = 80
|
|||||||
ROWS = 8
|
ROWS = 8
|
||||||
COLS = 8
|
COLS = 8
|
||||||
OUTLINE_WIDTH = 2
|
OUTLINE_WIDTH = 2
|
||||||
|
|
||||||
SHAPES = {
|
SHAPES = {
|
||||||
"I": [(0, 0), (1, 0), (2, 0), (3, 0)],
|
"I": [(0, 0), (1, 0), (2, 0), (3, 0)],
|
||||||
"I_R1": [(0, 0), (0, 1), (0, 2), (0, 3)],
|
"I_R1": [(0, 0), (0, 1), (0, 2), (0, 3)],
|
||||||
|
|||||||
Reference in New Issue
Block a user