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.

breakout.py 8.9KB

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