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

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