Python RGB Matrix games and animations https://www.xythobuz.de/ledmatrix_v2.html
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

breakout.py 9.6KB

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