Python RGB Matrix games and animations https://www.xythobuz.de/ledmatrix_v2.html
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

manager.py 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. class Manager:
  10. def __init__(self, g, i = None, ss = 2):
  11. self.gui = g
  12. self.input = i
  13. self.step_size = ss
  14. self.screens = []
  15. self.restart()
  16. def restart(self):
  17. self.index = 0
  18. self.done = False
  19. self.lastTime = time.time()
  20. self.old_keys = {
  21. "l": False,
  22. "r": False,
  23. }
  24. #print("Manager ", len(self.screens), " reset to ", self.index)
  25. if len(self.screens) > 0:
  26. self.screens[0][0].restart()
  27. def finished(self):
  28. return self.done
  29. def add(self, s, d = None):
  30. v = (s, d)
  31. self.screens.append(v)
  32. def buttons(self):
  33. keys = self.input.get()
  34. if keys["l"] and (not self.old_keys["l"]):
  35. c = self.screens[self.index][0]
  36. if hasattr(c, "switch_to") and hasattr(c, "child_count"):
  37. if c.child_count(-1):
  38. self.switch_to(-1, False)
  39. else:
  40. c.switch_to(-1, False)
  41. else:
  42. self.switch_to(-1, False)
  43. elif keys["r"] and (not self.old_keys["r"]):
  44. c = self.screens[self.index][0]
  45. if hasattr(c, "switch_to") and hasattr(c, "child_count"):
  46. if c.child_count(1):
  47. self.switch_to(1, False)
  48. else:
  49. c.switch_to(1, False)
  50. else:
  51. self.switch_to(1, False)
  52. self.old_keys = keys.copy()
  53. def child_count(self, i):
  54. if i > 0:
  55. index = int(self.index / self.step_size)
  56. l = int(len(self.screens) / self.step_size)
  57. return index >= (l - 1)
  58. else:
  59. return self.index <= 0
  60. def switch_to(self, i, update_flag):
  61. self.lastTime = time.time()
  62. self.index = int((int(self.index / self.step_size) + i) * self.step_size) % len(self.screens)
  63. #print("Manager ", len(self.screens), " switch to ", self.index, update_flag)
  64. if update_flag:
  65. self.done = (self.index == 0)
  66. self.screens[self.index][0].restart()
  67. def draw(self):
  68. if self.input != None:
  69. self.buttons()
  70. self.screens[self.index][0].draw()
  71. if self.screens[self.index][1] == None:
  72. # let screen decide when it is done
  73. if self.screens[self.index][0].finished():
  74. self.switch_to(1, True)
  75. else:
  76. # use given timeout
  77. now = time.time()
  78. if ((now - self.lastTime) > self.screens[self.index][1]) or (now < self.lastTime):
  79. self.switch_to(1, True)
  80. if __name__ == "__main__":
  81. from splash import SplashScreen
  82. from scroll import ScrollText
  83. from solid import Solid
  84. from life import GameOfLife
  85. import util
  86. t = util.getTarget()
  87. splash = SplashScreen(t)
  88. t.loop_start()
  89. splash.draw()
  90. t.loop_end()
  91. m = Manager(t)
  92. m.add(ScrollText(t, "This appears once", "ib8x8u"))
  93. m.add(Solid(t, 1.0))
  94. m.add(ScrollText(t, "And this twice...", "ib8x8u", 2))
  95. m.add(Solid(t, 1.0))
  96. m.add(GameOfLife(t, 20, (0, 255, 0), (0, 0, 0), 20.0, True))
  97. m.add(Solid(t, 1.0))
  98. sub = Manager(t)
  99. sub.add(ScrollText(t, "Hello", "ib8x8u"))
  100. sub.add(Solid(t, 1.0, (0, 255, 0)))
  101. sub.add(ScrollText(t, "World", "ib8x8u"))
  102. sub.add(Solid(t, 1.0, (0, 0, 255)))
  103. m.add(sub)
  104. m.add(Solid(t, 1.0))
  105. m.restart()
  106. t.loop(m.draw)