import os, arcade, arcade.gui, random, json, time, copy from game.sprites import Shape from utils.constants import SHAPES, CELL_SIZE, ROWS, COLS, OUTLINE_WIDTH, COLORS, COMBO_MOVES, button_style, POWER_UPS from utils.preload import button_texture, button_hovered_texture, click_sound, break_sound class Game(arcade.gui.UIView): def __init__(self, pypresence_client): super().__init__() self.shape_list = arcade.SpriteList() self.mouse_shape_list = arcade.SpriteList() self.pypresence_client = pypresence_client self.tile_grid = {} self.sprite_grid = {} self.state_before = [] self.shape_to_place = random.choice(list(SHAPES.keys())) self.shape_color = random.choice(COLORS) self.next_shape_to_place = random.choice(list(SHAPES.keys())) self.next_shape_color = random.choice(COLORS) self.shape_data = SHAPES[self.shape_to_place] self.start_x = self.window.width / 2 - (COLS * (CELL_SIZE + OUTLINE_WIDTH)) / 2 self.start_y = self.window.height - (ROWS * (CELL_SIZE + OUTLINE_WIDTH)) - (CELL_SIZE / 2) self.shape_center_x = 0 self.shape_center_y = 0 self.can_place_shape = True self.is_game_over = False self.combo = 0 self.combo_moves_left = COMBO_MOVES self.combo_booster_active = False self.combo_booster_moves_left = 0 self.score = 0 self.score_booster_active = False self.score_booster_moves_left = 0 if os.path.exists("data.json"): with open("data.json", "r") as file: self.high_score = json.load(file)["high_score"] else: self.high_score = 0 self.tiles_to_destroy: list[tuple[int, int]] = [] self.anchor = self.add_widget(arcade.gui.UIAnchorLayout()) with open("settings.json", "r") as file: self.settings_dict = json.load(file) def undo_move(self): if self.score >= POWER_UPS["undo_move"]: self.score -= POWER_UPS["undo_move"] else: return if self.state_before: self.tile_grid, self.shape_to_place, self.next_shape_to_place, self.shape_color, self.next_shape_color, self.score, self.combo, self.combo_moves_left = self.state_before.pop(-1) self.shape_data = SHAPES[self.shape_to_place] for row in range(ROWS): for col in range(COLS): color = self.tile_grid[row][col] self.sprite_grid[row][col].color = color if color else arcade.color.GRAY self.sprite_grid[row][col].original_color = color if color else arcade.color.GRAY def clear_screen(self): if self.score >= POWER_UPS["clear_screen"]: self.score -= POWER_UPS["clear_screen"] else: return self.state_before = [] for row in range(ROWS): for col in range(COLS): if self.tile_grid[row][col]: self.tiles_to_destroy.append((row, col)) break_sound.play() def combo_booster(self): if self.score >= POWER_UPS["combo_booster"]: self.score -= POWER_UPS["combo_booster"] else: return self.combo_booster_active = True self.combo_booster_moves_left = 6 def score_booster(self): if self.score >= POWER_UPS["score_booster"]: self.score -= POWER_UPS["score_booster"] else: return self.score_booster_active = True self.score_booster_moves_left = 6 def main_exit(self): self.window.set_mouse_visible(True) with open("data.json", "w") as file: file.write(json.dumps({"high_score": self.high_score})) from menus.main import Main self.window.show_view(Main()) def on_show_view(self): super().on_show_view() self.setup_grid() self.mouse_shape = Shape(0, 0, self.shape_to_place, self.shape_color, self.mouse_shape_list) 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) self.score_box = self.anchor.add(arcade.gui.UIBoxLayout(space_between=10, vertical=False), anchor_x="center", anchor_y="top") self.score_label = self.score_box.add(arcade.gui.UILabel(text="Score: 0", font_name="Roboto", font_size=24)) self.high_score_label = self.score_box.add(arcade.gui.UILabel(text=f"High Score: {self.high_score}", font_name="Roboto", font_size=24)) self.power_up_label = self.anchor.add(arcade.gui.UILabel(text=f'''{POWER_UPS["undo_move"]} Score - Undo Move (1 to activate) {POWER_UPS["combo_booster"]} Score - Combo Booster (2 to activate) {POWER_UPS["score_booster"]} Score - Score Booster (3 to activate) {POWER_UPS["clear_screen"]} Score - Clear Screen (4 to activate) ''', font_size=14, multiline=True), anchor_x="right", anchor_y="bottom") 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) def on_stick_motion(self, controller, name, value): if name == "leftstick": value *= 10 self.on_mouse_motion(self.mouse_shape.x + value.x, self.mouse_shape.y + value.y, value.x, value.y) def on_button_press(self, controller, name): if name == "a": self.on_mouse_press(self.mouse_shape.x, self.mouse_shape.y, arcade.MOUSE_BUTTON_LEFT, 0) elif name == "start": self.main_exit() def on_mouse_motion(self, x, y, dx, dy): if self.is_game_over: return grid_col = int((x - self.start_x + (CELL_SIZE / 2)) // (CELL_SIZE + OUTLINE_WIDTH)) grid_row = int((y - self.start_y + (CELL_SIZE / 2)) // (CELL_SIZE + OUTLINE_WIDTH)) self.can_place_shape = True tile_positions = [] for offset_col, offset_row in self.shape_data: 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.tile_grid[tile_row][tile_col] or (tile_row, tile_col) in self.tiles_to_destroy: self.can_place_shape = False break tile_positions.append((tile_row, tile_col)) 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: self.collided_tile_positions = [] for row in range(ROWS): for col in range(COLS): if not self.tile_grid[row][col]: 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 else: 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 self.mouse_shape.update(self.shape_to_place, self.shape_color, x, y) def setup_grid(self): for row in range(ROWS): self.tile_grid[row] = {} self.sprite_grid[row] = {} for col in range(COLS): self.create_empty_tile(row, col) def create_empty_tile(self, row, col): self.tile_grid[row][col] = 0 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) self.sprite_grid[row][col] = tile def check_collisions(self, grid_col, grid_row): modified_grid = copy.deepcopy(self.tile_grid) for offset_col, offset_row in self.shape_data: tile_col = grid_col + offset_col tile_row = grid_row + offset_row modified_grid[tile_row][tile_col] = 1 collided_tiles = [] for row_idx, row in modified_grid.items(): if all(row.values()): for col in range(COLS): collided_tiles.append((row_idx, col)) for col in range(COLS): column = [row[col] for row in modified_grid.values()] if all(column): for row in range(ROWS): collided_tiles.append((row, col)) return collided_tiles def on_update(self, _): 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}" for row, col in self.tiles_to_destroy[:]: tile = self.sprite_grid[row][col] if not tile: self.tiles_to_destroy.remove((row, col)) continue tile.scale = (tile.scale_x - 0.05, tile.scale_y - 0.05) if tile.scale_x <= 0.05: tile.color = arcade.color.GRAY tile.original_color = arcade.color.GRAY tile.scale = (1, 1) self.tile_grid[row][col] = 0 self.tiles_to_destroy.remove((row, col)) if self.combo_booster_active: combo_score = (25 * self.combo) else: combo_score = (10 * self.combo) if self.score_booster_active: base_score = 50 else: base_score = 25 self.score += (base_score + combo_score) 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 self.shape_data: tile_col = grid_col + offset_col tile_row = grid_row + offset_row if not (0 <= tile_row < ROWS and 0 <= tile_col < COLS): can_place = False break if (tile_row, tile_col) in self.tiles_to_destroy: continue if self.tile_grid[tile_row][tile_col]: can_place = False break if can_place: return self.is_game_over = True self.game_over_box = self.anchor.add(arcade.gui.UIBoxLayout(), anchor_x="center", anchor_y="center") self.game_over_box._bg_color = arcade.color.BLACK 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)) self.mouse_shape_list.clear() self.window.set_mouse_visible(True) def on_key_press(self, symbol: int, modifiers: int) -> bool | None: if symbol == arcade.key.ESCAPE: self.main_exit() elif symbol == arcade.key.KEY_1: self.undo_move() elif symbol == arcade.key.KEY_2: self.combo_booster() elif symbol == arcade.key.KEY_3: self.score_booster() elif symbol == arcade.key.KEY_4: self.clear_screen() def on_mouse_press(self, x: int, y: int, button: int, modifiers: int) -> bool | None: if self.is_game_over: return grid_col = int((x - self.start_x + (CELL_SIZE / 2)) // (CELL_SIZE + OUTLINE_WIDTH)) grid_row = int((y - self.start_y + (CELL_SIZE / 2)) // (CELL_SIZE + OUTLINE_WIDTH)) if self.can_place_shape: if self.settings_dict.get("sfx", True): click_sound.play(volume=self.settings_dict.get("sfx_volume", 50) / 100) self.state_before.append([copy.deepcopy(self.tile_grid), self.shape_to_place, self.next_shape_to_place, self.shape_color, self.next_shape_color, self.score, self.combo, self.combo_moves_left]) if len(self.state_before) > 3: self.state_before.pop(0) for offset_col, offset_row in self.shape_data: tile_col = grid_col + offset_col tile_row = grid_row + offset_row self.tile_grid[tile_row][tile_col] = self.shape_color self.sprite_grid[tile_row][tile_col].color = self.shape_color self.sprite_grid[tile_row][tile_col].original_color = self.shape_color if self.score_booster_active: self.score += 10 else: self.score += 5 for row, col in self.collided_tile_positions: self.tiles_to_destroy.append((row, col)) if self.collided_tile_positions: break_sound.play() if self.score_booster_active: self.score_booster_moves_left -= 1 if self.score_booster_moves_left <= 0: self.score_booster_active = False if self.combo_booster_active: self.combo_booster_moves_left -= 1 if self.combo_booster_moves_left <= 0: self.combo_booster_active = False if self.collided_tile_positions: self.combo += 1 self.combo_moves_left = COMBO_MOVES else: self.combo_moves_left -= 1 if self.combo_moves_left == 0: self.combo = 0 self.shape_to_place = self.next_shape_to_place self.shape_color = self.next_shape_color self.shape_data = SHAPES[self.shape_to_place] if not self.is_game_over: self.check_game_over() 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. self.next_shape_to_place = random.choice(list(SHAPES.keys())) self.next_shape_color = random.choice(COLORS) self.next_shape_ui.update(self.next_shape_to_place, self.next_shape_color) def on_draw(self): self.window.clear() self.shape_list.draw() self.mouse_shape_list.draw() self.ui.draw()