mirror of
https://github.com/csd4ni3l/game-of-life.git
synced 2025-11-05 02:58:03 +01:00
fix python 3.11 compatibility and mouse cell creation/deletion to
on_update
This commit is contained in:
@@ -1 +1 @@
|
||||
3.13
|
||||
3.11
|
||||
|
||||
85
game/play.py
85
game/play.py
@@ -76,47 +76,6 @@ class Game(arcade.gui.UIView):
|
||||
self.spritelist.append(cell)
|
||||
|
||||
def update_generation(self, _):
|
||||
if self.window.keyboard[arcade.key.UP] or self.window.keyboard[arcade.key.DOWN]: # type: ignore
|
||||
self.generation_fps += 1 if self.window.keyboard[arcade.key.UP] else -1 # type: ignore
|
||||
if self.generation_fps < 1:
|
||||
self.generation_fps = 1
|
||||
self.fps_label.text = f"FPS: {self.generation_fps}"
|
||||
|
||||
arcade.unschedule(self.update_generation)
|
||||
arcade.schedule(self.update_generation, 1 / self.generation_fps)
|
||||
|
||||
if self.window.mouse[arcade.MOUSE_BUTTON_LEFT]: # type: ignore
|
||||
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] == 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)
|
||||
|
||||
self.sprite_grid[grid_row][grid_col].visible = True
|
||||
self.cell_grid[grid_row][grid_col] = 1
|
||||
|
||||
elif self.window.mouse[arcade.MOUSE_BUTTON_RIGHT]: # type: ignore
|
||||
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)
|
||||
self.sprite_grid[grid_row][grid_col].visible = False
|
||||
self.cell_grid[grid_row][grid_col] = 0
|
||||
|
||||
if self.running:
|
||||
self.generation += 1
|
||||
|
||||
@@ -174,6 +133,50 @@ class Game(arcade.gui.UIView):
|
||||
self.setup_grid()
|
||||
arcade.schedule(self.update_generation, 1 / self.generation_fps)
|
||||
|
||||
def on_update(self, delta_time):
|
||||
super().on_update(delta_time)
|
||||
|
||||
if self.window.keyboard[arcade.key.UP] or self.window.keyboard[arcade.key.DOWN]: # type: ignore
|
||||
self.generation_fps += 1 if self.window.keyboard[arcade.key.UP] else -1 # type: ignore
|
||||
if self.generation_fps < 1:
|
||||
self.generation_fps = 1
|
||||
self.fps_label.text = f"FPS: {self.generation_fps}"
|
||||
|
||||
arcade.unschedule(self.update_generation)
|
||||
arcade.schedule(self.update_generation, 1 / self.generation_fps)
|
||||
|
||||
if self.window.mouse[arcade.MOUSE_BUTTON_LEFT]: # type: ignore
|
||||
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] == 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)
|
||||
|
||||
self.sprite_grid[grid_row][grid_col].visible = True
|
||||
self.cell_grid[grid_row][grid_col] = 1
|
||||
|
||||
elif self.window.mouse[arcade.MOUSE_BUTTON_RIGHT]: # type: ignore
|
||||
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)
|
||||
self.sprite_grid[grid_row][grid_col].visible = False
|
||||
self.cell_grid[grid_row][grid_col] = 0
|
||||
|
||||
def on_draw(self):
|
||||
super().on_draw()
|
||||
|
||||
|
||||
@@ -117,9 +117,9 @@ class Settings(arcade.gui.UIView):
|
||||
if self.settings_dict.get(setting_dict["config_key"]) == 0:
|
||||
label_text = "FPS Limit: Disabled"
|
||||
else:
|
||||
label_text = f"FPS Limit: {self.settings_dict.get(setting_dict["config_key"], setting_dict["default"])}"
|
||||
label_text = f"FPS Limit: {self.settings_dict.get(setting_dict['config_key'], setting_dict['default'])}"
|
||||
else:
|
||||
label_text = f"{setting}: {int(self.settings_dict.get(setting_dict["config_key"], setting_dict["default"]))}"
|
||||
label_text = f"{setting}: {int(self.settings_dict.get(setting_dict['config_key'], setting_dict['default']))}"
|
||||
|
||||
label.text = label_text
|
||||
|
||||
|
||||
Reference in New Issue
Block a user