changeset 576:1b1ab71535bd pyntnclick

Classify constants, which involves a whole bunch of XXX comments
author Stefano Rivera <stefano@rivera.za.net>
date Sat, 11 Feb 2012 16:02:06 +0200
parents 970cdc219e15
children ccc26c23d2c1
files gamelib/scenes/bridge.py gamelib/scenes/mess.py pyntnclick/constants.py pyntnclick/cursor.py pyntnclick/gamescreen.py pyntnclick/main.py pyntnclick/scenewidgets.py pyntnclick/sound.py pyntnclick/state.py pyntnclick/widgets/__init__.py scripts/regen-speech.py tools/rect_drawer.py
diffstat 12 files changed, 105 insertions(+), 75 deletions(-) [+]
line wrap: on
line diff
--- a/gamelib/scenes/bridge.py	Sat Feb 11 16:01:33 2012 +0200
+++ b/gamelib/scenes/bridge.py	Sat Feb 11 16:02:06 2012 +0200
@@ -9,11 +9,14 @@
 
 from pyntnclick.cursor import CursorSprite
 from pyntnclick.state import Scene, Item, Thing, Result
-from pyntnclick.constants import DEBUG
 from pyntnclick.scenewidgets import (InteractNoImage, InteractRectUnion,
                                   InteractImage, InteractAnimated,
                                   GenericDescThing)
 
+# XXX: Need a way to get at the constants.
+from pyntnclick.constants import GameConstants
+DEBUG = GameConstants().debug
+
 from gamelib.scenes.game_constants import PLAYER_ID
 from gamelib.scenes.game_widgets import Door, BaseCamera, make_jim_dialog
 
--- a/gamelib/scenes/mess.py	Sat Feb 11 16:01:33 2012 +0200
+++ b/gamelib/scenes/mess.py	Sat Feb 11 16:02:06 2012 +0200
@@ -4,7 +4,6 @@
 
 from pyntnclick.state import Scene, Item, CloneableItem, Thing, Result
 from pyntnclick.cursor import CursorSprite
-from pyntnclick import constants
 from pyntnclick.scenewidgets import (InteractNoImage, InteractImage,
                                   InteractImageRect, InteractAnimated,
                                   GenericDescThing)
@@ -271,7 +270,7 @@
             else:
                 self.set_data('anim_pos', self.current_interact._anim_pos)
             return True
-        if randint(0, 30 * constants.FRAME_RATE) == 0:
+        if randint(0, 30 * self.state.gd.constants.frame_rate) == 0:
             self.set_interact('snake')
             self.set_data('anim_pos', 0)
             hiss.play()
--- a/pyntnclick/constants.py	Sat Feb 11 16:01:33 2012 +0200
+++ b/pyntnclick/constants.py	Sat Feb 11 16:02:06 2012 +0200
@@ -2,17 +2,18 @@
 # copyright boomslang team (see COPYRIGHT file for details)
 
 
-SCREEN = (800, 600)
-FREQ = 44100   # same as audio CD
-BITSIZE = -16  # unsigned 16 bit
-CHANNELS = 2   # 1 == mono, 2 == stereo
-BUFFER = 1024  # audio buffer size in no. of samples
+class GameConstants(object):
+    screen = (800, 600)
+    snd_freq = 44100
+    snd_bitsize = -16
+    snd_channels = 2
+    snd_buffer = 1024  # no. of samples
 
-BUTTON_SIZE = 50
-SCENE_SIZE = (SCREEN[0], SCREEN[1] - BUTTON_SIZE)
-# Animation frame rate
-FRAME_RATE = 25
+    button_size = 50
+    scene_size = (screen[0], screen[1] - button_size)
+    frame_rate = 25
+    debug = False
 
-DEBUG = False
-
-ENTER, LEAVE = 1, 2
+    # User event IDs:
+    enter = 1
+    leave = 2
--- a/pyntnclick/cursor.py	Sat Feb 11 16:01:33 2012 +0200
+++ b/pyntnclick/cursor.py	Sat Feb 11 16:02:06 2012 +0200
@@ -11,7 +11,9 @@
 import pygame.cursors
 import pygame.mouse
 
