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 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 = 43#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 = True
  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):
  81. keys = self.input.get()
  82. if keys["left"] and (not self.old_keys["left"]) and (not self.old_keys["select"]):
  83. self.direction = "l"
  84. elif keys["right"] and (not self.old_keys["right"]) 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 paddle
  112. if (self.ball[1] == self.gui.height - 2) and (self.ball[0] >= (self.player - int(self.paddle_width / 2))) and (self.ball[0] <= (self.player + int(self.paddle_width / 2))):
  113. # TODO angle for bounce from paddle
  114. #self.ball[3] = -self.ball[3]
  115. d = self.ball[0] - (self.player - (self.paddle_width / 2))
  116. angle = (d / self.paddle_width - 0.5) / 2 * 3.14159
  117. print(math.degrees(angle))
  118. self.ball[2] = math.cos(angle) * math.sqrt(2)
  119. self.ball[3] = math.sin(angle) * math.sqrt(2)
  120. # check for collision with floor
  121. if self.ball[1] >= self.gui.height - 1:
  122. if self.nothing_to_lose:
  123. # TODO should this bounce with an angle?
  124. self.ball[3] = -self.ball[3]
  125. else:
  126. self.place()
  127. self.lives -= 1
  128. def finishedEndScreen(self):
  129. if self.score >= self.maxScore:
  130. return self.winText.finished()
  131. else:
  132. return self.loseText.finished()
  133. def drawEndScreen(self):
  134. if self.score >= self.maxScore:
  135. self.winText.draw()
  136. else:
  137. self.loseText.draw()
  138. def drawScoreScreen(self):
  139. self.scoreText.draw()
  140. def draw(self):
  141. # handle / generate player inputs
  142. if self.input != None:
  143. self.buttons()
  144. else:
  145. # TODO "AI"
  146. pass
  147. # only draw end-cards when game is over
  148. if (self.lives < 0) or (self.score >= self.maxScore):
  149. if self.finishedEndScreen():
  150. self.drawScoreScreen()
  151. else:
  152. self.drawEndScreen()
  153. self.scoreText.restart()
  154. return
  155. # move paddle according to player input
  156. if self.direction == "l":
  157. self.player = max(self.player - 1, 0)
  158. elif self.direction == "r":
  159. self.player = min(self.player + 1, self.gui.width - 1)
  160. self.direction = ""
  161. # run next iteration
  162. now = time.time()
  163. if (now - self.last) >= self.timestep:
  164. self.last = now
  165. self.step()
  166. # end game when all lives lost or when won
  167. if (self.lives < 0) or (self.score >= self.maxScore):
  168. self.scoreText.setText("Score: " + str(self.score), "uushi")
  169. self.winText.restart()
  170. self.loseText.restart()
  171. self.scoreText.restart()
  172. # draw targets on playing area
  173. for x in range(0, self.gui.width):
  174. for y in range(0, self.gui.height):
  175. self.gui.set_pixel(x, y, self.data[x][y])
  176. # draw score
  177. self.text.setText(str(self.score), "tom-thumb")
  178. self.text.draw(-1, self.gui.height / 2 - 2)
  179. # draw lives
  180. self.text.setText(str(self.lives), "tom-thumb")
  181. self.text.draw(-self.gui.width + 4, self.gui.height / 2 - 2)
  182. # draw paddle
  183. for x in range(0, self.paddle_width):
  184. self.gui.set_pixel(x + self.player - int(self.paddle_width / 2), self.gui.height - 1, self.paddle_c)
  185. # draw ball
  186. self.gui.set_pixel(int(self.ball[0]), int(self.ball[1]), self.ball_c)
  187. if __name__ == "__main__":
  188. # Need to import InputWrapper before initializing RGB Matrix on Pi
  189. i = util.getInput()
  190. t = util.getTarget(i)
  191. d = Breakout(t, i)
  192. # example color modifications
  193. d.fg_c = (0, 150, 0)
  194. d.ball_c = (150, 0, 0)
  195. d.paddle_c = (150, 150, 150)
  196. d.text_c = (0, 0, 150)
  197. d.restart() # re-gen with new colors
  198. util.loop(t, d.draw)