Python RGB Matrix games and animations https://www.xythobuz.de/ledmatrix_v2.html
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

life.py 3.9KB

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