-from pyntnclick.constants import SCENE_SIZE
+# XXX: Need a way to get at the constants
+from pyntnclick.constants import GameConstants
+SCENE_SIZE = GameConstants().scene_size
 
 
 class CursorSprite(Sprite):
--- a/pyntnclick/gamescreen.py	Sat Feb 11 16:01:33 2012 +0200
+++ b/pyntnclick/gamescreen.py	Sat Feb 11 16:02:06 2012 +0200
@@ -9,12 +9,19 @@
 from pygame import Rect, mouse
 from pygame.color import Color
 
-from pyntnclick.constants import SCREEN, BUTTON_SIZE, SCENE_SIZE, LEAVE
 from pyntnclick.cursor import CursorWidget
 from pyntnclick.state import handle_result
 from pyntnclick.widgets import (
     MessageDialog, BoomButton, HandButton, PopupMenu, PopupMenuButton)
 
+# XXX: Need a way to get at the constants.
+from pyntnclick.constants import GameConstants
+constants = GameConstants()
+SCREEN = constants.screen
+BUTTON_SIZE = constants.button_size
+SCENE_SIZE = constants.scene_size
+LEAVE = constants.leave
+
 
 class InventoryView(PaletteView):
 
--- a/pyntnclick/main.py	Sat Feb 11 16:01:33 2012 +0200
+++ b/pyntnclick/main.py	Sat Feb 11 16:02:06 2012 +0200
@@ -19,20 +19,19 @@
 from pyntnclick.menu import MenuScreen
 from pyntnclick.gamescreen import GameScreen
 from pyntnclick.endscreen import EndScreen
-from pyntnclick.constants import (
-    SCREEN, FRAME_RATE, DEBUG)
+from pyntnclick.constants import GameConstants
 from pyntnclick.resources import Resources
 from pyntnclick.sound import Sound
 from pyntnclick import state
 
 
 class MainShell(Shell):
-    def __init__(self, display, initial_state):
+    def __init__(self, display, initial_state, frame_rate):
         Shell.__init__(self, display)
         self.menu_screen = MenuScreen(self)
         self.game_screen = GameScreen(self, initial_state)
         self.end_screen = EndScreen(self)
-        self.set_timer(FRAME_RATE)
+        self.set_timer(frame_rate)
         self.show_screen(self.menu_screen)
 
 
@@ -63,6 +62,7 @@
         self._debug_rects = False
         self.resource = Resources(self._resource_module)
         self.sound = Sound(self.resource)
+        self.constants = self.game_constants()
 
     def initial_state(self):
         """Create a copy of the initial game state."""
@@ -74,11 +74,14 @@
         initial_state.set_do_enter_leave()
         return initial_state
 
+    def game_constants(self):
+        return GameConstants()
+
     def option_parser(self):
         parser = OptionParser()
         parser.add_option("--no-sound", action="store_false", default=True,
                 dest="sound", help="disable sound")
