mirror of
https://github.com/csd4ni3l/shatterstack.git
synced 2025-11-05 05:58:18 +01:00
Add tile destroy animations
This commit is contained in:
51
game/play.py
51
game/play.py
@@ -1,4 +1,4 @@
|
||||
import os, arcade, arcade.gui, random, json, time, copy
|
||||
import os, arcade, arcade.gui, random, json, time
|
||||
|
||||
from game.sprites import Shape
|
||||
from utils.constants import SHAPES, CELL_SIZE, ROWS, COLS, OUTLINE_WIDTH, COLORS, COMBO_TIME, button_style
|
||||
@@ -33,6 +33,9 @@ class Game(arcade.gui.UIView):
|
||||
else:
|
||||
self.high_score = 0
|
||||
|
||||
self.tiles_to_destroy: list[tuple[int, int]] = []
|
||||
self.tiles_to_destroy_classes: dict[tuple[int, int], arcade.SpriteSolidColor] = {}
|
||||
|
||||
self.score = 0
|
||||
self.combo = 0
|
||||
self.last_combo = time.perf_counter()
|
||||
@@ -67,7 +70,7 @@ class Game(arcade.gui.UIView):
|
||||
self.back_button = arcade.gui.UITextureButton(texture=button_texture, texture_hovered=button_hovered_texture, text='<--', style=button_style, width=100, height=50)
|
||||
self.back_button.on_click = lambda e: self.main_exit()
|
||||
self.anchor.add(self.back_button, anchor_x="left", anchor_y="top", align_x=5, align_y=-5)
|
||||
|
||||
|
||||
self.pypresence_client.update(state='In Game', details='Shattering Stacks', start=self.pypresence_client.start_time)
|
||||
|
||||
self.window.set_mouse_visible(False)
|
||||
@@ -93,7 +96,7 @@ class Game(arcade.gui.UIView):
|
||||
tile_col = grid_col + offset_col
|
||||
tile_row = grid_row + offset_row
|
||||
|
||||
if not (0 <= tile_row < ROWS and 0 <= tile_col < COLS) or self.occupied[tile_row][tile_col]:
|
||||
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:
|
||||
self.can_place_shape = False
|
||||
break
|
||||
|
||||
@@ -101,7 +104,7 @@ class Game(arcade.gui.UIView):
|
||||
|
||||
self.shape_center_x = self.start_x + grid_col * (CELL_SIZE + OUTLINE_WIDTH)
|
||||
self.shape_center_y = self.start_y + grid_row * (CELL_SIZE + OUTLINE_WIDTH)
|
||||
|
||||
|
||||
if self.can_place_shape:
|
||||
self.collided_tile_positions = self.check_collisions(grid_col, grid_row)
|
||||
else:
|
||||
@@ -164,38 +167,56 @@ class Game(arcade.gui.UIView):
|
||||
|
||||
def update_game(self):
|
||||
for row, col in self.collided_tile_positions:
|
||||
self.shape_list.remove(self.occupied[row][col])
|
||||
|
||||
self.create_empty_tile(row, col)
|
||||
self.tiles_to_destroy.append((row, col))
|
||||
self.tiles_to_destroy_classes[(row, col)] = self.occupied[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 "")
|
||||
|
||||
|
||||
if self.score > self.high_score:
|
||||
self.high_score = self.score
|
||||
self.high_score_label.text = f"High Score: {self.high_score}"
|
||||
|
||||
self.check_game_over()
|
||||
|
||||
def on_update(self, _):
|
||||
if time.perf_counter() - self.last_combo >= COMBO_TIME:
|
||||
self.combo = 0
|
||||
self.score_label.text = f"Score: {self.score}"
|
||||
|
||||
self.check_game_over()
|
||||
for row, col in self.tiles_to_destroy[:]:
|
||||
tile = self.tiles_to_destroy_classes.get((row, col))
|
||||
if not tile:
|
||||
self.tiles_to_destroy.remove((row, col))
|
||||
self.tiles_to_destroy_classes.pop((row, col), None)
|
||||
continue
|
||||
|
||||
tile.scale = (tile.scale_x - 0.05, tile.scale_y - 0.05)
|
||||
|
||||
if tile.scale_x <= 0.05:
|
||||
self.tiles_to_destroy.remove((row, col))
|
||||
self.tiles_to_destroy_classes.pop((row, col))
|
||||
tile.remove_from_sprite_lists()
|
||||
del tile
|
||||
|
||||
self.create_empty_tile(row, col)
|
||||
|
||||
def check_game_over(self):
|
||||
for grid_row in range(ROWS):
|
||||
for grid_col in range(COLS):
|
||||
can_place = True
|
||||
|
||||
|
||||
for offset_col, offset_row in SHAPES[self.shape_to_place]:
|
||||
tile_col = grid_col + offset_col
|
||||
tile_row = grid_row + offset_row
|
||||
|
||||
if not (0 <= tile_row < ROWS and 0 <= tile_col < COLS) or self.occupied[tile_row][tile_col]:
|
||||
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]):
|
||||
can_place = False
|
||||
|
||||
if can_place:
|
||||
@@ -223,7 +244,7 @@ class Game(arcade.gui.UIView):
|
||||
if self.can_place_shape:
|
||||
if self.settings_dict.get("sfx", True):
|
||||
click_sound.play(volume=self.settings_dict.get("sfx_volume", 50) / 100)
|
||||
|
||||
|
||||
shape = Shape(self.shape_center_x, self.shape_center_y, self.shape_to_place, self.shape_color, self.shape_list)
|
||||
self.shapes.append(shape)
|
||||
|
||||
@@ -254,4 +275,4 @@ class Game(arcade.gui.UIView):
|
||||
self.window.clear()
|
||||
self.shape_list.draw()
|
||||
self.mouse_shape_list.draw()
|
||||
self.ui.draw()
|
||||
self.ui.draw()
|
||||
|
||||
6
uv.lock
generated
6
uv.lock
generated
@@ -212,9 +212,9 @@ requires-dist = [
|
||||
|
||||
[[package]]
|
||||
name = "typing-extensions"
|
||||
version = "4.13.2"
|
||||
version = "4.14.0"
|
||||
source = { registry = "https://pypi.org/simple" }
|
||||
sdist = { url = "https://files.pythonhosted.org/packages/f6/37/23083fcd6e35492953e8d2aaaa68b860eb422b34627b13f2ce3eb6106061/typing_extensions-4.13.2.tar.gz", hash = "sha256:e6c81219bd689f51865d9e372991c540bda33a0379d5573cddb9a3a23f7caaef", size = 106967, upload_time = "2025-04-10T14:19:05.416Z" }
|
||||
sdist = { url = "https://files.pythonhosted.org/packages/d1/bc/51647cd02527e87d05cb083ccc402f93e441606ff1f01739a62c8ad09ba5/typing_extensions-4.14.0.tar.gz", hash = "sha256:8676b788e32f02ab42d9e7c61324048ae4c6d844a399eebace3d4979d75ceef4", size = 107423, upload_time = "2025-06-02T14:52:11.399Z" }
|
||||
wheels = [
|
||||
{ url = "https://files.pythonhosted.org/packages/8b/54/b1ae86c0973cc6f0210b53d508ca3641fb6d0c56823f288d108bc7ab3cc8/typing_extensions-4.13.2-py3-none-any.whl", hash = "sha256:a439e7c04b49fec3e5d3e2beaa21755cadbbdc391694e28ccdd36ca4a1408f8c", size = 45806, upload_time = "2025-04-10T14:19:03.967Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/69/e0/552843e0d356fbb5256d21449fa957fa4eff3bbc135a74a691ee70c7c5da/typing_extensions-4.14.0-py3-none-any.whl", hash = "sha256:a1514509136dd0b477638fc68d6a91497af5076466ad0fa6c338e44e359944af", size = 43839, upload_time = "2025-06-02T14:52:10.026Z" },
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user