Python RGB Matrix games and animations https://www.xythobuz.de/ledmatrix_v2.html
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

life.py 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #!/usr/bin/env python3
  2. import time
  3. import random
  4. class GameOfLife:
  5. def __init__(self, g, f = 20, c1 = (255, 255, 255), c2 = (0, 0, 0), t = 20.0):
  6. self.gui = g
  7. self.interval = 1.0 / f
  8. self.setColors(c1, c2)
  9. self.timeout = t
  10. random.seed()
  11. self.restart()
  12. def restart(self):
  13. self.data = self.init()
  14. self.start = time.time()
  15. self.last = time.time()
  16. self.done = False
  17. def setColors(self, c1, c2):
  18. self.colorFG = c1
  19. self.colorBG = c2
  20. def init(self):
  21. data = []
  22. for x in range(0, self.gui.width):
  23. d = []
  24. for y in range(0, self.gui.height):
  25. v = False
  26. if random.randrange(0, 2) == 1:
  27. v = True
  28. d.append(v)
  29. data.append(d)
  30. return data
  31. def finished(self):
  32. if self.done or ((time.time() - self.start) > self.timeout):
  33. return True
  34. return False
  35. def alive(self, data, x, y):
  36. if (x < 0) or (y < 0) or (x >= self.gui.width) or (y >= self.gui.height):
  37. return False
  38. return data[x][y]
  39. def live_neighbours(self, data, x, y):
  40. c = 0
  41. for xOff in range(-1, 2):
  42. for yOff in range(-1, 2):
  43. if (xOff == 0) and (yOff == 0):
  44. continue
  45. if self.alive(data, x + xOff, y + yOff):
  46. c += 1
  47. return c
  48. def step(self):
  49. # deep copy
  50. old = [x[:] for x in self.data]
  51. for x in range(0, self.gui.width):
  52. for y in range(0, self.gui.height):
  53. ln = self.live_neighbours(old, x, y)
  54. if old[x][y] and ((ln == 2) or (ln == 3)):
  55. # Any live cell with two or three live neighbours survives.
  56. self.data[x][y] = True
  57. elif (not old[x][y]) and (ln == 3):
  58. # Any dead cell with three live neighbours becomes a live cell.
  59. self.data[x][y] = True
  60. else:
  61. # All other live cells die in the next generation. Similarly, all other dead cells stay dead.
  62. self.data[x][y] = False
  63. # compare new and old states
  64. same = True
  65. for x in range(0, self.gui.width):
  66. for y in range(0, self.gui.height):
  67. if self.data[x][y] != old[x][y]:
  68. same = False
  69. break
  70. self.done = same
  71. def draw(self):
  72. if (time.time() - self.last) > self.interval:
  73. self.last = time.time()
  74. self.step()
  75. for x in range(0, self.gui.width):
  76. for y in range(0, self.gui.height):
  77. if self.data[x][y]:
  78. self.gui.set_pixel(x, y, self.colorFG)
  79. else:
  80. self.gui.set_pixel(x, y, self.colorBG)
  81. if __name__ == "__main__":
  82. import platform
  83. t = None
  84. if platform.machine() == "armv7l":
  85. from pi import PiMatrix
  86. t = PiMatrix()
  87. else:
  88. from test import TestGUI
  89. t = TestGUI()
  90. g = GameOfLife(t)
  91. def helperRestart():
  92. c = (random.randrange(0, 256), random.randrange(0, 256), random.randrange(0, 256))
  93. g.setColors(c, (0, 0, 0))
  94. # start out with random colors
  95. helperRestart()
  96. def helper():
  97. if g.finished():
  98. g.restart()
  99. helperRestart()
  100. g.draw()
  101. t.debug_loop(helper)