Python RGB Matrix games and animations https://www.xythobuz.de/ledmatrix_v2.html
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 Snake:
  13. def __init__(self, g, i, sc = (0, 255, 0), d = (0, 0, 255), bg = (0, 0, 0), ts = 0.3, su = 0.75, to = 60.0):
  14. self.gui = g
  15. self.input = i
  16. self.colors = [ bg, sc, d ]
  17. self.timestep = ts
  18. self.timeout = to
  19. self.speedup = su
  20. self.winText = ScrollText(self.gui, "You Won!", "uushi",
  21. 2, 50, (0, 255, 0))
  22. self.loseText = ScrollText(self.gui, "Game Over!", "uushi",
  23. 2, 50, (255, 0, 0))
  24. self.scoreText = ScrollText(self.gui, "Score:", "uushi",
  25. 2, 50, sc)
  26. self.text_c = (0, 0, 255)
  27. random.seed()
  28. self.restart()
  29. def restart(self):
  30. self.start = time.time()
  31. self.last = time.time()
  32. self.direction = "r"
  33. self.directionTmp = "r"
  34. self.score = 0
  35. self.data = [[0 for y in range(self.gui.height)] for x in range(self.gui.width)]
  36. self.player = [ (int(self.gui.width / 2), int(self.gui.height / 2)) ]
  37. self.data[self.player[0][0]][self.player[0][1]] = 1
  38. DrawText = util.getTextDrawer()
  39. self.text = DrawText(self.gui, self.text_c)
  40. self.old_keys = {
  41. "left": False,
  42. "right": False,
  43. "up": False,
  44. "down": False,
  45. "a": False,
  46. "b": False,
  47. "x": False,
  48. "y": False,
  49. "l": False,
  50. "r": False,
  51. "start": False,
  52. "select": False,
  53. }
  54. self.placeDot()
  55. def finished(self):
  56. if self.input == None:
  57. # backup timeout for "AI"
  58. if (time.time() - self.start) >= self.timeout:
  59. return True
  60. if self.direction == "":
  61. # game over screen
  62. return self.scoreText.finished()
  63. return False
  64. def placeDot(self):
  65. d = (random.randrange(0, self.gui.width), random.randrange(0, self.gui.height))
  66. while self.data[d[0]][d[1]] != 0: # TODO don't place dots below score text
  67. d = (random.randrange(0, self.gui.width), random.randrange(0, self.gui.height))
  68. self.data[d[0]][d[1]] = 2
  69. def buttons(self):
  70. keys = self.input.get()
  71. if keys["left"] and (not self.old_keys["left"]) and (not self.old_keys["select"]):
  72. self.directionTmp = "l"
  73. elif keys["right"] and (not self.old_keys["right"]) and (not self.old_keys["select"]):
  74. self.directionTmp = "r"
  75. elif keys["up"] and (not self.old_keys["up"]) and (not self.old_keys["select"]):
  76. self.directionTmp = "u"
  77. elif keys["down"] and (not self.old_keys["down"]) and (not self.old_keys["select"]):
  78. self.directionTmp = "d"
  79. elif (keys["select"] and keys["start"] and (not self.old_keys["start"])) or (keys["start"] and keys["select"] and (not self.old_keys["select"])):
  80. self.restart()
  81. self.old_keys = keys.copy()
  82. def step(self):
  83. player = self.player[len(self.player) - 1]
  84. if self.direction == "r":
  85. player = (player[0] + 1, player[1])
  86. elif self.direction == "l":
  87. player = (player[0] - 1, player[1])
  88. elif self.direction == "u":
  89. player = (player[0], player[1] - 1)
  90. elif self.direction == "d":
  91. player = (player[0], player[1] + 1)
  92. if (player[0] < 0) or (player[1] < 0) or (player[0] >= self.gui.width) or (player[1] >= self.gui.height):
  93. return False
  94. if self.data[player[0]][player[1]] == 0:
  95. # remove last tail piece if not on dot
  96. self.data[self.player[0][0]][self.player[0][1]] = 0
  97. self.player.pop(0)
  98. elif self.data[player[0]][player[1]] == 1:
  99. # snake crashed into itself
  100. return False
  101. else:
  102. # collected a dot
  103. self.score += 1
  104. if self.score >= self.gui.width * self.gui.height:
  105. return False
  106. self.timestep = self.timestep * self.speedup
  107. self.placeDot()
  108. self.player.append(player)
  109. self.data[player[0]][player[1]] = 1
  110. return True
  111. def finishedEndScreen(self):
  112. if self.score >= self.gui.width * self.gui.height:
  113. return self.winText.finished()
  114. else:
  115. return self.loseText.finished()
  116. def drawEndScreen(self):
  117. if self.score >= self.gui.width * self.gui.height:
  118. self.winText.draw()
  119. else:
  120. self.loseText.draw()
  121. def drawScoreScreen(self):
  122. self.scoreText.draw()
  123. def draw(self):
  124. if self.input != None:
  125. self.buttons()
  126. else:
  127. # TODO "AI"
  128. pass
  129. if self.direction == "":
  130. if self.finishedEndScreen():
  131. self.drawScoreScreen()
  132. else:
  133. self.drawEndScreen()
  134. self.scoreText.restart()
  135. return
  136. now = time.time()
  137. if (now - self.last) >= self.timestep:
  138. self.last = now
  139. # only allow valid inputs
  140. tmp = self.direction + self.directionTmp
  141. if (tmp != "lr") and (tmp != "rl") and (tmp != "ud") and (tmp != "du"):
  142. self.direction = self.directionTmp
  143. cont = self.step()
  144. if cont == False:
  145. self.direction = ""
  146. self.scoreText.setText("Score: " + str(self.score), "uushi")
  147. self.winText.restart()
  148. self.loseText.restart()
  149. self.scoreText.restart()
  150. for x in range(0, self.gui.width):
  151. for y in range(0, self.gui.height):
  152. self.gui.set_pixel(x, y, self.colors[self.data[x][y]])
  153. # draw score
  154. self.text.setText(str(self.score), "tom-thumb")
  155. self.text.draw(-1, self.gui.height / 2 - 2)
  156. if __name__ == "__main__":
  157. # Need to import InputWrapper before initializing RGB Matrix on Pi
  158. i = util.getInput()
  159. t = util.getTarget(i)
  160. d = Snake(t, i)
  161. util.loop(t, d.draw)