|
@@ -0,0 +1,189 @@
|
|
1
|
+#!/usr/bin/env python3
|
|
2
|
+
|
|
3
|
+# ----------------------------------------------------------------------------
|
|
4
|
+# "THE BEER-WARE LICENSE" (Revision 42):
|
|
5
|
+# <xythobuz@xythobuz.de> wrote this file. As long as you retain this notice
|
|
6
|
+# you can do whatever you want with this stuff. If we meet some day, and you
|
|
7
|
+# think this stuff is worth it, you can buy me a beer in return. Thomas Buck
|
|
8
|
+# ----------------------------------------------------------------------------
|
|
9
|
+
|
|
10
|
+from scroll import ScrollText
|
|
11
|
+import time
|
|
12
|
+import random
|
|
13
|
+import math
|
|
14
|
+
|
|
15
|
+class Breakout:
|
|
16
|
+ def __init__(self, g, i, ts = 0.1, to = 60.0):
|
|
17
|
+ self.gui = g
|
|
18
|
+ self.input = i
|
|
19
|
+ self.timestep = ts
|
|
20
|
+ self.timeout = to
|
|
21
|
+
|
|
22
|
+ self.paddle_width = 9
|
|
23
|
+
|
|
24
|
+ self.winText = ScrollText(self.gui, "You Won!", "uushi",
|
|
25
|
+ 2, 50, (0, 255, 0))
|
|
26
|
+ self.loseText = ScrollText(self.gui, "Game Over!", "uushi",
|
|
27
|
+ 2, 50, (255, 0, 0))
|
|
28
|
+ self.scoreText = ScrollText(self.gui, "Score:", "uushi",
|
|
29
|
+ 2, 50, (255, 255, 255))
|
|
30
|
+
|
|
31
|
+ random.seed()
|
|
32
|
+ self.restart()
|
|
33
|
+
|
|
34
|
+ def place(self):
|
|
35
|
+ self.player = int(self.gui.width / 2)
|
|
36
|
+ self.ball = [
|
|
37
|
+ self.player, # x
|
|
38
|
+ self.gui.height - 2, # y
|
|
39
|
+ 1, # v x
|
|
40
|
+ -1, # v y
|
|
41
|
+ ]
|
|
42
|
+
|
|
43
|
+ def restart(self):
|
|
44
|
+ self.start = time.time()
|
|
45
|
+ self.last = time.time()
|
|
46
|
+ self.lives = 3
|
|
47
|
+ self.score = 0
|
|
48
|
+ self.direction = ""
|
|
49
|
+
|
|
50
|
+ self.data = [[(0, 0, 0) for y in range(self.gui.height)] for x in range(self.gui.width)]
|
|
51
|
+
|
|
52
|
+ for x in range(self.gui.width - 2):
|
|
53
|
+ for y in range(5):
|
|
54
|
+ self.data[x + 1][y] = (0, 255, 0)
|
|
55
|
+
|
|
56
|
+ self.place()
|
|
57
|
+
|
|
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
|
+
|
|
73
|
+ def finished(self):
|
|
74
|
+ if self.input == None:
|
|
75
|
+ # backup timeout for "AI"
|
|
76
|
+ if (time.time() - self.start) >= self.timeout:
|
|
77
|
+ return True
|
|
78
|
+
|
|
79
|
+ if self.lives < 0:
|
|
80
|
+ # game over screen
|
|
81
|
+ return self.scoreText.finished()
|
|
82
|
+
|
|
83
|
+ return False
|
|
84
|
+
|
|
85
|
+ def buttons(self):
|
|
86
|
+ keys = self.input.get()
|
|
87
|
+
|
|
88
|
+ if keys["left"] and (not self.old_keys["left"]) and (not self.old_keys["select"]):
|
|
89
|
+ self.direction = "l"
|
|
90
|
+ elif keys["right"] and (not self.old_keys["right"]) and (not self.old_keys["select"]):
|
|
91
|
+ self.direction = "r"
|
|
92
|
+ elif (keys["select"] and keys["start"] and (not self.old_keys["start"])) or (keys["start"] and keys["select"] and (not self.old_keys["select"])):
|
|
93
|
+ self.restart()
|
|
94
|
+
|
|
95
|
+ self.old_keys = keys.copy()
|
|
96
|
+
|
|
97
|
+ def step(self):
|
|
98
|
+ # TODO check for collisions with pieces
|
|
99
|
+
|
|
100
|
+ # move ball
|
|
101
|
+ self.ball[0] += self.ball[2]
|
|
102
|
+ self.ball[1] += self.ball[3]
|
|
103
|
+
|
|
104
|
+ # check for collision with left wall
|
|
105
|
+ if self.ball[0] <= 0:
|
|
106
|
+ self.ball[2] = -self.ball[2]
|
|
107
|
+
|
|
108
|
+ # check for collision with right wall
|
|
109
|
+ if self.ball[0] >= self.gui.width - 1:
|
|
110
|
+ self.ball[2] = -self.ball[2]
|
|
111
|
+
|
|
112
|
+ # check for collision with ceiling
|
|
113
|
+ if self.ball[1] <= 0:
|
|
114
|
+ self.ball[3] = -self.ball[3]
|
|
115
|
+
|
|
116
|
+ # check for collision with paddle
|
|
117
|
+ 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))):
|
|
118
|
+ # TODO angle
|
|
119
|
+ self.ball[3] = -self.ball[3]
|
|
120
|
+
|
|
121
|
+ # check for collision with floor
|
|
122
|
+ if self.ball[1] >= self.gui.height - 1:
|
|
123
|
+ self.place()
|
|
124
|
+ self.lives -= 1
|
|
125
|
+
|
|
126
|
+ def finishedEndScreen(self):
|
|
127
|
+ if self.score >= self.gui.width * self.gui.height:
|
|
128
|
+ return self.winText.finished()
|
|
129
|
+ else:
|
|
130
|
+ return self.loseText.finished()
|
|
131
|
+
|
|
132
|
+ def drawEndScreen(self):
|
|
133
|
+ if self.score >= self.gui.width * self.gui.height:
|
|
134
|
+ self.winText.draw()
|
|
135
|
+ else:
|
|
136
|
+ self.loseText.draw()
|
|
137
|
+
|
|
138
|
+ def drawScoreScreen(self):
|
|
139
|
+ self.scoreText.draw()
|
|
140
|
+
|
|
141
|
+ def draw(self):
|
|
142
|
+ if self.input != None:
|
|
143
|
+ self.buttons()
|
|
144
|
+ else:
|
|
145
|
+ # TODO "AI"
|
|
146
|
+ pass
|
|
147
|
+
|
|
148
|
+ if self.lives < 0:
|
|
149
|
+ if self.finishedEndScreen():
|
|
150
|
+ self.drawScoreScreen()
|
|
151
|
+ else:
|
|
152
|
+ self.drawEndScreen()
|
|
153
|
+ self.scoreText.restart()
|
|
154
|
+ return
|
|
155
|
+
|
|
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
|
+
|
|
162
|
+ now = time.time()
|
|
163
|
+ if (now - self.last) >= self.timestep:
|
|
164
|
+ self.last = now
|
|
165
|
+ self.step()
|
|
166
|
+
|
|
167
|
+ if self.lives < 0:
|
|
168
|
+ self.scoreText.setText("Score: " + str(self.score), "uushi")
|
|
169
|
+ self.winText.restart()
|
|
170
|
+ self.loseText.restart()
|
|
171
|
+ self.scoreText.restart()
|
|
172
|
+
|
|
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
|
+
|
|
177
|
+ for x in range(0, self.paddle_width):
|
|
178
|
+ self.gui.set_pixel(x + self.player - int(self.paddle_width / 2), self.gui.height - 1, (255, 255, 255))
|
|
179
|
+
|
|
180
|
+ self.gui.set_pixel(self.ball[0], self.ball[1], (255, 0, 0))
|
|
181
|
+
|
|
182
|
+if __name__ == "__main__":
|
|
183
|
+ import util
|
|
184
|
+ # Need to import InputWrapper before initializing RGB Matrix on Pi
|
|
185
|
+ i = util.getInput()
|
|
186
|
+ t = util.getTarget(i)
|
|
187
|
+
|
|
188
|
+ d = Breakout(t, i)
|
|
189
|
+ util.loop(t, d.draw)
|