Python RGB Matrix games and animations https://www.xythobuz.de/ledmatrix_v2.html
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

manager.py 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. self.switch_to(-1, False)
  36. elif keys["r"] and (not self.old_keys["r"]):
  37. self.switch_to(1, False)
  38. self.old_keys = keys.copy()
  39. def child_count(self, i, update_flag):
  40. if i > 0:
  41. if update_flag:
  42. index = self.index
  43. l = len(self.screens)
  44. else:
  45. index = int(self.index / self.step_size)
  46. l = int(len(self.screens) / self.step_size)
  47. #print(self.index, len(self.screens), index, l, (index >= (l - 1)))
  48. return index >= (l - 1)
  49. else:
  50. return self.index <= 0
  51. def switch_this_to(self, i, update_flag):
  52. self.lastTime = time.time()
  53. if update_flag:
  54. # go through all for normal operation
  55. self.index = (self.index + i) % len(self.screens)
  56. else:
  57. # use step_size for button presses
  58. self.index = int((int(self.index / self.step_size) + i) * self.step_size) % len(self.screens)
  59. self.done = self.child_count(i, update_flag)
  60. #print("Manager ", len(self.screens), " switch to ", self.index, update_flag)
  61. self.screens[self.index][0].restart()
  62. def switch_to(self, i, update_flag):
  63. c = self.screens[self.index][0]
  64. if hasattr(c, "switch_to") and hasattr(c, "child_count"):
  65. if c.child_count(i, update_flag):
  66. self.switch_this_to(i, update_flag)
  67. else:
  68. c.switch_to(i, update_flag)
  69. else:
  70. self.switch_this_to(i, update_flag)
  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. sub = Manager(t)
  97. sub.add(ScrollText(t, "Hello", "ib8x8u"))
  98. sub.add(Solid(t, 1.0, (0, 255, 0)))
  99. sub.add(ScrollText(t, "World", "ib8x8u"))
  100. sub.add(Solid(t, 1.0, (0, 0, 255)))
  101. m = Manager(t, i)
  102. m.add(sub)
  103. m.add(Solid(t, 1.0, (255, 255, 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)