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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. if c == 4:
  54. # 4 or more is not interesting for us
  55. break
  56. return c
  57. def step(self):
  58. # deep copy
  59. old = [x[:] for x in self.data]
  60. for x in range(0, self.gui.width):
  61. for y in range(0, self.gui.height):
  62. ln = self.live_neighbours(old, x, y)
  63. if old[x][y] and ((ln == 2) or (ln == 3)):
  64. # Any live cell with two or three live neighbours survives.
  65. self.data[x][y] = True
  66. elif (not old[x][y]) and (ln == 3):
  67. # Any dead cell with three live neighbours becomes a live cell.
  68. self.data[x][y] = True
  69. else:
  70. # All other live cells die in the next generation. Similarly, all other dead cells stay dead.
  71. self.data[x][y] = False
  72. # compare new and old states
  73. same = True
  74. for x in range(0, self.gui.width):
  75. for y in range(0, self.gui.height):
  76. if self.data[x][y] != old[x][y]:
  77. same = False
  78. break
  79. self.done = same
  80. def draw(self):
  81. if (time.time() - self.last) > self.interval:
  82. self.last = time.time()
  83. self.step()
  84. for x in range(0, self.gui.width):
  85. for y in range(0, self.gui.height):
  86. if self.data[x][y]:
  87. self.gui.set_pixel(x, y, self.colorFG)
  88. else:
  89. self.gui.set_pixel(x, y, self.colorBG)
  90. if __name__ == "__main__":
  91. import platform
  92. t = None
  93. if platform.machine() == "armv7l":
  94. from pi import PiMatrix
  95. t = PiMatrix()
  96. else:
  97. from test import TestGUI
  98. t = TestGUI()
  99. g = GameOfLife(t)
  100. def helperRestart():
  101. c1 = (random.randrange(0, 256), random.randrange(0, 256), random.randrange(0, 256))
  102. c2 = (random.randrange(0, 128), random.randrange(0, 128), random.randrange(0, 128))
  103. g.setColors(c1, c2)
  104. # start out with random colors
  105. helperRestart()
  106. def helper():
  107. if g.finished():
  108. g.restart()
  109. helperRestart()
  110. g.draw()
  111. t.debug_loop(helper)