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.1KB

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