Created
November 23, 2017 12:58
-
-
Save MarcScott/09ae64166689ebaab5d605a1cd1c237a to your computer and use it in GitHub Desktop.
Simple Game of Life for learners
This file contains 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
from guizero import App, Waffle | |
from time import sleep | |
from random import randint | |
app = App() | |
gun = """........................................................ | |
.........................*.............................. | |
.......................*.*.............................. | |
.............**......**............**................... | |
............*...*....**............**................... | |
.**........*.....*...**................................. | |
.**........*...*.**....*.*.............................. | |
...........*.....*.......*.............................. | |
............*...*....................................... | |
.............**......................................... | |
........................................................ | |
........................................................ | |
........................................................ | |
........................................................ | |
........................................................ | |
........................................................ | |
........................................................ | |
........................................................ | |
........................................................ | |
........................................................ | |
""" | |
grid = [[0 if char == "." else 1 for char in row] for row in gun.split()] | |
width = len(grid[0]) | |
height = len(grid) | |
# Set the waffle to have a memory | |
my_waffle = Waffle(app, remember=False, width=width, height=height, pad=0, dim=10) | |
def check_neighbours(x, y): | |
adjacent_cells = [] | |
neighbours = 0 | |
for i in range(x-1, x+2): | |
for j in range(y-1, y+2): | |
if (i, j) != (x, y): | |
adjacent_cells.append((i,j)) | |
for cell in adjacent_cells: | |
if width > cell[0] >= 0: | |
if height > cell[1] >= 0: | |
if grid[cell[1]][cell[0]] == 1: | |
neighbours += 1 | |
return neighbours | |
def tick(): | |
for y in range(height): | |
for x in range(width): | |
if grid[y][x] == 1: | |
my_waffle.set_pixel(x, y, 'black') | |
else: | |
my_waffle.set_pixel(x, y, 'white') | |
next_gen_live = [] | |
next_gen_dead = [] | |
for y in range(height): | |
for x in range(width): | |
neighbours = check_neighbours(x, y) | |
if neighbours == 3: | |
next_gen_live.append((x,y)) | |
elif neighbours < 2 or neighbours > 3: | |
next_gen_dead.append((x,y)) | |
for cell in next_gen_live: | |
grid[cell[1]][cell[0]] = 1 | |
for cell in next_gen_dead: | |
grid[cell[1]][cell[0]] = 0 | |
app.after(100, tick) | |
tick() | |
app.display() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment