Browse Source

gol random color cycling

Thomas Buck 1 year ago
parent
commit
6b585f69f3
1 changed files with 12 additions and 10 deletions
  1. 12
    10
      life.py

+ 12
- 10
life.py View File

11
 import random
11
 import random
12
 
12
 
13
 class GameOfLife:
13
 class GameOfLife:
14
-    def __init__(self, g, f = 20, c1 = (255, 255, 255), c2 = (0, 0, 0), t = 20.0, rc = False):
14
+    def __init__(self, g, f = 20, c1 = (255, 255, 255), c2 = (0, 0, 0), t = 20.0, rc = None):
15
         self.gui = g
15
         self.gui = g
16
         self.interval = 1.0 / f
16
         self.interval = 1.0 / f
17
         self.setColors(c1, c2)
17
         self.setColors(c1, c2)
24
         self.data = self.init()
24
         self.data = self.init()
25
         self.start = time.time()
25
         self.start = time.time()
26
         self.last = time.time()
26
         self.last = time.time()
27
+        self.lastColor = time.time()
27
         self.done = False
28
         self.done = False
28
 
29
 
29
-        if self.randomizeColors:
30
+        if self.randomizeColors != None:
30
             self.randomize()
31
             self.randomize()
31
 
32
 
32
-    def setColors(self, c1, c2):
33
+    def setColors(self, c1 = (255, 255, 255), c2 = (0, 0, 0)):
33
         self.colorFG = c1
34
         self.colorFG = c1
34
         self.colorBG = c2
35
         self.colorBG = c2
35
 
36
 
36
     def randomize(self):
37
     def randomize(self):
37
-        c1 = (random.randrange(0, 256), random.randrange(0, 256), random.randrange(0, 256))
38
-        c2 = (random.randrange(0, 128), random.randrange(0, 128), random.randrange(0, 128))
38
+        c1 = (random.randrange(0, 16) << 4, random.randrange(0, 16) << 4, random.randrange(0, 16) << 4)
39
+        c2 = (random.randrange(0, 16) << 0, random.randrange(0, 16) << 0, random.randrange(0, 16) << 0)
39
         self.setColors(c1, c2)
40
         self.setColors(c1, c2)
40
 
41
 
41
     def init(self):
42
     def init(self):
105
             self.last = time.time()
106
             self.last = time.time()
106
             self.step()
107
             self.step()
107
 
108
 
109
+        if (self.randomizeColors != None) and (self.randomizeColors != True):
110
+            if (time.time() - self.lastColor) > self.randomizeColors:
111
+                self.lastColor = time.time()
112
+                g.randomize()
113
+
108
         for x in range(0, self.gui.width):
114
         for x in range(0, self.gui.width):
109
             for y in range(0, self.gui.height):
115
             for y in range(0, self.gui.height):
110
                 if self.data[x][y]:
116
                 if self.data[x][y]:
122
         from test import TestGUI
128
         from test import TestGUI
123
         t = TestGUI()
129
         t = TestGUI()
124
 
130
 
125
-    g = GameOfLife(t)
126
-
127
-    # start out with random colors
128
-    g.randomize()
131
+    g = GameOfLife(t, 20, (255, 255, 255), (0, 0, 0), 20.0, 2.0)
129
 
132
 
130
     def helper():
133
     def helper():
131
         if g.finished():
134
         if g.finished():
132
             g.restart()
135
             g.restart()
133
-            g.randomize()
134
         g.draw()
136
         g.draw()
135
 
137
 
136
     t.debug_loop(helper)
138
     t.debug_loop(helper)

Loading…
Cancel
Save