Use Roboto instead of Protest Strike, fix display mode crash, add file

loading
This commit is contained in:
csd4ni3l
2025-06-22 18:37:17 +02:00
parent 81e65daae8
commit f109d8d268
10 changed files with 284 additions and 36 deletions

122
game/file_manager.py Normal file
View File

@@ -0,0 +1,122 @@
import arcade, arcade.gui, os, time
from utils.constants import button_style
from utils.preload import button_texture, button_hovered_texture
from arcade.gui.experimental.scroll_area import UIScrollArea, UIScrollBar
class FileManager(arcade.gui.UIView):
def __init__(self, start_directory, allowed_extensions, *args):
super().__init__()
self.current_directory = start_directory
self.allowed_extensions = allowed_extensions
self.file_buttons = []
self.submitted_content = ""
self.done = False
self.args = args
self.anchor = self.ui.add(arcade.gui.UIAnchorLayout(size_hint=(1, 1)))
self.box = self.anchor.add(arcade.gui.UIBoxLayout(size_hint=(0.7, 0.7)), anchor_x="center", anchor_y="center")
self.content_cache = {}
self.pre_cache_contents()
def on_show_view(self):
super().on_show_view()
self.current_directory_label = self.anchor.add(arcade.gui.UILabel(text=self.current_directory, font_name="Roboto", font_size=24), anchor_x="center", anchor_y="top", align_y=-15)
self.scroll_area = UIScrollArea(size_hint=(0.95, 1)) # center on screen
self.scroll_area.scroll_speed = -50
self.box.add(self.scroll_area)
self.scrollbar = UIScrollBar(self.scroll_area)
self.scrollbar.size_hint = (0.02, 1)
self.anchor.add(self.scrollbar, anchor_x="right", anchor_y="center")
self.files_box = arcade.gui.UIBoxLayout(space_between=5)
self.scroll_area.add(self.files_box)
self.back_button = self.anchor.add(arcade.gui.UITextureButton(texture=button_texture, texture_hovered=button_hovered_texture, text='<--', style=button_style, width=100, height=50), anchor_x="left", anchor_y="top", align_x=5, align_y=-5)
self.back_button.on_click = lambda event: self.change_directory(os.path.dirname(self.current_directory))
self.show_directory()
def submit(self, content):
self.submitted_content = content
self.done = True
if os.path.isfile(content):
from game.play import Game
self.window.show_view(Game(*self.args, load_from=self.submitted_content))
def get_content(self, directory):
if not directory in self.content_cache or time.perf_counter() - self.content_cache[directory][-1] >= 30:
try:
entries = os.listdir(directory)
except PermissionError:
return None
filtered = [
entry for entry in entries
if (os.path.isdir(os.path.join(directory, entry)) and not "." in entry) or
os.path.splitext(entry)[1].lower() in self.allowed_extensions
]
sorted_entries = sorted(
filtered,
key=lambda x: (0 if os.path.isdir(os.path.join(directory, x)) else 1, x.lower())
)
self.content_cache[directory] = sorted_entries
self.content_cache[directory].append(time.perf_counter())
return self.content_cache[directory][:-1]
def pre_cache_contents(self):
for directory in self.walk_limited_depth(self.current_directory):
self.get_content(directory)
def walk_limited_depth(self, start_dir, max_depth=2):
start_dir = os.path.abspath(start_dir)
def _walk(current_dir, current_depth):
if current_depth > max_depth:
return
yield current_dir
try:
with os.scandir(current_dir) as it:
for entry in it:
if entry.is_dir(follow_symlinks=False):
yield from _walk(entry.path, current_depth + 1)
except PermissionError:
pass # skip directories you can't access
return _walk(start_dir, 0)
def show_directory(self):
self.files_box.clear()
self.file_buttons.clear()
self.current_directory_label.text = self.current_directory
for file in self.get_content(self.current_directory):
self.file_buttons.append(self.files_box.add(arcade.gui.UITextureButton(texture=button_texture, texture_hovered=button_hovered_texture, text=file, style=button_style, width=self.window.width / 1.5)))
if os.path.isdir(f"{self.current_directory}/{file}"):
self.file_buttons[-1].on_click = lambda event, directory=f"{self.current_directory}/{file}": self.change_directory(directory)
else:
self.file_buttons[-1].on_click = lambda event, file=f"{self.current_directory}/{file}": self.submit(file)
def change_directory(self, directory):
if directory.startswith("//"): # Fix / paths
directory = directory[1:]
self.current_directory = directory
self.show_directory()
def on_mouse_press(self, x, y, button, modifiers):
if button == arcade.MOUSE_BUTTON_RIGHT:
self.change_directory(os.path.dirname(self.current_directory))

View File

