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

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