Python RGB Matrix games and animations https://www.xythobuz.de/ledmatrix_v2.html
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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