mirror of
https://github.com/csd4ni3l/game-of-life.git
synced 2026-01-01 04:23:42 +01:00
use numpy instead of bitmasks and get atleast 15x speedup, change default cols and rows to 128x96 for more space.
This commit is contained in:
@@ -1,39 +1,27 @@
|
||||
from utils.constants import ROWS, COLS, NEIGHBORS
|
||||
TOTAL_CELLS = ROWS * COLS
|
||||
from utils.constants import ROWS, COLS
|
||||
import numpy as np
|
||||
|
||||
def create_numpy_grid():
|
||||
return np.zeros((ROWS, COLS), dtype=np.uint8)
|
||||
|
||||
def get_index(row, col):
|
||||
return row * COLS + col
|
||||
def count_neighbors(grid):
|
||||
padded = np.pad(grid, pad_width=1, mode='constant', constant_values=0)
|
||||
|
||||
neighbors = (
|
||||
padded[0:-2, 0:-2] + # top-left
|
||||
padded[0:-2, 1:-1] + # top
|
||||
padded[0:-2, 2:] + # top-right
|
||||
padded[1:-1, 0:-2] + # left
|
||||
padded[1:-1, 2:] + # right
|
||||
padded[2:, 0:-2] + # bottom-left
|
||||
padded[2:, 1:-1] + # bottom
|
||||
padded[2:, 2:] # bottom-right
|
||||
)
|
||||
|
||||
return neighbors
|
||||
|
||||
def get_neighbors(cell_grid, neighbor_mask):
|
||||
return (cell_grid & neighbor_mask).bit_count()
|
||||
|
||||
def unset_bit(number, i):
|
||||
return number & ~(1 << i)
|
||||
|
||||
def set_bit(number, i):
|
||||
return number | (1 << i)
|
||||
|
||||
def get_bit(number, i):
|
||||
return (number >> i) & 1
|
||||
|
||||
def print_bits(n: int, width: int = 8):
|
||||
print(f"{n:0{width}b}")
|
||||
|
||||
def create_zeroed_int(n):
|
||||
zero_val = 0
|
||||
bitmask = (1 << n) -1
|
||||
return zero_val & bitmask
|
||||
|
||||
def precompute_neighbor_masks():
|
||||
masks = [0] * TOTAL_CELLS
|
||||
for row in range(ROWS):
|
||||
for col in range(COLS):
|
||||
index = get_index(row, col)
|
||||
mask = 0
|
||||
for dy, dx in NEIGHBORS:
|
||||
ny, nx = row + dy, col + dx
|
||||
if 0 <= ny < ROWS and 0 <= nx < COLS:
|
||||
neighbor_index = get_index(ny, nx)
|
||||
mask |= 1 << neighbor_index
|
||||
masks[index] = mask
|
||||
return masks
|
||||
def update_generation(cell_grid: np.array):
|
||||
neighbors = count_neighbors(cell_grid)
|
||||
new_grid = ((cell_grid == 1) & ((neighbors == 2) | (neighbors == 3))) | \
|
||||
((cell_grid == 0) & (neighbors == 3))
|
||||
return new_grid.astype(np.uint8)
|
||||
65
game/play.py
65
game/play.py
@@ -1,11 +1,12 @@
|
||||
import arcade, arcade.gui, random, time, json, os
|
||||
from game.file_support import load_file
|
||||
from utils.constants import COLS, ROWS, CELL_SIZE, SPACING, button_style
|
||||
from utils.preload import create_sound, destroy_sound, button_texture, button_hovered_texture, NEIGHBOUR_MASKS
|
||||
from game.game_of_life import get_index, get_bit, get_neighbors, set_bit, unset_bit, create_zeroed_int
|
||||
from utils.preload import create_sound, destroy_sound, button_texture, button_hovered_texture
|
||||
from game.game_of_life import create_numpy_grid, update_generation
|
||||
import numpy as np
|
||||
|
||||
class Game(arcade.gui.UIView):
|
||||
def __init__(self, pypresence_client=None, generation=None, running=False, cell_grid=None, generation_fps=10, load_from=None):
|
||||
def __init__(self, pypresence_client=None, generation=None, running=True, cell_grid=None, generation_fps=9999, load_from="/home/csd4ni3l/Downloads/glider_gun.rle"):
|
||||
super().__init__()
|
||||
|
||||
self.generation = generation or 0
|
||||
@@ -20,6 +21,7 @@ class Game(arcade.gui.UIView):
|
||||
self.generation_time = 1 / self.generation_fps
|
||||
self.generation_delta_time = 1 / self.generation_fps
|
||||
self.last_generation_update = time.perf_counter()
|
||||
self.last_info_update = time.perf_counter()
|
||||
|
||||
self.pypresence_client = pypresence_client
|
||||
self.spritelist = arcade.SpriteList()
|
||||
@@ -74,22 +76,25 @@ class Game(arcade.gui.UIView):
|
||||
if self.load_from:
|
||||
loaded_data = load_file(COLS / 2, ROWS / 2, self.load_from)
|
||||
|
||||
self.cell_grid = create_zeroed_int(ROWS * COLS)
|
||||
self.cell_grid = create_numpy_grid()
|
||||
|
||||
for row in range(ROWS):
|
||||
self.sprite_grid[row] = {}
|
||||
for col in range(COLS):
|
||||
if self.load_from:
|
||||
if (row, col) in loaded_data:
|
||||
self.cell_grid = set_bit(self.cell_grid, get_index(row, col))
|
||||
self.cell_grid[row, col] = 1
|
||||
elif not load_existing:
|
||||
if randomized and random.randint(0, 1):
|
||||
self.cell_grid = set_bit(self.cell_grid, get_index(row, col))
|
||||
self.cell_grid[row, col] = 1
|
||||
self.population += 1
|
||||
continue
|
||||
|
||||
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 = get_bit(self.cell_grid, get_index(row, col))
|
||||
|
||||
if not bool(self.cell_grid[row, col]):
|
||||
cell.visible = bool(self.cell_grid[row, col])
|
||||
|
||||
self.sprite_grid[row][col] = cell
|
||||
self.spritelist.append(cell)
|
||||
|
||||
@@ -105,27 +110,12 @@ 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 = self.cell_grid | 0
|
||||
old_grid = self.cell_grid.copy()
|
||||
self.cell_grid = update_generation(self.cell_grid)
|
||||
|
||||
for x in range(0, COLS):
|
||||
for y in range(0, ROWS):
|
||||
index = get_index(y, x)
|
||||
cell_neighbors = get_neighbors(self.cell_grid, NEIGHBOUR_MASKS[index])
|
||||
|
||||
if get_bit(self.cell_grid, index):
|
||||
if (cell_neighbors == 2 or cell_neighbors == 3):
|
||||
pass # survives
|
||||
else: # dies
|
||||
self.population -= 1
|
||||
self.sprite_grid[y][x].visible = False
|
||||
next_grid = unset_bit(next_grid, index)
|
||||
|
||||
elif cell_neighbors == 3: # newborn
|
||||
self.population += 1
|
||||
self.sprite_grid[y][x].visible = True
|
||||
next_grid = set_bit(next_grid, index)
|
||||
|
||||
self.cell_grid = next_grid
|
||||
changed_rows, changed_cols = np.where(old_grid != self.cell_grid)
|
||||
for row, col in zip(changed_rows, changed_cols):
|
||||
self.sprite_grid[row][col].visible = bool(self.cell_grid[row, col])
|
||||
|
||||
def on_key_press(self, symbol: int, modifiers: int) -> bool | None:
|
||||
super().on_key_press(symbol, modifiers)
|
||||
@@ -150,9 +140,12 @@ class Game(arcade.gui.UIView):
|
||||
def on_update(self, delta_time):
|
||||
super().on_update(delta_time)
|
||||
|
||||
self.actual_fps_label.text = f"Actual FPS: {round(1 / self.generation_delta_time, 2)}"
|
||||
self.population_label.text = f"Population: {self.population}"
|
||||
self.generation_label.text = f"Generation: {self.generation}"
|
||||
if time.perf_counter() - self.last_info_update >= 0.5:
|
||||
self.last_info_update = time.perf_counter()
|
||||
self.actual_fps_label.text = f"Actual FPS: {round(1 / self.generation_delta_time, 2)}"
|
||||
if not self.population < 0: # generation might be faster than 60 FPS, leading to minus population counts.
|
||||
self.population_label.text = f"Population: {self.population}"
|
||||
self.generation_label.text = f"Generation: {self.generation}"
|
||||
|
||||
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
|
||||
@@ -173,9 +166,7 @@ class Game(arcade.gui.UIView):
|
||||
if grid_col < 0 or grid_row < 0 or grid_row >= ROWS or grid_col >= COLS:
|
||||
return
|
||||
|
||||
index = get_index(grid_row, grid_col)
|
||||
|
||||
if not get_bit(self.cell_grid, index):
|
||||
if not self.cell_grid[grid_row, grid_col]:
|
||||
self.population += 1
|
||||
|
||||
if time.perf_counter() - self.last_create_sound >= 0.05:
|
||||
@@ -184,7 +175,7 @@ class Game(arcade.gui.UIView):
|
||||
create_sound.play(volume=self.settings_dict.get("sfx_volume", 50) / 100)
|
||||
|
||||
self.sprite_grid[grid_row][grid_col].visible = True
|
||||
self.cell_grid = set_bit(self.cell_grid, index)
|
||||
self.cell_grid[grid_row, grid_col] = 1
|
||||
|
||||
elif self.window.mouse[arcade.MOUSE_BUTTON_RIGHT]: # type: ignore
|
||||
grid_col = int((self.window.mouse.data["x"] - self.start_x + (CELL_SIZE / 2)) // (CELL_SIZE + SPACING)) # type: ignore
|
||||
@@ -193,14 +184,12 @@ class Game(arcade.gui.UIView):
|
||||
if grid_col < 0 or grid_row < 0 or grid_row >= ROWS or grid_col >= COLS:
|
||||
return
|
||||
|
||||
index = get_index(grid_row, grid_col)
|
||||
|
||||
if get_bit(self.cell_grid, index):
|
||||
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 = unset_bit(self.cell_grid, index)
|
||||
self.cell_grid[grid_row, grid_col] = 0
|
||||
|
||||
def load(self):
|
||||
arcade.unschedule(self.update_generation)
|
||||
|
||||
Reference in New Issue
Block a user