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

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