Commits

Chas Leichner  committed 83562e4

better mole randomization implemented

  • Participants
  • Parent commits 93cbeca

Comments (0)

Files changed (1)

File games/mole/mole.py

 PLUGIN_AUTHOR = 'Chas Leichner + Kevin Luikens'
 PLUGIN_DESCRIPTION = 'Whack-A-Mole! game'
 
+def generate_num(density):
+    if random.random() < density:
+        return 1
+    else:
+        return 0
 
 class CircleDrawer( MTWidget ):
     '''Draw a circle at the position of all touches.'''
         self.time = time.time()
         kwargs.setdefault('matrix_size', (5, 5))
         super(Mole, self).__init__(**kwargs)
-        self.matrix = self.random_matrix(0.3)
+        self.matrix = [[generate_num(0.2) for i in range(self.matrix_size[0])] for j in range(self.matrix_size[1])]
         #TODO
         #self.player_images = (MTWidget(),MTSvg(filename='cross.svg'),MTSvg(filename='circle.svg') )
         self.done = False
             self.last_tile = (i, j)
 
     def random_matrix(self, density):
-        def generate_num():
-            if random.random() > density:
-                return 1
-            else:
-                return 0
+        sustain_factor = 5
+        create_factor = 3
+        assert sustain_factor * density < 1
+        assert density / create_factor > 0
 
-        return [[generate_num() for x in xrange(len(self.matrix))] for y in xrange(len(self.matrix[0]))]
+        new_matrix = []
+        for row in self.matrix:
+            new_line = []
+            for cell in row:
+                if cell:
+                    new_line.append(generate_num(density * sustain_factor))
+                else:
+                    new_line.append(generate_num(density / create_factor))
+            new_matrix.append(new_line)
+                   
+        return new_matrix
+
+    def random_flop(self, density):
+        for row in self.matrix:
+            for cell in row:
+                if random.random() < density:
+                    if cell:
+                        cell = 0
+                    else:
+                        cell = 1
 
     def draw(self):
+        # using time.time may cause problems on windows (if something weird is
+        # happening, check here)
+
         current_time = time.time()
-        if current_time - self.time > 2:
+        if current_time - self.time > 0.25:
             self.time = current_time
-            self.matrix = self.random_matrix(0.3)
+            self.matrix = self.random_matrix(0.15)
 
         for i in range (self._matrix_size[0]):
             for j in range (self._matrix_size[1]):
     #     self.get_parent_window().add_widget(popup)
 
 def pymt_plugin_activate(w,ctx):
-    ctx.game = Mole( size = w.size, buttoncolor=(0,0,0,0), downcolor=(1,1,1,1) )
+    ctx.game = Mole( size = w.size, matrix_size=(10,10), buttoncolor=(0,0,0,0), downcolor=(1,1,1,1) )
     w.add_widget( ctx.game )
 
 def pymt_plugin_deactivate(w, ctx):