Browse Source

randomization for manager

Thomas Buck 1 year ago
parent
commit
2779ddad94
2 changed files with 55 additions and 22 deletions
  1. 30
    18
      livingroom.py
  2. 25
    4
      manager.py

+ 30
- 18
livingroom.py View File

9
 
9
 
10
 camp_pink = (251, 72, 196)
10
 camp_pink = (251, 72, 196)
11
 camp_green = (63, 255, 33)
11
 camp_green = (63, 255, 33)
12
+pause = 2.0
12
 
13
 
13
 from splash import SplashScreen
14
 from splash import SplashScreen
14
 from scroll import ScrollText
15
 from scroll import ScrollText
22
 from manager import Manager
23
 from manager import Manager
23
 from tetris import Tetris
24
 from tetris import Tetris
24
 from breakout import Breakout
25
 from breakout import Breakout
25
-from weather import WeatherScreen
26
 from config import Config
26
 from config import Config
27
 import util
27
 import util
28
 
28
 
37
 splash.draw()
37
 splash.draw()
38
 t.loop_end()
38
 t.loop_end()
39
 
39
 
40
-m = Manager(t, i)
40
+m = Manager(t, i, 2, True)
41
 
41
 
42
-m.add(ImageScreen(t, "cann.png"))
43
-m.add(Solid(t, 1.0))
44
-
45
-m.add(WeatherScreen(t, i, Config.weather_latlon))
46
-m.add(Solid(t, 1.0))
42
+if not util.isPi():
43
+    # TODO weather does not run on Pi yet unfortunately :(
44
+    from weather import WeatherScreen
45
+    m.add(WeatherScreen(t, i, Config.weather_latlon))
46
+    m.add(Solid(t, pause))
47
 
47
 
48
 m.add(GameOfLife(t, 20, (0, 255, 0), (0, 0, 0), None, 2.0))
48
 m.add(GameOfLife(t, 20, (0, 255, 0), (0, 0, 0), None, 2.0))
49
-m.add(Solid(t, 1.0))
49
+m.add(Solid(t, pause))
50
+
51
+m.add(ImageScreen(t, "32_earth.gif", 0.2, 2))
52
+m.add(Solid(t, pause))
53
+
54
+m.add(ImageScreen(t, "cann.png", 0.2, 1, 10.0))
55
+m.add(Solid(t, pause))
56
+
57
+m.add(ImageScreen(t, "cann2.png", 0.2, 1, 10.0))
58
+m.add(Solid(t, pause))
59
+
60
+m.add(ImageScreen(t, "64_cloud.gif", 0.2, 1, 5.0, (0, 0, 0)))
61
+m.add(Solid(t, pause))
50
 
62
 
51
-m.add(ImageScreen(t, "cann2.png"))
52
-m.add(Solid(t, 1.0))
63
+m.add(ImageScreen(t, "64_sephiroth.gif", 0.2, 4, 5.0, (0, 0, 0)))
64
+m.add(Solid(t, pause))
53
 
65
 
54
-m.add(ImageScreen(t, "earth.gif"))
55
-m.add(Solid(t, 1.0))
66
+m.add(ImageScreen(t, "64_nlogospin.gif", 0.2, 2))
67
+m.add(Solid(t, pause))
56
 
68
 
57
-#m.add(Breakout(t, i))
58
-#m.add(Solid(t, 1.0))
69
+m.add(Breakout(t, i))
70
+m.add(Solid(t, pause))
59
 
71
 
60
-#m.add(Tetris(t, i,))
61
-#m.add(Solid(t, 1.0))
72
+m.add(Tetris(t, i,))
73
+m.add(Solid(t, pause))
62
 
74
 
63
-#m.add(Snake(t, i, camp_pink, camp_green))
64
-#m.add(Solid(t, 1.0))
75
+m.add(Snake(t, i, camp_pink, camp_green))
76
+m.add(Solid(t, pause))
65
 
77
 
66
 m.restart()
78
 m.restart()
67
 util.loop(t, m.draw)
79
 util.loop(t, m.draw)

+ 25
- 4
manager.py View File

8
 # ----------------------------------------------------------------------------
8
 # ----------------------------------------------------------------------------
9
 
9
 
10
 import time
10
 import time
11
+import random
11
 
12
 
12
 class Manager:
13
 class Manager:
13
-    def __init__(self, g, i = None, ss = 2):
14
+    def __init__(self, g, i = None, ss = 2, randomize = False):
14
         self.gui = g
15
         self.gui = g
15
         self.input = i
16
         self.input = i
16
         self.step_size = ss
17
         self.step_size = ss
18
+        self.randomize = randomize
17
         self.screens = []
19
         self.screens = []
20
+
21
+        if self.randomize:
22
+            random.seed()
23
+
18
         self.restart()
24
         self.restart()
19
 
25
 
20
     def restart(self):
26
     def restart(self):
21
-        self.index = 0
27
+        if self.randomize and (len(self.screens) > 0):
28
+            self.index = int(random.randrange(0, len(self.screens) / self.step_size) * self.step_size)
29
+        else:
30
+            self.index = 0
31
+
22
         self.done = False
32
         self.done = False
23
         self.lastTime = time.time()
33
         self.lastTime = time.time()
24
         self.old_keys = {
34
         self.old_keys = {
65
         self.lastTime = time.time()
75
         self.lastTime = time.time()
66
 
76
 
67
         if update_flag:
77
         if update_flag:
68
-            # go through all for normal operation
69
-            self.index = (self.index + i) % len(self.screens)
78
+            if self.randomize:
79
+                if (self.index % self.step_size) == (self.step_size - 1):
80
+                    # end of "segment", now go to random next segment
81
+                    new_index = self.index
82
+                    while (new_index == self.index) and (len(self.screens) > self.step_size):
83
+                        new_index = int(random.randrange(0, len(self.screens) / self.step_size) * self.step_size)
84
+                    self.index = new_index
85
+                else:
86
+                    # still in "segment", so just normal iteration
87
+                    self.index = (self.index + i) % len(self.screens)
88
+            else:
89
+                # go through all for normal operation
90
+                self.index = (self.index + i) % len(self.screens)
70
         else:
91
         else:
71
             # use step_size for button presses
92
             # use step_size for button presses
72
             self.index = int((int(self.index / self.step_size) + i) * self.step_size) % len(self.screens)
93
             self.index = int((int(self.index / self.step_size) + i) * self.step_size) % len(self.screens)

Loading…
Cancel
Save