Explorar el Código

tweaks, started work on new game

Thomas Buck hace 8 meses
padre
commit
e6f5568e0d
Se han modificado 6 ficheros con 177 adiciones y 13 borrados
  1. 3
    3
      breakout.py
  2. BIN
      images/aniglobe.gif
  3. 164
    0
      runner.py
  4. 3
    3
      snake.py
  5. 2
    2
      tetris.py
  6. 5
    5
      weather.py

+ 3
- 3
breakout.py Ver fichero

@@ -23,11 +23,11 @@ class Breakout:
23 23
         self.paddle_width = 9
24 24
 
25 25
         self.winText = ScrollText(self.gui, "You Won!", "uushi",
26
-                                  2, 50, (0, 255, 0))
26
+                                  1, 50, (0, 255, 0))
27 27
         self.loseText = ScrollText(self.gui, "Game Over!", "uushi",
28
-                                   2, 50, (255, 0, 0))
28
+                                   1, 50, (255, 0, 0))
29 29
         self.scoreText = ScrollText(self.gui, "Score:", "uushi",
30
-                                    2, 50, (255, 255, 255))
30
+                                    3, 50, (255, 255, 255))
31 31
 
32 32
         self.bg_c = (0, 0, 0)
33 33
         self.fg_c = (0, 255, 0)

BIN
images/aniglobe.gif Ver fichero


+ 164
- 0
runner.py Ver fichero

@@ -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)

+ 3
- 3
snake.py Ver fichero

@@ -22,11 +22,11 @@ class Snake:
22 22
         self.speedup = su
23 23
 
24 24
         self.winText = ScrollText(self.gui, "You Won!", "uushi",
25
-                                  2, 50, (0, 255, 0))
25
+                                  1, 50, (0, 255, 0))
26 26
         self.loseText = ScrollText(self.gui, "Game Over!", "uushi",
27
-                                   2, 50, (255, 0, 0))
27
+                                   1, 50, (255, 0, 0))
28 28
         self.scoreText = ScrollText(self.gui, "Score:", "uushi",
29
-                                    2, 50, sc)
29
+                                    3, 50, sc)
30 30
 
31 31
         self.text_c = (0, 0, 255)
32 32
 

+ 2
- 2
tetris.py Ver fichero

@@ -22,9 +22,9 @@ class Tetris:
22 22
         self.height = min(h, self.gui.height)
23 23
 
24 24
         self.endText = ScrollText(self.gui, "Game Over!", "uushi",
25
-                                   2, 50, (251, 72, 196))
25
+                                   1, 50, (251, 72, 196))
26 26
         self.scoreText = ScrollText(self.gui, "Score:", "uushi",
27
-                                    2, 50, (63, 255, 33))
27
+                                    3, 50, (63, 255, 33))
28 28
 
29 29
         self.bg = (0, 0, 0)
30 30
         self.colors = [

+ 5
- 5
weather.py Ver fichero

@@ -261,14 +261,14 @@ class WeatherScreen:
261 261
         # heading
262 262
         self.t_head.setText("Temps:", "lemon")
263 263
         self.t_head.draw(3, -self.gui.height / 2 + 7)
264
+        self.t_sub.setText("Current:", "lemon")
265
+        self.t_sub.draw(3, -self.gui.height / 2 + 20)
264 266
 
265 267
         # current temperature
266
-        self.t_sub.setText("Current:", "tom-thumb")
267
-        self.t_sub.draw(0, -self.gui.height / 2 + 5 + 6 * 2)
268 268
         val = self.data.filter(polars.col("parameter") == self.params[1])[0]["value"][0]
269 269
         val = val - 273.15 # kelvin to celsius
270
-        self.t_val.setText("{:.1f} °C".format(val), "tom-thumb")
271
-        self.t_val.draw(0, -self.gui.height / 2 + 5 + 6 * 3)
270
+        self.t_val.setText("{:.1f}°C".format(val), "tom-thumb")
271
+        self.t_val.draw(0, -self.gui.height / 2 + 5 + 6 * 5)
272 272
 
273 273
     def draw_cloud_cover(self):
274 274
         # heading
@@ -278,7 +278,7 @@ class WeatherScreen:
278 278
         self.t_sub.draw(3, -self.gui.height / 2 + 20)
279 279
 
280 280
         val = self.data.filter(polars.col("parameter") == self.params[2])[0]["value"][0]
281
-        self.t_val.setText("{:.1f} %".format(val), "tom-thumb")
281
+        self.t_val.setText("{:.1f}%".format(val), "tom-thumb")
282 282
         self.t_val.draw(0, -self.gui.height / 2 + 5 + 6 * 5)
283 283
 
284 284
     def draw(self):

Loading…
Cancelar
Guardar