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 4.2KB

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