Fix game outline, performance improvements

This commit is contained in:
csd4ni3l
2025-04-22 09:13:11 +02:00
parent edd21280f2
commit b151ad50b3
2 changed files with 35 additions and 31 deletions

View File

@@ -15,8 +15,8 @@ class Game(arcade.gui.UIView):
self.pypresence_client = pypresence_client self.pypresence_client = pypresence_client
self.spritelist = arcade.SpriteList() self.spritelist = arcade.SpriteList()
self.start_x = self.window.width / 2 - (COLS * (CELL_SIZE + SPACING)) / 2 + (CELL_SIZE / 2) self.start_x = self.window.width / 2 - ((COLS * (CELL_SIZE + SPACING)) / 2)
self.start_y = self.window.height / 2 - (ROWS * (CELL_SIZE + SPACING)) / 2 + (CELL_SIZE / 2) self.start_y = self.window.height / 2 - ((ROWS * (CELL_SIZE + SPACING)) / 2)
def on_show_view(self): def on_show_view(self):
super().on_show_view() super().on_show_view()
@@ -40,20 +40,27 @@ class Game(arcade.gui.UIView):
self.spritelist.clear() self.spritelist.clear()
self.cell_grid = {} self.cell_grid = {}
self.sprite_grid = {}
for row in range(ROWS): for row in range(ROWS):
self.cell_grid[row] = {} self.cell_grid[row] = {}
self.sprite_grid[row] = {}
for col in range(COLS): for col in range(COLS):
if randomized and random.randint(0, 1) == 1: 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) 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] = cell self.cell_grid[row][col] = 1
self.sprite_grid[row][col] = cell
self.spritelist.append(cell) self.spritelist.append(cell)
self.population += 1 self.population += 1
continue continue
self.cell_grid[row][col] = None 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
self.sprite_grid[row][col] = cell
self.spritelist.append(cell)
def update_generation(self, _): def update_generation(self, _):
if self.running: if self.running:
@@ -67,33 +74,30 @@ class Game(arcade.gui.UIView):
next_grid = {y: {x: cell for x, cell in row.items()} for y, row in self.cell_grid.items()} next_grid = {y: {x: cell for x, cell in row.items()} for y, row in self.cell_grid.items()}
grid = self.cell_grid
for x in range(0, COLS): for x in range(0, COLS):
for y in range(0, ROWS): for y in range(0, ROWS):
cell_neighbors = 0 cell_neighbors = 0
for neighbor_y in NEIGHBORS: for neighbor_y, neighbor_x in NEIGHBORS:
for neighbor_x in NEIGHBORS: if neighbor_x == 0 and neighbor_y == 0:
if neighbor_x == 0 and neighbor_y == 0: continue
continue
if self.cell_grid.get(y + neighbor_y, {}).get(x + neighbor_x) is not None: if grid.get(y + neighbor_y, {}).get(x + neighbor_x) == 1:
cell_neighbors += 1 cell_neighbors += 1
if self.cell_grid[y][x] is not None: if grid[y][x] == 1:
if (cell_neighbors == 2 or cell_neighbors == 3): if (cell_neighbors == 2 or cell_neighbors == 3):
pass # survives pass # survives
else: # dies else: # dies
self.population -= 1 self.population -= 1
self.sprite_grid[y][x].visible = False
self.spritelist.remove(self.cell_grid[y][x]) next_grid[y][x] = 0
del next_grid[y][x]
next_grid[y][x] = None
elif cell_neighbors == 3: # newborn elif cell_neighbors == 3: # newborn
self.population += 1 self.population += 1
self.sprite_grid[y][x].visible = True
cell = arcade.SpriteSolidColor(CELL_SIZE, CELL_SIZE, center_x=self.start_x + x * (CELL_SIZE + SPACING), center_y=self.start_y + y * (CELL_SIZE + SPACING), color=arcade.color.WHITE) next_grid[y][x] = 1
next_grid[y][x] = cell
self.spritelist.append(cell)
self.cell_grid = next_grid self.cell_grid = next_grid
@@ -106,6 +110,8 @@ class Game(arcade.gui.UIView):
if symbol == arcade.key.SPACE: if symbol == arcade.key.SPACE:
self.running = not self.running self.running = not self.running
elif symbol == arcade.key.C: elif symbol == arcade.key.C:
self.population = 0
arcade.unschedule(self.update_generation) arcade.unschedule(self.update_generation)
self.setup_grid() self.setup_grid()
arcade.schedule(self.update_generation, 1 / self.generation_fps) arcade.schedule(self.update_generation, 1 / self.generation_fps)
@@ -115,6 +121,8 @@ class Game(arcade.gui.UIView):
if self.window.keyboard[arcade.key.UP] or self.window.keyboard[arcade.key.DOWN]: # type: ignore 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 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}" self.fps_label.text = f"FPS: {self.generation_fps}"
arcade.unschedule(self.update_generation) arcade.unschedule(self.update_generation)
@@ -127,27 +135,23 @@ class Game(arcade.gui.UIView):
if grid_col < 0 or grid_row < 0 or grid_row >= ROWS or grid_col >= COLS: if grid_col < 0 or grid_row < 0 or grid_row >= ROWS or grid_col >= COLS:
return return
if self.cell_grid[grid_row][grid_col] is None: if self.cell_grid[grid_row][grid_col] == 0:
self.population += 1 self.population += 1
self.sprite_grid[grid_row][grid_col].visible = True
cell = arcade.SpriteSolidColor(CELL_SIZE, CELL_SIZE, center_x=self.start_x + grid_col * (CELL_SIZE + SPACING), center_y=self.start_y + grid_row * (CELL_SIZE + SPACING), color=arcade.color.WHITE) self.cell_grid[grid_row][grid_col] = 1
self.cell_grid[grid_row][grid_col] = cell
self.spritelist.append(cell)
elif self.window.mouse[arcade.MOUSE_BUTTON_RIGHT]: # type: ignore 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_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 grid_row = math.ceil((self.window.mouse.data["y"] - self.start_y + (CELL_SIZE / 2)) // (CELL_SIZE + SPACING)) # type: ignore
if self.cell_grid[grid_row][grid_col] is not None: if self.cell_grid[grid_row][grid_col] == 1:
self.population -= 1 self.population -= 1
self.sprite_grid[grid_row][grid_col].visible = False
self.spritelist.remove(self.cell_grid[grid_row][grid_col]) self.cell_grid[grid_row][grid_col] = 0
del self.cell_grid[grid_row][grid_col]
self.cell_grid[grid_row][grid_col] = None
def on_draw(self): def on_draw(self):
super().on_draw() super().on_draw()
arcade.draw_rect_outline(arcade.rect.LBWH(self.start_x, self.start_y, COLS * (CELL_SIZE + SPACING), ROWS * (CELL_SIZE + SPACING)), arcade.color.WHITE) arcade.draw_rect_outline(arcade.rect.LBWH(self.start_x - (SPACING * 2), self.start_y - (SPACING * 2), COLS * (CELL_SIZE + SPACING), ROWS * (CELL_SIZE + SPACING)), arcade.color.WHITE)
self.spritelist.draw() self.spritelist.draw()

View File

@@ -6,7 +6,7 @@ COLS = 80
ROWS = 60 ROWS = 60
CELL_SIZE = 10 CELL_SIZE = 10
SPACING = 2 SPACING = 2
NEIGHBORS = [0, 1, -1] NEIGHBORS = [(-1, 0), (-1, 1), (-1, -1),(0, 0), (0, 1), (0, -1), (1, 0), (1, 1), (1, -1)]
log_dir = 'logs' log_dir = 'logs'
menu_background_color = Color(28, 28, 28) menu_background_color = Color(28, 28, 28)