11#import essential libraries
2- import lcd
32import pyb
43
4+ lcd = pyb.LCD('x')
5+ lcd.light(1)
6+
57# do 1 iteration of Conway's Game of Life
68def conway_step():
79 for x in range(128): # loop over x coordinates
@@ -21,26 +23,24 @@ def conway_step():
2123
2224 # apply the rules of life
2325 if self and not (2 <= num_neighbours <= 3):
24- lcd.reset (x, y) # not enough, or too many neighbours: cell dies
26+ lcd.pixel (x, y, 0 ) # not enough, or too many neighbours: cell dies
2527 elif not self and num_neighbours == 3:
26- lcd.set (x, y) # exactly 3 neigbours around an empty cell: cell is born
28+ lcd.pixel (x, y, 1 ) # exactly 3 neigbours around an empty cell: cell is born
2729
2830# randomise the start
2931def conway_rand():
30- lcd.clear( ) # clear the LCD
32+ lcd.fill(0 ) # clear the LCD
3133 for x in range(128): # loop over x coordinates
3234 for y in range(32): # loop over y coordinates
33- if pyb.rand() & 1: # get a 1-bit random number
34- lcd.set(x, y) # set the pixel randomly
35+ lcd.pixel(x, y, pyb.rng() & 1) # set the pixel randomly
3536
3637# loop for a certain number of frames, doing iterations of Conway's Game of Life
3738def conway_go(num_frames):
3839 for i in range(num_frames):
3940 conway_step() # do 1 iteration
4041 lcd.show() # update the LCD
41- pyb.delay(300 )
42+ pyb.delay(50 )
4243
43- # PC testing
44- lcd = lcd.LCD(128, 32)
44+ # testing
4545conway_rand()
46- conway_go(1000 )
46+ conway_go(100 )
0 commit comments