-        if DEBUG:
+        if self.constants.debug:
             parser.add_option("--scene", type="str", default=None,
                 dest="scene", help="initial scene")
             parser.add_option("--no-rects", action="store_false", default=True,
@@ -91,20 +94,22 @@
         pygame.display.init()
         pygame.font.init()
         if opts.sound:
-            self.sound.enable_sound()
+            self.sound.enable_sound(self.constants)
         else:
             self.sound.disable_sound()
-        if DEBUG:
+        if self.constants.debug:
             if opts.scene is not None:
                 # debug the specified scene
                 self._initial_scene = opts.scene
             self._debug_rects = opts.rects
-        display = pygame.display.set_mode(SCREEN, SWSURFACE)
+        display = pygame.display.set_mode(self.constants.screen,
+                                          SWSURFACE)
         pygame.display.set_icon(pygame.image.load(
             self.resource.get_resource_path('icons/suspended_sentence24x24'
                                             '.png')))
         pygame.display.set_caption("Suspended Sentence")
-        shell = MainShell(display, self.initial_state)
+        shell = MainShell(display, self.initial_state,
+                          self.constants.frame_rate)
         try:
             shell.run()
         except KeyboardInterrupt:
--- a/pyntnclick/scenewidgets.py	Sat Feb 11 16:01:33 2012 +0200
+++ b/pyntnclick/scenewidgets.py	Sat Feb 11 16:02:06 2012 +0200
@@ -8,9 +8,12 @@
 from albow.resource import get_image
 
 from pyntnclick.state import Thing
-from pyntnclick.constants import DEBUG
 from pyntnclick.widgets import BoomLabel
 
+# XXX: Need a way to get at the constants.
+from pyntnclick.constants import GameConstants
+DEBUG = GameConstants().debug
+
 
 class Interact(object):
 
--- a/pyntnclick/sound.py	Sat Feb 11 16:01:33 2012 +0200
+++ b/pyntnclick/sound.py	Sat Feb 11 16:02:06 2012 +0200
@@ -18,7 +18,6 @@
     pygame_Sound = None
     music = None
 
-from pyntnclick.constants import FREQ, BITSIZE, CHANNELS, BUFFER
 from pyntnclick.resources import ResourceNotFound
 
 import albow.music
@@ -61,13 +60,16 @@
         self.sound_cache = {}
         self._resource_finder = resource_finder
 
-    def enable_sound(self):
+    def enable_sound(self, constants):
         """Attempt to initialise the sound system"""
         if pygame_Sound is None:
             self.disable_sound(pygame_import_error)
             return
         try:
-            pygame.mixer.init(FREQ, BITSIZE, CHANNELS, BUFFER)
+            pygame.mixer.init(constants.snd_freq,
+                              constants.snd_bitsize,
+                              constants.snd_channels,
+                              constants.snd_buffer)
             self.sound_enabled = True
         except pygame.error, exc:
             self.disable_sound(exc)
--- a/pyntnclick/state.py	Sat Feb 11 16:01:33 2012 +0200
+++ b/pyntnclick/state.py	Sat Feb 11 16:02:06 2012 +0200
@@ -8,8 +8,6 @@
 from pygame.rect import Rect
 from pygame.color import Color
 
-from pyntnclick import constants
-
 
 class Result(object):
     """Result of interacting with a thing"""
@@ -183,12 +181,12 @@
     def check_enter_leave(self, screen):
         if not self.do_check:
             return None
-        if self.do_check == constants.LEAVE:
-            self.do_check = constants.ENTER
+        if self.do_check == self.gd.constants.leave:
+            self.do_check = self.gd.constants.enter
             if self.previous_scene:
                 return self.previous_scene.leave()
             return None
-        elif self.do_check == constants.ENTER:
+        elif self.do_check == self.gd.constants.enter:
             self.do_check = None
             # Fix descriptions, etc.
             if self.old_pos:
@@ -198,7 +196,7 @@
 
     def set_do_enter_leave(self):
         """Flag that we need to run the enter loop"""
-        self.do_check = constants.LEAVE
+        self.do_check = self.gd.constants.leave
 
 
 class StatefulGizmo(object):
--- a/pyntnclick/widgets/__init__.py	Sat Feb 11 16:01:33 2012 +0200
+++ b/pyntnclick/widgets/__init__.py	Sat Feb 11 16:02:06 2012 +0200
@@ -15,9 +15,12 @@
 from pygame.draw import lines as draw_lines
 from pygame import mouse
 
-from pyntnclick.constants import BUTTON_SIZE
 from pyntnclick.cursor import CursorWidget
 
+# XXX: Need a way to get at the constants.
+from pyntnclick.constants import GameConstants
+BUTTON_SIZE = GameConstants().button_size
+
 
 class BoomLabel(albow.controls.Label):
 
--- a/scripts/regen-speech.py	Sat Feb 11 16:01:33 2012 +0200
+++ b/scripts/regen-speech.py	Sat Feb 11 16:02:06 2012 +0200
@@ -10,11 +10,11 @@
 from albow.resource import resource_path
 
 from pygame.locals import SWSURFACE
-from gamelib.constants import SCREEN
+from gamelib.constants import GameConstants
 
 # We need this stuff set up so we can load images and whatnot.
 pygame.display.init()
-pygame.display.set_mode(SCREEN, SWSURFACE)
+pygame.display.set_mode(GameConstants().screen, SWSURFACE)
 
 
 def espeak(text, filename, voice="en-sc"):
--- a/tools/rect_drawer.py	Sat Feb 11 16:01:33 2012 +0200
+++ b/tools/rect_drawer.py	Sat Feb 11 16:02:06 2012 +0200
@@ -19,18 +19,22 @@
                            BLEND_RGBA_MIN, SRCALPHA)
 import pygame
 
