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

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