Python RGB Matrix games and animations https://www.xythobuz.de/ledmatrix_v2.html
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

manager.py 5.0KB

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