2020-02-26 23:36:53 -05:00
|
|
|
# import essential libraries
|
2014-01-24 01:48:10 -05:00
|
|
|
import pyb
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
lcd = pyb.LCD("x")
|
2014-10-19 14:02:34 -04:00
|
|
|
lcd.light(1)
|
|
|
|
|
2023-02-01 19:51:48 -05:00
|
|
|
|
2014-01-07 12:14:05 -05:00
|
|
|
# do 1 iteration of Conway's Game of Life
|
|
|
|
def conway_step():
|
2020-02-26 23:36:53 -05:00
|
|
|
for x in range(128): # loop over x coordinates
|
|
|
|
for y in range(32): # loop over y coordinates
|
2017-05-29 03:08:14 -04:00
|
|
|
# count number of neighbours
|
2020-02-26 23:36:53 -05:00
|
|
|
num_neighbours = (
|
|
|
|
lcd.get(x - 1, y - 1)
|
|
|
|
+ lcd.get(x, y - 1)
|
|
|
|
+ lcd.get(x + 1, y - 1)
|
|
|
|
+ lcd.get(x - 1, y)
|
|
|
|
+ lcd.get(x + 1, y)
|
|
|
|
+ lcd.get(x + 1, y + 1)
|
|
|
|
+ lcd.get(x, y + 1)
|
|
|
|
+ lcd.get(x - 1, y + 1)
|
|
|
|
)
|
2014-01-07 12:14:05 -05:00
|
|
|
|
|
|
|
# check if the centre cell is alive or not
|
|
|
|
self = lcd.get(x, y)
|
|
|
|
|
|
|
|
# apply the rules of life
|
|
|
|
if self and not (2 <= num_neighbours <= 3):
|
2020-02-26 23:36:53 -05:00
|
|
|
lcd.pixel(x, y, 0) # not enough, or too many neighbours: cell dies
|
2014-01-07 12:14:05 -05:00
|
|
|
elif not self and num_neighbours == 3:
|
2020-02-26 23:36:53 -05:00
|
|
|
lcd.pixel(x, y, 1) # exactly 3 neighbours around an empty cell: cell is born
|
|
|
|
|
2014-01-07 12:14:05 -05:00
|
|
|
|
|
|
|
# randomise the start
|
|
|
|
def conway_rand():
|
2020-02-26 23:36:53 -05:00
|
|
|
lcd.fill(0) # clear the LCD
|
|
|
|
for x in range(128): # loop over x coordinates
|
|
|
|
for y in range(32): # loop over y coordinates
|
|
|
|
lcd.pixel(x, y, pyb.rng() & 1) # set the pixel randomly
|
|
|
|
|
2014-01-07 12:14:05 -05:00
|
|
|
|
|
|
|
# loop for a certain number of frames, doing iterations of Conway's Game of Life
|
|
|
|
def conway_go(num_frames):
|
|
|
|
for i in range(num_frames):
|
2020-02-26 23:36:53 -05:00
|
|
|
conway_step() # do 1 iteration
|
|
|
|
lcd.show() # update the LCD
|
2014-10-19 14:02:34 -04:00
|
|
|
pyb.delay(50)
|
2014-01-07 12:14:05 -05:00
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2014-10-19 14:02:34 -04:00
|
|
|
# testing
|
2014-01-07 12:14:05 -05:00
|
|
|
conway_rand()
|
2014-10-19 14:02:34 -04:00
|
|
|
conway_go(100)
|