# import essential librariesimport pyblcd = pyb.LCD("x")lcd.light(1)# do 1 iteration of Conway's Game of Lifedef conway_step():for x in range(128): # loop over x coordinatesfor y in range(32): # loop over y coordinates# count number of neighboursnum_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))# check if the centre cell is alive or notself = lcd.get(x, y)# apply the rules of lifeif self and not (2 <= num_neighbours <= 3):lcd.pixel(x, y, 0) # not enough, or too many neighbours: cell dieselif not self and num_neighbours == 3:lcd.pixel(x, y, 1) # exactly 3 neighbours around an empty cell: cell is born# randomise the startdef conway_rand():lcd.fill(0) # clear the LCDfor x in range(128): # loop over x coordinatesfor y in range(32): # loop over y coordinateslcd.pixel(x, y, pyb.rng() & 1) # set the pixel randomly# loop for a certain number of frames, doing iterations of Conway's Game of Lifedef conway_go(num_frames):for i in range(num_frames):conway_step() # do 1 iterationlcd.show() # update the LCDpyb.delay(50)# testingconway_rand()conway_go(100)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。