@@ -1,16 +1,18 @@
import arcade, arcade.gui, random, math, copy, time, json
import arcade, arcade.gui, random, math, copy, time, json, os
from utils.constants import COLS, ROWS, CELL_SIZE, SPACING, NEIGHBORS, button_style
from utils.preload import create_sound, destroy_sound, button_texture, button_hovered_texture
class Game(arcade.gui.UIView):
def __init__(self, pypresence_client=None):
def __init__(self, pypresence_client=None, generation=None, running=False, cell_grid=None, load_from=None):
super().__init__()
self.generation = 0
self.generation = generation or 0
self.population = 0
self.running = True
self.running = running or False
self.generation_fps = 10
self.cell_grid = cell_grid or {}
self.sprite_grid = {}
self.load_from = load_from
self.pypresence_generation_count = 0
self.pypresence_client = pypresence_client
@@ -30,48 +32,63 @@ class Game(arcade.gui.UIView):
self.anchor = self.add_widget(arcade.gui.UIAnchorLayout(size_hint=(1, 1)))
self.info_box = self.anchor.add(arcade.gui.UIBoxLayout(space_between=5, vertical=False), anchor_x="center", anchor_y="top")
self.population_label = arcade.gui.UILabel(text="Population: 0", font_name="Protest Strike", font_size=16)
self.population_label = arcade.gui.UILabel(text="Population: 0", font_name="Roboto", font_size=16)
self.info_box.add(self.population_label)
self.generation_label = arcade.gui.UILabel(text="Generation: 0", font_name="Protest Strike", font_size=16)
self.generation_label = arcade.gui.UILabel(text="Generation: 0", font_name="Roboto", font_size=16)
self.info_box.add(self.generation_label)
self.fps_label = arcade.gui.UILabel(text="FPS: 10", font_name="Protest Strike", font_size=16)
self.fps_label = arcade.gui.UILabel(text="FPS: 10", font_name="Roboto", font_size=16)
self.info_box.add(self.fps_label)
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.back_button.on_click = lambda event: self.main_exit()
self.anchor.add(self.back_button, anchor_x="left", anchor_y="top", align_x=5, align_y=-5)
self.load_button = arcade.gui.UITextureButton(texture=button_texture, texture_hovered=button_hovered_texture, text="Load", style=button_style, width=200, height=100)
self.load_button.on_click = lambda event: self.load()
self.anchor.add(self.load_button, anchor_x="left", anchor_y="bottom", align_x=5, align_y=5)
arcade.schedule(self.update_generation, 1 / self.generation_fps)
def main_exit(self):
from menus.main import Main
self.window.show_view(Main(self.pypresence_client))
def setup_grid(self, randomized=False):
def setup_grid(self, load_existing=False, randomized=False):
self.spritelist.clear()
self.cell_grid = {}
self.sprite_grid = {}
for row in range(ROWS):
self.cell_grid[row] = {}
self.sprite_grid[row] = {}
if self.load_from:
loaded_data = []
with open(self.load_from, "r") as file:
data = file.read().splitlines()
for line in data:
if line == "#Life 1.06":
continue
x, y = line.split(" ")
x = int(COLS / 2 + int(x))
y = int(ROWS / 2 + int(y))
loaded_data.append((y, x))
for row in range(ROWS):
for col in range(COLS):
if randomized and random.randint(0, 1) == 1:
cell = arcade.SpriteSolidColor(CELL_SIZE, CELL_SIZE, center_x=self.start_x + col * (CELL_SIZE + SPACING), center_y=self.start_y + row * (CELL_SIZE + SPACING), color=arcade.color.WHITE)
self.cell_grid[row][col] = 1
self.sprite_grid[row][col] = cell
self.spritelist.append(cell)
if self.load_from:
self.cell_grid[row][col] = 1 if (row, col) in loaded_data else 0
elif not load_existing:
if randomized and random.randint(0, 1):
self.cell_grid[row][col] = 1
self.population += 1
continue
self.population += 1
self.cell_grid[row][col] = 0
continue
self.cell_grid[row][col] = 0
cell = arcade.SpriteSolidColor(CELL_SIZE, CELL_SIZE, center_x=self.start_x + col * (CELL_SIZE + SPACING), center_y=self.start_y + row * (CELL_SIZE + SPACING), color=arcade.color.WHITE)
cell.visible = False
cell.visible = self.cell_grid[row][col]
self.sprite_grid[row][col] = cell
self.spritelist.append(cell)
@@ -96,10 +113,10 @@ class Game(arcade.gui.UIView):
if neighbor_x == 0 and neighbor_y == 0:
continue
if grid.get(y + neighbor_y, {}).get(x + neighbor_x) == 1:
if grid.get(y + neighbor_y, {}).get(x + neighbor_x):
cell_neighbors += 1
if grid[y][x] == 1:
if grid[y][x]:
if (cell_neighbors == 2 or cell_neighbors == 3):
pass # survives
else: # dies
@@ -129,6 +146,10 @@ class Game(arcade.gui.UIView):
self.population_label.text = f"Population: {self.population}"
self.generation_label.text = f"Generation: {self.generation}"
self.cell_grid.clear()
self.spritelist.clear()
self.sprite_grid.clear()
arcade.unschedule(self.update_generation)
self.setup_grid()
arcade.schedule(self.update_generation, 1 / self.generation_fps)
@@ -170,13 +191,17 @@ class Game(arcade.gui.UIView):
if grid_col < 0 or grid_row < 0 or grid_row >= ROWS or grid_col >= COLS:
return
if self.cell_grid[grid_row][grid_col] == 1:
if self.cell_grid[grid_row][grid_col]:
self.population -= 1
if self.settings_dict.get("sfx", True):
destroy_sound.play(volume=self.settings_dict.get("sfx_volume", 50) / 100)
self.sprite_grid[grid_row][grid_col].visible = False
self.cell_grid[grid_row][grid_col] = 0
def load(self):
from game.file_manager import FileManager
self.window.show_view(FileManager(os.path.expanduser("~"), [".txt"], self.pypresence_client, self.generation, self.running, self.cell_grid))
def on_draw(self):
super().on_draw()