|
@@ -0,0 +1,164 @@
|
|
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 util
|
|
14
|
+
|
|
15
|
+class TunnelRun:
|
|
16
|
+ def __init__(self, g, i, t = 0.10, to = 60.0):
|
|
17
|
+ self.gui = g
|
|
18
|
+ self.input = i
|
|
19
|
+ self.timestep = t
|
|
20
|
+ self.timeout = to
|
|
21
|
+
|
|
22
|
+ self.winText = ScrollText(self.gui, "You Won!", "uushi",
|
|
23
|
+ 1, 50, (0, 255, 0))
|
|
24
|
+ self.loseText = ScrollText(self.gui, "Game Over!", "uushi",
|
|
25
|
+ 1, 50, (255, 0, 0))
|
|
26
|
+ self.scoreText = ScrollText(self.gui, "Score:", "uushi",
|
|
27
|
+ 3, 50, (255, 0, 255))
|
|
28
|
+
|
|
29
|
+ DrawText = util.getTextDrawer()
|
|
30
|
+ self.text = DrawText(self.gui, (0, 0, 255))
|
|
31
|
+
|
|
32
|
+ self.restart()
|
|
33
|
+
|
|
34
|
+ def restart(self):
|
|
35
|
+ self.start = time.time()
|
|
36
|
+ self.last = time.time()
|
|
37
|
+ self.direction = "r"
|
|
38
|
+ self.directionTmp = "r"
|
|
39
|
+ self.done = False
|
|
40
|
+
|
|
41
|
+ self.player = [ int(self.gui.width / 2), int(self.gui.height / 2) ]
|
|
42
|
+
|
|
43
|
+ self.old_keys = {
|
|
44
|
+ "left": False,
|
|
45
|
+ "right": False,
|
|
46
|
+ "up": False,
|
|
47
|
+ "down": False,
|
|
48
|
+ "a": False,
|
|
49
|
+ "b": False,
|
|
50
|
+ "x": False,
|
|
51
|
+ "y": False,
|
|
52
|
+ "l": False,
|
|
53
|
+ "r": False,
|
|
54
|
+ "start": False,
|
|
55
|
+ "select": False,
|
|
56
|
+ }
|
|
57
|
+
|
|
58
|
+ def finished(self):
|
|
59
|
+ if self.input == None:
|
|
60
|
+ # backup timeout for "AI"
|
|
61
|
+ if (time.time() - self.start) >= self.timeout:
|
|
62
|
+ return True
|
|
63
|
+
|
|
64
|
+ if self.done:
|
|
65
|
+ # game over screen
|
|
66
|
+ return self.scoreText.finished()
|
|
67
|
+
|
|
68
|
+ return False
|
|
69
|
+
|
|
70
|
+ def buttons(self):
|
|
71
|
+ keys = self.input.get()
|
|
72
|
+
|
|
73
|
+ if keys["left"] and (not self.old_keys["select"]):
|
|
74
|
+ self.player[0] = self.player[0] - 1
|
|
75
|
+ if keys["right"] and (not self.old_keys["select"]):
|
|
76
|
+ self.player[0] = self.player[0] + 1
|
|
77
|
+ if keys["up"] and (not self.old_keys["select"]):
|
|
78
|
+ self.player[1] = self.player[1] - 1
|
|
79
|
+ if keys["down"] and (not self.old_keys["select"]):
|
|
80
|
+ self.player[1] = self.player[1] + 1
|
|
81
|
+ if (keys["select"] and keys["start"] and (not self.old_keys["start"])) or (keys["start"] and keys["select"] and (not self.old_keys["select"])):
|
|
82
|
+ self.restart()
|
|
83
|
+
|
|
84
|
+ self.old_keys = keys.copy()
|
|
85
|
+
|
|
86
|
+ if (self.player[0] < 0) or (self.player[1] < 0) or (self.player[0] >= self.gui.width) or (self.player[1] >= self.gui.height):
|
|
87
|
+ return False
|
|
88
|
+
|
|
89
|
+ return True
|
|
90
|
+
|
|
91
|
+ def step(self):
|
|
92
|
+ return True
|
|
93
|
+
|
|
94
|
+ def finishedEndScreen(self):
|
|
95
|
+ now = time.time()
|
|
96
|
+ score = int(now - self.start)
|
|
97
|
+ # TODO win condition
|
|
98
|
+ if score >= self.gui.width * self.gui.height:
|
|
99
|
+ return self.winText.finished()
|
|
100
|
+ else:
|
|
101
|
+ return self.loseText.finished()
|
|
102
|
+
|
|
103
|
+ def drawEndScreen(self):
|
|
104
|
+ now = time.time()
|
|
105
|
+ score = int(now - self.start)
|
|
106
|
+ # TODO win condition
|
|
107
|
+ if score >= self.gui.width * self.gui.height:
|
|
108
|
+ self.winText.draw()
|
|
109
|
+ else:
|
|
110
|
+ self.loseText.draw()
|
|
111
|
+
|
|
112
|
+ def die(self):
|
|
113
|
+ self.done = True
|
|
114
|
+ now = time.time()
|
|
115
|
+ score = int(now - self.start)
|
|
116
|
+ self.scoreText.setText("Score: " + str(score), "uushi")
|
|
117
|
+ self.winText.restart()
|
|
118
|
+ self.loseText.restart()
|
|
119
|
+ self.scoreText.restart()
|
|
120
|
+
|
|
121
|
+ def draw(self):
|
|
122
|
+ if self.done:
|
|
123
|
+ if self.finishedEndScreen():
|
|
124
|
+ self.scoreText.draw()
|
|
125
|
+ else:
|
|
126
|
+ self.drawEndScreen()
|
|
127
|
+ self.scoreText.restart()
|
|
128
|
+ return
|
|
129
|
+
|
|
130
|
+ if self.input != None:
|
|
131
|
+ cont = self.buttons()
|
|
132
|
+ if cont == False:
|
|
133
|
+ self.die()
|
|
134
|
+ else:
|
|
135
|
+ # TODO "AI"
|
|
136
|
+ pass
|
|
137
|
+
|
|
138
|
+ now = time.time()
|
|
139
|
+ if (now - self.last) >= self.timestep:
|
|
140
|
+ self.last = now
|
|
141
|
+ cont = self.step()
|
|
142
|
+ if cont == False:
|
|
143
|
+ self.die()
|
|
144
|
+
|
|
145
|
+ #for x in range(0, self.gui.width):
|
|
146
|
+ # for y in range(0, self.gui.height):
|
|
147
|
+ # self.gui.set_pixel(x, y, (0, 0, 0))
|
|
148
|
+
|
|
149
|
+ # draw score
|
|
150
|
+ now = time.time()
|
|
151
|
+ score = int(now - self.start)
|
|
152
|
+ self.text.setText(str(score), "tom-thumb")
|
|
153
|
+ self.text.draw(-1, self.gui.height / 2 - 2)
|
|
154
|
+
|
|
155
|
+ # draw player
|
|
156
|
+ self.gui.set_pixel(self.player[0], self.player[1], (255, 0, 0))
|
|
157
|
+
|
|
158
|
+if __name__ == "__main__":
|
|
159
|
+ # Need to import InputWrapper before initializing RGB Matrix on Pi
|
|
160
|
+ i = util.getInput()
|
|
161
|
+ t = util.getTarget(i)
|
|
162
|
+
|
|
163
|
+ d = TunnelRun(t, i)
|
|
164
|
+ util.loop(t, d.draw)
|