Python RGB Matrix games and animations https://www.xythobuz.de/ledmatrix_v2.html
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

manager.py 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. if update_flag:
  63. self.done = (self.index == 0)
  64. # go through all for normal operation
  65. self.index = (self.index + i) % len(self.screens)
  66. else:
  67. # use step_size for button presses
  68. self.index = int((int(self.index / self.step_size) + i) * self.step_size) % len(self.screens)
  69. #print("Manager ", len(self.screens), " switch to ", self.index, update_flag)
  70. self.screens[self.index][0].restart()
  71. def draw(self):
  72. if self.input != None:
  73. self.buttons()
  74. self.screens[self.index][0].draw()
  75. if self.screens[self.index][1] == None:
  76. # let screen decide when it is done
  77. if self.screens[self.index][0].finished():
  78. self.switch_to(1, True)
  79. else:
  80. # use given timeout
  81. now = time.time()
  82. if ((now - self.lastTime) > self.screens[self.index][1]) or (now < self.lastTime):
  83. self.switch_to(1, True)
  84. if __name__ == "__main__":
  85. from splash import SplashScreen
  86. from scroll import ScrollText
  87. from solid import Solid
  88. from life import GameOfLife
  89. import util
  90. i = util.getInput()
  91. t = util.getTarget(i)
  92. splash = SplashScreen(t)
  93. t.loop_start()
  94. splash.draw()
  95. t.loop_end()
  96. m = Manager(t, i)
  97. sub = Manager(t)
  98. sub.add(ScrollText(t, "Hello", "ib8x8u"))
  99. sub.add(Solid(t, 1.0, (0, 255, 0)))
  100. sub.add(ScrollText(t, "World", "ib8x8u"))
  101. sub.add(Solid(t, 1.0, (0, 0, 255)))
  102. m.add(sub)
  103. m.add(Solid(t, 1.0))
  104. m.add(ScrollText(t, "This appears once", "ib8x8u"))
  105. m.add(Solid(t, 1.0))
  106. m.add(ScrollText(t, "And this twice...", "ib8x8u", 2))
  107. m.add(Solid(t, 1.0))
  108. m.add(GameOfLife(t, 20, (0, 255, 0), (0, 0, 0), 5.0, True))
  109. m.add(Solid(t, 1.0))
  110. m.restart()
  111. util.loop(t, m.draw)