conways-game-of-life-on-neo.../src/conway.py
arofarn 0f1023dff8 Add neotrellisM4 lib sources
Lot of cleanup
2020-04-15 01:28:41 +02:00

92 lines
3.6 KiB
Python

from random import randrange
# Acceleration threshold to end the genesis and start
# evolving
ACCEL_THRESHOLD = 100
# Colors:
OFF = (0, 0, 0)
ON = (80, 80, 50)
# Some presets :
VOID = [[False, False, False, False, False, False, False, False],
[False, False, False, False, False, False, False, False],
[False, False, False, False, False, False, False, False],
[False, False, False, False, False, False, False, False],
[False, False, False, False, False, False, False, False],
[False, False, False, False, False, False, False, False],
[False, False, False, False, False, False, False, False],
[False, False, False, False, False, False, False, False]]
GLIDER = [[False, False, False, False, False, False, False, False],
[False, False, False, False, True, False, False, False],
[False, False, True, False, True, False, False, False],
[False, False, False, True, True, False, False, False],
[False, False, False, False, False, False, False, False],
[False, False, False, False, False, False, False, False],
[False, False, False, False, False, False, False, False],
[False, False, False, False, False, False, False, False]]
BEATING_HEART = [[False, False, False, False, False, False, False, False],
[False, False, False, False, False, False, False, False],
[False, False, True, True, True, False, False, False],
[False, False, False, False, False, False, False, False],
[False, False, False, False, False, False, False, False],
[False, False, True, True, True, False, False, False],
[False, False, False, False, False, False, False, False],
[False, False, False, False, False, False, False, False]]
def RANDOM_UNIVERSE(rate):
return [[randrange(0,100) < rate for i in range(8) ] for j in range(8)]
def draw_universe(trellis, universe):
for x in range(8):
for y in range(8):
#activate rising edge events on all keys
if universe[x][y]:
trellis.color(x, y, ON)
else:
trellis.color(x, y, OFF)
def extend_universe(universe):
"""Extend an 8x8 universe to 10x10 with first and last columns and rows as in an donut-shaped universe"""
ext_universe = [[False for x_ext in range(10)] for y_ext in range(10)]
# Copy universe in an extended donut-shaped universe
for x_ext in range(10):
for y_ext in range(10):
if x_ext == 0:
loc_x = 7
elif x_ext == 9:
loc_x = 0
else:
loc_x = x_ext-1
if y_ext == 0:
loc_y = 7
elif y_ext == 9:
loc_y = 0
else:
loc_y = y_ext-1
ext_universe[x_ext][y_ext] = universe[loc_x][loc_y]
return ext_universe
def living_neighbour_count(xcoord, ycoord, ext_universe):
"""Count living cells around xcoord, ycoord"""
living_neighbours = []
living_neighbours.append(ext_universe[xcoord][ycoord])
living_neighbours.append(ext_universe[xcoord][ycoord+1])
living_neighbours.append(ext_universe[xcoord][ycoord+2])
living_neighbours.append(ext_universe[xcoord+1][ycoord])
living_neighbours.append(ext_universe[xcoord+1][ycoord+2])
living_neighbours.append(ext_universe[xcoord+2][ycoord])
living_neighbours.append(ext_universe[xcoord+2][ycoord+1])
living_neighbours.append(ext_universe[xcoord+2][ycoord+2])
return living_neighbours.count(True)