-from pyntnclick import constants
-constants.DEBUG = True
-MENU_WIDTH = 200
-MENU_BUTTON_HEIGHT = 25
-ZOOM = 4
-ZOOM_STEP = 100
-
+import pyntnclick.constants
 from pyntnclick import state
 state.DEBUG_RECTS = True
 from pyntnclick.widgets import BoomLabel
 
 
+class RectDrawerConstants(pyntnclick.constants.GameConstants):
+    debug = True
+    menu_width = 200
+    menu_button_height = 25
+    zoom = 4
+    zoom_step = 100
+
+constants = RectDrawerConstants()
+
+
 class AppPalette(PaletteView):
 
     sel_width = 5
@@ -72,8 +76,8 @@
     def __init__(self, state):
         self.state = state
         super(AppImage, self).__init__(pygame.rect.Rect(0, 0,
-                                                        constants.SCREEN[0],
-                                                        constants.SCREEN[1]))
+                                                        constants.screen[0],
+                                                        constants.screen[1]))
         self.mode = 'draw'
         self.rects = []
         self.images = []
@@ -241,11 +245,11 @@
             base_surface = surface.copy()
             self.do_unzoomed_draw(base_surface)
             zoomed = pygame.transform.scale(base_surface,
-                                            (ZOOM * constants.SCREEN[0],
-                                             ZOOM * constants.SCREEN[1]))
+                            (constants.zoom * constants.screen[0],
+                             constants.zoom * constants.screen[1]))
             area = pygame.rect.Rect(self.zoom_offset[0], self.zoom_offset[1],
-                                    self.zoom_offset[0] + constants.SCREEN[0],
-                                    self.zoom_offset[1] + constants.SCREEN[1])
+                                    self.zoom_offset[0] + constants.screen[0],
+                                    self.zoom_offset[1] + constants.screen[1])
             surface.blit(zoomed, (0, 0), area)
         else:
             self.do_unzoomed_draw(surface)
@@ -294,13 +298,13 @@
                     self.draw_sub_image(self.current_image, surface,
                                         cropped_rect)
         if self.draw_toolbar:
-            tb_surf = surface.subsurface(0, constants.SCREEN[1]
-                                            - constants.BUTTON_SIZE,
-                                         constants.SCREEN[0],
-                                         constants.BUTTON_SIZE).convert_alpha()
+            tb_surf = surface.subsurface(0, constants.screen[1]
+                                            - constants.button_size,
+                                         constants.screen[0],
+                                         constants.button_size).convert_alpha()
             tb_surf.fill(pygame.color.Color(127, 0, 0, 191))
-            surface.blit(tb_surf, (0, constants.SCREEN[1]
-                                      - constants.BUTTON_SIZE))
+            surface.blit(tb_surf, (0, constants.screen[1]
+                                      - constants.button_size))
 
     def _make_dict(self):
         d = {}
