Python RGB Matrix games and animations https://www.xythobuz.de/ledmatrix_v2.html
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

life.py 4.5KB

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