Python RGB Matrix games and animations https://www.xythobuz.de/ledmatrix_v2.html
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

snake.py 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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.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. now = time.time()
  116. if (now - self.last) >= self.timestep:
  117. self.last = now
  118. # only allow valid inputs
  119. tmp = self.direction + self.directionTmp
  120. if (tmp != "lr") and (tmp != "rl") and (tmp != "ud") and (tmp != "du"):
  121. self.direction = self.directionTmp
  122. cont = self.step()
  123. if cont == False:
  124. self.direction = ""
  125. self.scoreText.setText("Score: " + str(self.score), "uushi")
  126. self.winText.restart()
  127. self.loseText.restart()
  128. self.scoreText.restart()
  129. for x in range(0, self.gui.width):
  130. for y in range(0, self.gui.height):
  131. self.gui.set_pixel(x, y, self.colors[self.data[x][y]])
  132. if __name__ == "__main__":
  133. import util
  134. t = util.getTarget()
  135. from gamepad import InputWrapper
  136. i = InputWrapper()
  137. d = Snake(t, i)
  138. t.loop(d.draw)