Add settings, credits, sound, add background color

This commit is contained in:
csd4ni3l
2025-04-22 11:33:10 +02:00
parent b151ad50b3
commit 78c981b399
11 changed files with 404 additions and 11 deletions

View File

@@ -1,5 +1,6 @@
import arcade, arcade.gui, random, math, copy
from utils.constants import COLS, ROWS, CELL_SIZE, SPACING, NEIGHBORS
import arcade, arcade.gui, random, math, copy, time, json
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):
@@ -14,10 +15,14 @@ class Game(arcade.gui.UIView):
self.pypresence_client = pypresence_client
self.spritelist = arcade.SpriteList()
self.last_create_sound = time.perf_counter()
self.start_x = self.window.width / 2 - ((COLS * (CELL_SIZE + SPACING)) / 2)
self.start_y = self.window.height / 2 - ((ROWS * (CELL_SIZE + SPACING)) / 2)
with open("settings.json", "r") as file:
self.settings_dict = json.load(file)
def on_show_view(self):
super().on_show_view()
self.setup_grid()
@@ -34,8 +39,16 @@ class Game(arcade.gui.UIView):
self.fps_label = arcade.gui.UILabel(text="FPS: 10", font_name="Protest Strike", 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.anchor.add(self.back_button, anchor_x="left", anchor_y="top", 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):
self.spritelist.clear()
@@ -72,7 +85,7 @@ class Game(arcade.gui.UIView):
self.pypresence_generation_count = 0
self.pypresence_client.update(state='In Game', details=f'Generation: {self.generation} Population: {self.population}', start=self.pypresence_client.start_time)
next_grid = {y: {x: cell for x, cell in row.items()} for y, row in self.cell_grid.items()}
next_grid = copy.deepcopy(self.cell_grid) # create a copy of the old grid so we dont modify it in-place
grid = self.cell_grid
@@ -111,6 +124,10 @@ class Game(arcade.gui.UIView):
self.running = not self.running
elif symbol == arcade.key.C:
self.population = 0
self.generation = 0
self.population_label.text = f"Population: {self.population}"
self.generation_label.text = f"Generation: {self.generation}"
arcade.unschedule(self.update_generation)
self.setup_grid()
@@ -137,6 +154,12 @@ class Game(arcade.gui.UIView):
if self.cell_grid[grid_row][grid_col] == 0:
self.population += 1
if time.perf_counter() - self.last_create_sound >= 0.05:
self.last_create_sound = time.perf_counter()
if self.settings_dict.get("sfx", True):
create_sound.play(volume=self.settings_dict.get("sfx_volume", 50) / 100, loop=True)
self.sprite_grid[grid_row][grid_col].visible = True
self.cell_grid[grid_row][grid_col] = 1
@@ -144,8 +167,13 @@ class Game(arcade.gui.UIView):
grid_col = math.ceil((self.window.mouse.data["x"] - self.start_x + (CELL_SIZE / 2)) // (CELL_SIZE + SPACING)) # type: ignore
grid_row = math.ceil((self.window.mouse.data["y"] - self.start_y + (CELL_SIZE / 2)) // (CELL_SIZE + SPACING)) # type: ignore
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:
self.population -= 1
if self.settings_dict.get("sfx", True):
destroy_sound.play(volume=self.settings_dict.get("sfx_volume", 50) / 100, loop=True)
self.sprite_grid[grid_row][grid_col].visible = False
self.cell_grid[grid_row][grid_col] = 0