changeset 346:6baf8b5beb5c

Remove the "constant" constant
author Neil Muller <drnlmuller@gmail.com>
date Sat, 05 Sep 2009 23:11:16 +0000
parents 279974cc0698
children 35f09e0ccd16
files gamelib/constants.py gamelib/engine.py gamelib/gameboard.py gamelib/mainmenu.py
diffstat 4 files changed, 19 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/gamelib/constants.py	Sat Sep 05 23:08:34 2009 +0000
+++ b/gamelib/constants.py	Sat Sep 05 23:11:16 2009 +0000
@@ -52,8 +52,7 @@
         'Unlimited' : 0,
         }
 
-# This is a "constant".
-TURN_LIMIT = TURN_LIMITS['Two weeks']
+DEFAULT_MODE = 'Two weeks'
 
 ABS_MAX_NUM_FOXES = 50 # Limit possible uppoer number of foxes, due to concerns
                         # about performance, etc.
--- a/gamelib/engine.py	Sat Sep 05 23:08:34 2009 +0000
+++ b/gamelib/engine.py	Sat Sep 05 23:11:16 2009 +0000
@@ -33,7 +33,11 @@
 
     def create_game_board(self):
         """Create and open a gameboard window."""
-        self.gameboard = gameboard.GameBoard(self.main_app)
+        self.mode = self.main_menu.get_mode()
+        if not self.mode:
+            self.mode = constants.DEFAULT_MODE
+        self.gameboard = gameboard.GameBoard(self.main_app,
+                constants.TURN_LIMITS[self.mode])
         self.open_window(self.gameboard.get_top_widget())
 
     def set_main_menu(self):
@@ -47,10 +51,8 @@
 
     def create_game_over(self):
         """Create and open the Game Over window"""
-        for mode, days in constants.TURN_LIMITS.iteritems():
-            if days == constants.TURN_LIMIT:
-                game_over = gameover.create_game_over(self.gameboard,
-                        self.scoreboard[mode], mode)
+        game_over = gameover.create_game_over(self.gameboard,
+                self.scoreboard[self.mode], self.mode)
         self.gameboard = None
         self.open_window(game_over)
 
--- a/gamelib/gameboard.py	Sat Sep 05 23:08:34 2009 +0000
+++ b/gamelib/gameboard.py	Sat Sep 05 23:11:16 2009 +0000
@@ -317,7 +317,7 @@
         (animal.Rinkhals, 1),
         )
 
-    def __init__(self, main_app):
+    def __init__(self, main_app, max_turns):
         self.disp = main_app
         self.tv = tiles.FarmVid()
         self.tv.png_folder_load_tiles('tiles')
@@ -325,6 +325,7 @@
         height, width = self.tv.size
         # Ensure we don't every try to create more foxes then is sane
         self.max_foxes = min(height+width-15, constants.ABS_MAX_NUM_FOXES)
+        self.max_turns = max_turns
         self.create_display()
 
         self.selected_tool = None
@@ -826,9 +827,10 @@
 
     def advance_day(self):
         self.days += 1
-        if self.days == constants.TURN_LIMIT:
+        if self.days == self.max_turns:
             self.toolbar.day_counter.style.color = (255, 0, 0)
-        self.toolbar.update_day_counter("%s/%s" % (self.days, constants.TURN_LIMIT if constants.TURN_LIMIT > 0 else "-"))
+        self.toolbar.update_day_counter("%s/%s" % (self.days,
+            self.max_turns if self.max_turns > 0 else "-"))
 
     def clear_foxes(self):
         for fox in self.foxes.copy():
@@ -1060,7 +1062,7 @@
         """Return true if we're complete"""
         if self.trees_left() == 0:
             return True
-        if constants.TURN_LIMIT > 0 and self.days >= constants.TURN_LIMIT:
+        if self.max_turns > 0 and self.days >= self.max_turns:
             return True
         if len(self.chickens) == 0:
             return True
--- a/gamelib/mainmenu.py	Sat Sep 05 23:08:34 2009 +0000
+++ b/gamelib/mainmenu.py	Sat Sep 05 23:11:16 2009 +0000
@@ -22,9 +22,13 @@
         pygame.display.get_surface().blit(splash, (0, 0))
         gui.Container.paint(self, s)
 
+    def get_mode(self):
+        return self.widgets[0].mode
+
 class MainMenu(gui.Table):
     def __init__(self, **params):
         gui.Table.__init__(self, **params)
+        self.mode = None
 
         def fullscreen_toggled():
             pygame.display.toggle_fullscreen()
@@ -33,7 +37,7 @@
             pygame.event.post(engine.QUIT)
 
         def start_game(mode):
-            constants.TURN_LIMIT = constants.TURN_LIMITS[mode]
+            self.mode = mode
             pygame.event.post(engine.START_DAY)
 
         def help_pressed():