Created
December 6, 2019 02:27
-
-
Save itarato/a31972f90bef37fda8c97311a1960cf7 to your computer and use it in GitHub Desktop.
Game of Life (in Pyxel)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pyxel | |
import random | |
class App: | |
def __init__(self, size, game_of_life): | |
super().__init__() | |
self.size = size | |
self.game_of_life = game_of_life | |
pyxel.init(size, size, fps=60, scale=4) | |
pyxel.cls(0) | |
pyxel.run(self.update, self.draw) | |
def update(self): | |
pass | |
def draw(self): | |
updates = self.game_of_life.step() | |
for [x, y, val] in updates: | |
pyxel.pix(x, y, val * 5) | |
offs_map = [[-1, -1], [-1, 0], [-1, 1], | |
[1, -1], [1, 0], [1, 1], [0, 1], [0, -1]] | |
class Cell: | |
def __init__(self): | |
super().__init__() | |
self.is_alive = False | |
self.alive_neighbor_current = 0 | |
self.alive_neighbor_new = 0 | |
def sync_neighbor(self): | |
self.alive_neighbor_current = self.alive_neighbor_new | |
def update_neighbor(self, val): | |
self.alive_neighbor_new += val | |
def neighbor_count(self): | |
return self.alive_neighbor_current | |
def __str__(self): | |
return "" | |
class GameOfLife: | |
def __init__(self, size): | |
super().__init__() | |
self.size = size | |
self.grid = [] | |
for _ in range(size): | |
row = [] | |
for _ in range(size): | |
row.append(Cell()) | |
self.grid.append(row) | |
self.randomize() | |
def set_cell(self, x, y, is_alive): | |
self.grid[y][x].is_alive = is_alive | |
neighbor_delta = 1 if is_alive else -1 | |
for [offs_x, offs_y] in offs_map: | |
n_x = (x + offs_x) % self.size | |
n_y = (y + offs_y) % self.size | |
self.get_cell(n_x, n_y).update_neighbor(neighbor_delta) | |
def get_cell(self, x, y): | |
return self.grid[y][x] | |
def is_alive(self, x, y): | |
return self.get_cell(x, y).is_alive | |
def randomize(self): | |
for y in range(self.size): | |
for x in range(self.size): | |
if random.random() > 0.6: | |
self.set_cell(x, y, True) | |
self.refresh_neighbor_counts() | |
def step(self): | |
updates = [] | |
for y in range(self.size): | |
for x in range(self.size): | |
cell = self.get_cell(x, y) | |
if self.is_alive(x, y): | |
if cell.neighbor_count() <= 1 or cell.neighbor_count() >= 4: | |
self.set_cell(x, y, False) | |
updates.append([x, y, 0]) | |
else: | |
if cell.neighbor_count() == 3: | |
self.set_cell(x, y, True) | |
updates.append([x, y, 1]) | |
self.refresh_neighbor_counts() | |
return updates | |
def refresh_neighbor_counts(self): | |
for y in range(self.size): | |
for x in range(self.size): | |
self.get_cell(x, y).sync_neighbor() | |
def debug_dump(self): | |
for y in range(self.size): | |
print("") | |
for x in range(self.size): | |
print("%s%s " % ('O' if self.grid[y][x].is_alive else '_', | |
self.grid[y][x].neighbor_count()), end='') | |
if __name__ == "__main__": | |
size = 128 | |
App(size, GameOfLife(size)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment