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.

snake.py 7.0KB

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