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 6.7KB

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