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

runner.py 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 = self.input.empty() # TODO support missing input
  35. def finished(self):
  36. if self.input == None:
  37. # backup timeout for "AI"
  38. if (time.time() - self.start) >= self.timeout:
  39. return True
  40. if self.done:
  41. # game over screen
  42. return self.scoreText.finished()
  43. return False
  44. def buttons(self):
  45. keys = self.input.get()
  46. if keys["left"] and (not self.old_keys["select"]):
  47. self.player[0] = self.player[0] - 1
  48. if keys["right"] and (not self.old_keys["select"]):
  49. self.player[0] = self.player[0] + 1
  50. if keys["up"] and (not self.old_keys["select"]):
  51. self.player[1] = self.player[1] - 1
  52. if keys["down"] and (not self.old_keys["select"]):
  53. self.player[1] = self.player[1] + 1
  54. if (keys["select"] and keys["start"] and (not self.old_keys["start"])) or (keys["start"] and keys["select"] and (not self.old_keys["select"])):
  55. self.restart()
  56. self.old_keys = keys.copy()
  57. if (self.player[0] < 0) or (self.player[1] < 0) or (self.player[0] >= self.gui.width) or (self.player[1] >= self.gui.height):
  58. return False
  59. return True
  60. def step(self):
  61. return True
  62. def finishedEndScreen(self):
  63. now = time.time()
  64. score = int(now - self.start)
  65. # TODO win condition
  66. if score >= self.gui.width * self.gui.height:
  67. return self.winText.finished()
  68. else:
  69. return self.loseText.finished()
  70. def drawEndScreen(self):
  71. now = time.time()
  72. score = int(now - self.start)
  73. # TODO win condition
  74. if score >= self.gui.width * self.gui.height:
  75. self.winText.draw()
  76. else:
  77. self.loseText.draw()
  78. def die(self):
  79. self.done = True
  80. now = time.time()
  81. score = int(now - self.start)
  82. self.scoreText.setText("Score: " + str(score), "uushi")
  83. self.winText.restart()
  84. self.loseText.restart()
  85. self.scoreText.restart()
  86. def draw(self):
  87. if self.done:
  88. if self.finishedEndScreen():
  89. self.scoreText.draw()
  90. else:
  91. self.drawEndScreen()
  92. self.scoreText.restart()
  93. return
  94. if self.input != None:
  95. cont = self.buttons()
  96. if cont == False:
  97. self.die()
  98. else:
  99. # TODO "AI"
  100. pass
  101. now = time.time()
  102. if (now - self.last) >= self.timestep:
  103. self.last = now
  104. cont = self.step()
  105. if cont == False:
  106. self.die()
  107. #for x in range(0, self.gui.width):
  108. # for y in range(0, self.gui.height):
  109. # self.gui.set_pixel(x, y, (0, 0, 0))
  110. # draw score
  111. now = time.time()
  112. score = int(now - self.start)
  113. self.text.setText(str(score), "tom-thumb")
  114. self.text.draw(-1, self.gui.height / 2 - 2)
  115. # draw player
  116. self.gui.set_pixel(self.player[0], self.player[1], (255, 0, 0))
  117. if __name__ == "__main__":
  118. # Need to import InputWrapper before initializing RGB Matrix on Pi
  119. i = util.getInput()
  120. t = util.getTarget(i)
  121. d = TunnelRun(t, i)
  122. util.loop(t, d.draw)