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

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