Add controller support

This commit is contained in:
csd4ni3l
2025-06-25 11:00:35 +02:00
parent 227d5416e0
commit d294ef3ebd
7 changed files with 61 additions and 46 deletions

View File

@@ -1,4 +1,5 @@
The Roboto Black font used in this project is licensed under the Open Font License. Read assets/fonts/OFL.txt for more information.
The Roboto Black font used in this project is licensed under the Open Font License.
Read assets/fonts/OFL.txt for more information.
Thanks to Freesound.org / kevinkac for the Crate Break 1 sound effect! (https://freesound.org/people/kevinkace/sounds/66777/)
Licensed under the Creative Commons Zero v1.0 Universal (CC0 1.0) Public Domain Dedication

View File

@@ -1,9 +1,8 @@
import os, arcade, arcade.gui, random, math, json, time
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
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__()
@@ -52,34 +51,6 @@ class Game(arcade.gui.UIView):
from menus.main import Main
self.window.show_view(Main())
def on_mouse_motion(self, x, y, dx, dy):
super().on_mouse_motion(x, y, dx, dy)
grid_col = math.ceil((x - self.start_x + (CELL_SIZE / 2)) // (CELL_SIZE + OUTLINE_WIDTH))
grid_row = math.ceil((y - self.start_y + (CELL_SIZE / 2)) // (CELL_SIZE + OUTLINE_WIDTH))
self.can_place_shape = True
tile_positions = []
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]:
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)
for row in range(ROWS):
for col in range(COLS):
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.mouse_shape.update(self.shape_to_place, self.shape_color, x, y)
def on_show_view(self):
super().on_show_view()
@@ -104,6 +75,43 @@ class Game(arcade.gui.UIView):
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):
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 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]:
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)
for row in range(ROWS):
for col in range(COLS):
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.mouse_shape.update(self.shape_to_place, self.shape_color, x, y)
def setup_grid(self):
for row in range(ROWS):
self.occupied[row] = {}
@@ -223,8 +231,8 @@ class Game(arcade.gui.UIView):
def on_mouse_press(self, x: int, y: int, button: int, modifiers: int) -> bool | None:
super().on_mouse_press(x, y, button, modifiers)
grid_col = math.ceil((x - self.start_x + (CELL_SIZE / 2)) // (CELL_SIZE + OUTLINE_WIDTH))
grid_row = math.ceil((y - self.start_y + (CELL_SIZE / 2)) // (CELL_SIZE + OUTLINE_WIDTH))
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):

View File

@@ -1,13 +1,15 @@
import arcade, arcade.gui, asyncio, pypresence, time, copy, json
from utils.preload import button_texture, button_hovered_texture
from utils.constants import button_style, discord_presence_id
from utils.utils import FakePyPresence
from arcade.gui.experimental.focus import UIFocusGroup
class Main(arcade.gui.UIView):
def __init__(self, pypresence_client=None):
super().__init__()
self.anchor = self.add_widget(arcade.gui.UIAnchorLayout())
self.anchor = self.add_widget(UIFocusGroup(size_hint=(1, 1)))
self.box = self.anchor.add(arcade.gui.UIBoxLayout(space_between=10), anchor_x='center', anchor_y='center')
self.pypresence_client = pypresence_client
@@ -57,6 +59,8 @@ class Main(arcade.gui.UIView):
self.settings_button = self.box.add(arcade.gui.UITextureButton(text="Settings", texture=button_texture, texture_hovered=button_hovered_texture, width=self.window.width / 2, height=150, style=button_style))
self.settings_button.on_click = lambda event: self.settings()
self.anchor.detect_focusable_widgets()
def play(self):
from game.play import Game
self.window.show_view(Game(self.pypresence_client))

View File

@@ -6,7 +6,6 @@ from utils.constants import button_style, dropdown_style, slider_style, settings
from utils.utils import FakePyPresence
from utils.preload import button_texture, button_hovered_texture, theme_sound
from arcade.gui import UIBoxLayout, UIAnchorLayout
from arcade.gui.experimental.focus import UIFocusGroup
class Settings(arcade.gui.UIView):
@@ -30,16 +29,16 @@ class Settings(arcade.gui.UIView):
self.modified_settings = {}
def create_layouts(self):
self.anchor = self.add_widget(UIAnchorLayout(size_hint=(1, 1)))
self.anchor = self.add_widget(UIFocusGroup(size_hint=(1, 1)))
self.box = UIBoxLayout(space_between=50, align="center", vertical=False)
self.box = arcade.gui.UIBoxLayout(space_between=50, align="center", vertical=False)
self.anchor.add(self.box, anchor_x="center", anchor_y="top", align_x=10, align_y=-75)
self.top_box = UIBoxLayout(space_between=self.window.width / 160, vertical=False)
self.top_box = arcade.gui.UIBoxLayout(space_between=self.window.width / 160, vertical=False)
self.anchor.add(self.top_box, anchor_x="left", anchor_y="top", align_x=10, align_y=-10)
self.key_layout = self.box.add(UIBoxLayout(space_between=20, align='left'))
self.value_layout = self.box.add(UIBoxLayout(space_between=13, align='left'))
self.key_layout = self.box.add(arcade.gui.UIBoxLayout(space_between=20, align='left'))
self.value_layout = self.box.add(arcade.gui.UIBoxLayout(space_between=13, align='left'))
def on_show_view(self):
super().on_show_view()
@@ -67,6 +66,8 @@ class Settings(arcade.gui.UIView):
self.top_box.add(category_button)
self.anchor.detect_focusable_widgets()
def display_category(self, category):
if hasattr(self, 'apply_button'):
self.anchor.remove(self.apply_button)
@@ -135,6 +136,8 @@ class Settings(arcade.gui.UIView):
self.apply_button.on_click = lambda e: self.apply_settings()
self.anchor.add(self.apply_button, anchor_x="right", anchor_y="bottom", align_x=-10, align_y=10)
self.anchor.detect_focusable_widgets()
def apply_settings(self):
for config_key, value in self.modified_settings.items():
self.settings_dict[config_key] = value

5
run.py
View File

@@ -7,7 +7,8 @@ import logging, datetime, os, json, sys, arcade
from utils.utils import get_closest_resolution, print_debug_info, on_exception
from utils.constants import log_dir, menu_background_color
from menus.main import Main
from utils.preload import theme_sound # type: ignore # needed for preload
from utils.preload import theme_sound # needed for preload
from arcade.experimental.controller_window import ControllerWindow
sys.excepthook = on_exception
@@ -72,7 +73,7 @@ else:
if settings.get("music", True):
theme_sound.play(volume=settings.get("music_volume", 50) / 100, loop=True)
window = arcade.Window(width=resolution[0], height=resolution[1], title='ShatterStack', samples=antialiasing, antialiasing=antialiasing > 0, fullscreen=fullscreen, vsync=vsync, resizable=False, style=style)
window = ControllerWindow(width=resolution[0], height=resolution[1], title='ShatterStack', samples=antialiasing, antialiasing=antialiasing > 0, fullscreen=fullscreen, vsync=vsync, resizable=False, style=style)
if vsync:
window.set_vsync(True)

View File

@@ -7,7 +7,7 @@ menu_background_color = (30, 30, 47)
log_dir = 'logs'
discord_presence_id = 1360953272843632680
COMBO_TIME = 10
COMBO_TIME = 5
CELL_SIZE = 80
ROWS = 8
COLS = 8

View File

@@ -1,9 +1,7 @@
import logging, arcade, arcade.gui, sys, traceback
import logging, pyglet, arcade, arcade.gui, sys, traceback
from utils.constants import menu_background_color
import pyglet.info, pyglet.event
def dump_platform():
import platform
logging.debug(f'Platform: {platform.platform()}')