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.

breakout.py 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 math
  12. import util
  13. class Breakout:
  14. def __init__(self, g, i, ts = 0.025, to = 60.0, ria = True):
  15. self.gui = g
  16. self.input = i
  17. self.timestep = ts
  18. self.timeout = to
  19. self.randomInitialAngle = ria
  20. self.paddle_width = 9
  21. self.winText = ScrollText(self.gui, "You Won!", "uushi",
  22. 1, 50, (0, 255, 0))
  23. self.loseText = ScrollText(self.gui, "Game Over!", "uushi",
  24. 1, 50, (255, 0, 0))
  25. self.scoreText = ScrollText(self.gui, "Score:", "uushi",
  26. 3, 50, (255, 255, 255))
  27. self.bg_c = (0, 0, 0)
  28. self.fg_c = (0, 255, 0)
  29. self.ball_c = (255, 0, 0)
  30. self.paddle_c = (255, 255, 255)
  31. self.text_c = (0, 0, 255)
  32. if self.randomInitialAngle:
  33. random.seed()
  34. self.restart()
  35. def place(self):
  36. self.player = int(self.gui.width / 2)
  37. self.ball = [
  38. self.player, # x
  39. self.gui.height - 2, # y
  40. math.sqrt(.5), # v x
  41. -math.sqrt(.5), # v y
  42. ]
  43. if self.randomInitialAngle:
  44. angle_degree = random.randrange(-45, 45)
  45. self.ball[2] = -1 * math.sin(angle_degree / 180 * 3.14159)
  46. self.ball[3] = -1 * math.cos(angle_degree / 180 * 3.14159)
  47. def restart(self):
  48. self.start = time.time()
  49. self.last = time.time()
  50. self.lives = 3
  51. self.score = 0
  52. self.direction = ""
  53. self.data = [[self.bg_c for y in range(self.gui.height)] for x in range(self.gui.width)]
  54. self.maxScore = 0
  55. for x in range(5,self.gui.width - 5):
  56. for y in range(5,10):
  57. self.data[x][y] = self.fg_c
  58. self.maxScore += 1
  59. # TODO easy mode
  60. self.nothing_to_lose = False
  61. DrawText = util.getTextDrawer()
  62. self.text = DrawText(self.gui, self.text_c)
  63. self.place()
  64. self.old_keys = {
  65. "left": False,
  66. "right": False,
  67. "up": False,
  68. "down": False,
  69. "a": False,
  70. "b": False,
  71. "x": False,
  72. "y": False,
  73. "l": False,
  74. "r": False,
  75. "start": False,
  76. "select": False,
  77. }
  78. def finished(self):
  79. if self.input == None:
  80. # backup timeout for "AI"
  81. if (time.time() - self.start) >= self.timeout:
  82. return True
  83. if self.lives < 0:
  84. # game over screen
  85. return self.scoreText.finished()
  86. return False
  87. def buttons(self, next_step=False):
  88. keys = self.input.get()
  89. if keys["left"] and (not self.old_keys["left"] or next_step) and (not self.old_keys["select"]):
  90. self.direction = "l"
  91. elif keys["right"] and (not self.old_keys["right"] or next_step) and (not self.old_keys["select"]):
  92. self.direction = "r"
  93. elif (keys["select"] and keys["start"] and (not self.old_keys["start"])) or (keys["start"] and keys["select"] and (not self.old_keys["select"])):
  94. self.restart()
  95. self.old_keys = keys.copy()
  96. def step(self):
  97. # move ball
  98. old_ball = self.ball.copy()
  99. self.ball[0] += self.ball[2]
  100. self.ball[1] += self.ball[3]
  101. # check for collision with left wall
  102. if self.ball[0] <= 0:
  103. self.ball[2] = -self.ball[2]
  104. self.ball[0] = 0
  105. # check for collision with right wall
  106. if self.ball[0] >= self.gui.width - 1:
  107. self.ball[2] = -self.ball[2]
  108. self.ball[0] = self.gui.width - 1
  109. # check for collision with ceiling
  110. if self.ball[1] <= 0:
  111. self.ball[3] = -self.ball[3]
  112. # check for collisions with pieces
  113. grid_pos_x = int(self.ball[0])
  114. grid_pos_y = int(self.ball[1])
  115. if self.data[grid_pos_x][grid_pos_y] != self.bg_c:
  116. self.data[grid_pos_x][grid_pos_y] = self.bg_c
  117. self.score += 1
  118. old_grid_pos_x = int(old_ball[0])
  119. old_grid_pos_y = int(old_ball[1])
  120. # horizontal collision
  121. if old_grid_pos_y == grid_pos_y:
  122. self.ball[2] = -self.ball[2]
  123. # vertical collision
  124. elif old_grid_pos_x == grid_pos_x:
  125. self.ball[3] = -self.ball[3]
  126. # "diagonal" collision with horizontal obstacle
  127. elif self.data[old_grid_pos_x-1][old_grid_pos_y] != self.bg_c or self.data[old_grid_pos_x+1][old_grid_pos_y] != self.bg_c:
  128. self.ball[2] = -self.ball[2]
  129. # "diagonal" collision with vertical obstacle
  130. elif self.data[old_grid_pos_x][old_grid_pos_y-1] != self.bg_c or self.data[old_grid_pos_x][old_grid_pos_y+1] != self.bg_c:
  131. self.ball[3] = -self.ball[3]
  132. # "diagonal" collision without obstacle
  133. else:
  134. self.ball[2] = -self.ball[2]
  135. self.ball[3] = -self.ball[3]
  136. # check for collision with floor
  137. if self.ball[1] >= self.gui.height - 1:
  138. if self.nothing_to_lose:
  139. # TODO should this bounce with an angle?
  140. self.ball[3] = -self.ball[3]
  141. else:
  142. self.place()
  143. self.lives -= 1
  144. # check for collision with paddle
  145. elif self.ball[1] >= self.gui.height - 2:
  146. pos_on_paddle = self.player - self.ball[0]
  147. if abs(pos_on_paddle) > self.paddle_width/2:
  148. return
  149. # if hit exactly in the middle the direction of the angle depens on the x-direction it came from
  150. if pos_on_paddle == 0:
  151. pos_on_paddle = -0.5 if self.ball[3] > 0 else 0.5
  152. # small angles in the middle, big angles at the end of the paddle (angle measured against the orthogonal of the paddle)
  153. angle_degree = 80 * pos_on_paddle / (self.paddle_width/2)
  154. self.ball[2] = -1 * math.sin(angle_degree/180*3.14159)
  155. self.ball[3] = -1 * math.cos(angle_degree/180*3.14159)
  156. def finishedEndScreen(self):
  157. if self.score >= self.maxScore:
  158. return self.winText.finished()
  159. else:
  160. return self.loseText.finished()
  161. def drawEndScreen(self):
  162. if self.score >= self.maxScore:
  163. self.winText.draw()
  164. else:
  165. self.loseText.draw()
  166. def drawScoreScreen(self):
  167. self.scoreText.draw()
  168. def draw(self):
  169. now = time.time()
  170. # handle / generate player inputs
  171. if self.input != None:
  172. self.buttons((now - self.last) >= self.timestep)
  173. else:
  174. # TODO "AI"
  175. pass
  176. # only draw end-cards when game is over
  177. if (self.lives < 0) or (self.score >= self.maxScore):
  178. if self.finishedEndScreen():
  179. self.drawScoreScreen()
  180. else:
  181. self.drawEndScreen()
  182. self.scoreText.restart()
  183. return
  184. # move paddle according to player input
  185. if self.direction == "l":
  186. self.player = max(self.player - 1, 0)
  187. elif self.direction == "r":
  188. self.player = min(self.player + 1, self.gui.width - 1)
  189. self.direction = ""
  190. # run next iteration
  191. now = time.time()
  192. if (now - self.last) >= self.timestep:
  193. self.last = now
  194. self.step()
  195. # end game when all lives lost or when won
  196. if (self.lives < 0) or (self.score >= self.maxScore):
  197. self.scoreText.setText("Score: " + str(self.score), "uushi")
  198. self.winText.restart()
  199. self.loseText.restart()
  200. self.scoreText.restart()
  201. # draw targets on playing area
  202. for x in range(0, self.gui.width):
  203. for y in range(0, self.gui.height):
  204. self.gui.set_pixel(x, y, self.data[x][y])
  205. # draw score
  206. self.text.setText(str(self.score), "tom-thumb")
  207. self.text.draw(-1, self.gui.height / 2 - 2)
  208. # draw lives
  209. self.text.setText(str(self.lives), "tom-thumb")
  210. self.text.draw(-self.gui.width + 4, self.gui.height / 2 - 2)
  211. # draw paddle
  212. for x in range(0, self.paddle_width):
  213. self.gui.set_pixel(x + self.player - int(self.paddle_width / 2), self.gui.height - 1, self.paddle_c)
  214. # draw ball
  215. self.gui.set_pixel(int(self.ball[0]), int(self.ball[1]), self.ball_c)
  216. if __name__ == "__main__":
  217. # Need to import InputWrapper before initializing RGB Matrix on Pi
  218. i = util.getInput()
  219. t = util.getTarget(i)
  220. d = Breakout(t, i)
  221. # example color modifications
  222. d.fg_c = (0, 150, 0)
  223. d.ball_c = (150, 0, 0)
  224. d.paddle_c = (150, 150, 150)
  225. d.text_c = (0, 0, 150)
  226. d.restart() # re-gen with new colors
  227. util.loop(t, d.draw)