1. Kevin Luikens
  2. whackamole

Commits

Chas Leichner  committed 095ab9e

scoring, losing and restarting added

  • Participants
  • Parent commits daa4993
  • Branches default

Comments (0)

Files changed (1)

File games/mole/mole.py

View file
  • Ignore whitespace
 PLUGIN_AUTHOR = 'Chas Leichner + Kevin Luikens'
 PLUGIN_DESCRIPTION = 'Whack-A-Mole! game'
 
+class GameOver(MTWidget):
+    def __init__(self, **kwargs):
+        super(GameOver, self).__init__(**kwargs)
+        self.game = kwargs.get('game')
+        self.text = kwargs.get('text')
+
+    def draw(self):
+        set_color(0, 0, 0, .9)
+        center=Vector(self.get_parent_window().size)/2
+        drawRectangle(pos=(0, center.y-50), size=(self.get_parent_window().width, 100))
+        drawLabel(label=self.text, pos=center, font_size=20)
+
+    def on_touch_down(self, *largs):
+        self.parent.remove_widget(self)
+        self.game.restart()
+        return True
+
+    def on_touch_move(self, *largs):
+        return True
+
+    def on_touch_up(self, *largs):
+        return True
+
 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.'''
-    
-    def draw(self):
-        set_color( 1, 0, 0 )
-        for touch in getCurrentTouches():
-            drawCircle( touch.pos, 50 )
-
 class GameMenu( MTBoxLayout ):
     
     def __init__(self, window, **kwargs):
         super( GameMenu, self ).__init__(**kwargs)
         self.window = window
         self.trial_num = 0
+
+def get_high_score():
+    high_score = 0
+    with open('scoreboard.txt', 'r') as f:
+        high_score = int(f.read().strip())
+    return high_score
+
+def write_high_score(score):
+    with open('scoreboard.txt', 'w') as f:
+        f.write(str(score)+'\n')
     
 class Mole( MTButtonMatrix ):
     
     def __init__(self,**kwargs):
-        self.time = time.time()
+        self.start_time = self.time = time.time()
         kwargs.setdefault('matrix_size', (5, 5))
         super(Mole, self).__init__(**kwargs)
         self.density = 0.1
         self.done = False
         self.score = 0
 
+    def restart(self):
+        pass
+
+    def show_gameover(self, text):
+        popup = GameOver(game=self, text=text)
+        self.get_parent_window().add_widget(popup)
+
     def matrix_proportion(self, matrix):
         size = len(matrix) * len(matrix[0])
         on = 0.0
         return on/size
 
     def on_touch_down(self, touch):
+        if self.done:
+            return True
         if self.collide_point(touch.x, touch.y):
             i, j = self.collide_point(touch.x, touch.y)
             #print i, j
             if self.matrix[i][j]:
                 self.matrix[i][j] = -2
-                self.score+=100
+                self.score+=10
 
             self.dispatch_event('on_value_change', self.matrix)
             self.dispatch_event('on_press', (i, j, self.matrix[i][j]))
                         cell = 1
 
     def draw_tile(self, i, j):
+        if self.done:
+            return 
         destroy_color = (1,0,0,1)
         if self.matrix[i][j] == 0:
             set_color(*self.buttoncolor)
     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 > 0.35:
             self.time = current_time
             self.matrix = self.random_matrix(self.density)
 
+        if current_time - self.start_time > 3 and self.matrix_proportion(self.matrix) < 0.5:
+            high_score = get_high_score()
+            self.show_gameover("Game Over!\nYour Score: %d\nHigest Score:%d" % (self.score, high_score))
+            if self.score > high_score:
+                write_high_score(self.score)
+            self.done = True
+
         for i in range (self._matrix_size[0]):
             for j in range (self._matrix_size[1]):
                 self.draw_tile(i, j)
 
     def restart(self):
+        self.start_time = self.time = time.time()
         self.done = False
-        self.player = 1
-        self.testpoint = (0,0)
+        self.density = 0.1
+        self.matrix = [[generate_num(self.density) for i in range(self.matrix_size[0])] for j in range(self.matrix_size[1])]
+        self.done = False
+        self.score = 0
         self.reset()
     
     # def show_winner(self, text):