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.

runner.py 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. from scroll import ScrollText
  9. import time
  10. import random
  11. import util
  12. class TunnelRun:
  13. def __init__(self, g, i, t = 0.10, to = 60.0):
  14. self.gui = g
  15. self.input = i
  16. self.timestep = t
  17. self.timeout = to
  18. self.winText = ScrollText(self.gui, "You Won!", "uushi",
  19. 1, 50, (0, 255, 0))
  20. self.loseText = ScrollText(self.gui, "Game Over!", "uushi",
  21. 1, 50, (255, 0, 0))
  22. self.scoreText = ScrollText(self.gui, "Score:", "uushi",
  23. 3, 50, (255, 0, 255))
  24. DrawText = util.getTextDrawer()
  25. self.text = DrawText(self.gui, (0, 0, 255))
  26. self.restart()
  27. def restart(self):
  28. self.start = time.time()
  29. self.last = time.time()
  30. self.direction = "r"
  31. self.directionTmp = "r"
  32. self.done = False
  33. self.player = [ int(self.gui.width / 2), int(self.gui.height / 2) ]
  34. self.old_keys = {
  35. "left": False,
  36. "right": False,
  37. "up": False,
  38. "down": False,
  39. "a": False,
  40. "b": False,
  41. "x": False,
  42. "y": False,
  43. "l": False,
  44. "r": False,
  45. "start": False,
  46. "select": False,
  47. }
  48. def finished(self):
  49. if self.input == None:
  50. # backup timeout for "AI"
  51. if (time.time() - self.start) >= self.timeout:
  52. return True
  53. if self.done:
  54. # game over screen
  55. return self.scoreText.finished()
  56. return False
  57. def buttons(self):
  58. keys = self.input.get()
  59. if keys["left"] and (not self.old_keys["select"]):
  60. self.player[0] = self.player[0] - 1
  61. if keys["right"] and (not self.old_keys["select"]):
  62. self.player[0] = self.player[0] + 1
  63. if keys["up"] and (not self.old_keys["select"]):
  64. self.player[1] = self.player[1] - 1
  65. if keys["down"] and (not self.old_keys["select"]):
  66. self.player[1] = self.player[1] + 1
  67. if (keys["select"] and keys["start"] and (not self.old_keys["start"])) or (keys["start"] and keys["select"] and (not self.old_keys["select"])):
  68. self.restart()
  69. self.old_keys = keys.copy()
  70. if (self.player[0] < 0) or (self.player[1] < 0) or (self.player[0] >= self.gui.width) or (self.player[1] >= self.gui.height):
  71. return False
  72. return True
  73. def step(self):
  74. return True
  75. def finishedEndScreen(self):
  76. now = time.time()
  77. score = int(now - self.start)
  78. # TODO win condition
  79. if score >= self.gui.width * self.gui.height:
  80. return self.winText.finished()
  81. else:
  82. return self.loseText.finished()
  83. def drawEndScreen(self):
  84. now = time.time()
  85. score = int(now - self.start)
  86. # TODO win condition
  87. if score >= self.gui.width * self.gui.height:
  88. self.winText.draw()
  89. else:
  90. self.loseText.draw()
  91. def die(self):
  92. self.done = True
  93. now = time.time()
  94. score = int(now - self.start)
  95. self.scoreText.setText("Score: " + str(score), "uushi")
  96. self.winText.restart()
  97. self.loseText.restart()
  98. self.scoreText.restart()
  99. def draw(self):
  100. if self.done:
  101. if self.finishedEndScreen():
  102. self.scoreText.draw()
  103. else:
  104. self.drawEndScreen()
  105. self.scoreText.restart()
  106. return
  107. if self.input != None:
  108. cont = self.buttons()
  109. if cont == False:
  110. self.die()
  111. else:
  112. # TODO "AI"
  113. pass
  114. now = time.time()
  115. if (now - self.last) >= self.timestep:
  116. self.last = now
  117. cont = self.step()
  118. if cont == False:
  119. self.die()
  120. #for x in range(0, self.gui.width):
  121. # for y in range(0, self.gui.height):
  122. # self.gui.set_pixel(x, y, (0, 0, 0))
  123. # draw score
  124. now = time.time()
  125. score = int(now - self.start)
  126. self.text.setText(str(score), "tom-thumb")
  127. self.text.draw(-1, self.gui.height / 2 - 2)
  128. # draw player
  129. self.gui.set_pixel(self.player[0], self.player[1], (255, 0, 0))
  130. if __name__ == "__main__":
  131. # Need to import InputWrapper before initializing RGB Matrix on Pi
  132. i = util.getInput()
  133. t = util.getTarget(i)
  134. d = TunnelRun(t, i)
  135. util.loop(t, d.draw)