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.0KB

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