@@ -337,8 +341,8 @@
             self.place_image_menu.enabled = True
             # ensure we're off screen to start
             self.current_image.rect = image_data.get_rect() \
-                    .move(constants.SCREEN[0] + MENU_WIDTH,
-                          constants.SCREEN[1])
+                    .move(constants.screen[0] + constants.menu_width,
+                          constants.screen[1])
         except pygame.error, e:
             print 'Unable to load image %s' % e
 
@@ -354,8 +358,8 @@
 
     def _conv_pos(self, mouse_pos):
         if self.zoom_display:
-            pos = ((mouse_pos[0] + self.zoom_offset[0]) / ZOOM,
-                   (mouse_pos[1] + self.zoom_offset[1]) / ZOOM)
+            pos = ((mouse_pos[0] + self.zoom_offset[0]) / constants.zoom,
+                   (mouse_pos[1] + self.zoom_offset[1]) / constants.zoom)
         else:
             pos = mouse_pos
         return pos
@@ -365,22 +369,23 @@
             offset[0] = 0
         if offset[1] < 0:
             offset[1] = 0
-        if offset[0] > ZOOM * constants.SCREEN[0] - constants.SCREEN[0]:
-            offset[0] = ZOOM * constants.SCREEN[0] - constants.SCREEN[0]
-        if offset[1] > ZOOM * constants.SCREEN[1] - constants.SCREEN[1]:
-            offset[1] = ZOOM * constants.SCREEN[1] - constants.SCREEN[1]
+        width, height = constants.screen
+        if offset[0] > constants.zoom * width - width:
+            offset[0] = constants.zoom * width - width
+        if offset[1] > constants.zoom * height - height:
+            offset[1] = constants.zoom * height - height
 
     def _make_zoom_offset(self, pos):
-        zoom_pos = (pos[0] * ZOOM, pos[1] * ZOOM)
-        offset = [zoom_pos[0] - constants.SCREEN[0] / 2,
-                zoom_pos[1] - constants.SCREEN[1] / 2]
+        zoom_pos = (pos[0] * constants.zoom, pos[1] * constants.zoom)
+        offset = [zoom_pos[0] - constants.screen[0] / 2,
+                zoom_pos[1] - constants.screen[1] / 2]
         self._check_limits(offset)
         self.zoom_offset = tuple(offset)
 
     def _move_zoom(self, x, y):
         offset = list(self.zoom_offset)
-        offset[0] += ZOOM_STEP * x
-        offset[1] += ZOOM_STEP * y
+        offset[0] += constants.zoom_step * x
+        offset[1] += constants.zoom_step * y
         self._check_limits(offset)
         self.zoom_offset = tuple(offset)
 
@@ -536,7 +541,8 @@
 def make_button(text, action, ypos):
     button = Button(text, action=action, font=get_font(15, 'VeraBd.ttf'))
     button.align = 'l'
-    button.rect = pygame.rect.Rect(0, 0, MENU_WIDTH, MENU_BUTTON_HEIGHT)
+    button.rect = pygame.rect.Rect(0, 0, constants.menu_width,
+                                   constants.menu_button_height)
     button.rect.move_ip(805, ypos)
     return button
 
@@ -608,7 +614,7 @@
         y += toggle_zoom.get_rect().h
         quit_but = make_button("Quit", self.quit, 570)
         self.add(quit_but)
-        self.set_timer(constants.FRAME_RATE)
+        self.set_timer(constants.frame_rate)
 
     def key_down(self, event):
         # Dispatch to image widget
@@ -635,8 +641,9 @@
 if __name__ == "__main__":
     pygame.display.init()
     pygame.font.init()
-    display = pygame.display.set_mode((constants.SCREEN[0] + MENU_WIDTH,
-                                      constants.SCREEN[1]))
+    display = pygame.display.set_mode((constants.screen[0]
+                                       + constants.menu_width,
+                                      constants.screen[1]))
     state = state.initial_state()
     if len(sys.argv) < 2:
         print 'Please provide a scene name or scene and detail names'