Mercurial > pyntnclick
changeset 28:0f25f7b9b37a
Add loading of initial state.
author | Simon Cross <hodgestar+bzr@gmail.com> |
---|---|
date | Sun, 22 Aug 2010 18:48:32 +0200 |
parents | 5c7bbbdf9296 |
children | 6322d92dc8f0 |
files | gamelib/gamescreen.py gamelib/main.py gamelib/scenes/cryo.py gamelib/state.py |
diffstat | 4 files changed, 48 insertions(+), 4 deletions(-) [+] |
line wrap: on
line diff
--- a/gamelib/gamescreen.py Sun Aug 22 18:44:19 2010 +0200 +++ b/gamelib/gamescreen.py Sun Aug 22 18:48:32 2010 +0200 @@ -2,6 +2,8 @@ # Copyright Boomslang team, 2010 (see COPYING File) # Main menu for the game +from state import initial_state + from pygame.color import Color from albow.screen import Screen from albow.controls import Button, Label @@ -57,6 +59,8 @@ self.inventory = InventoryView() self.inventory.bottomleft = self.bottomleft self.add(self.inventory) + # TODO: Randomly plonk the state here for now + self.state = initial_state() def main_menu(self): print 'Returning to menu'
--- a/gamelib/main.py Sun Aug 22 18:44:19 2010 +0200 +++ b/gamelib/main.py Sun Aug 22 18:48:32 2010 +0200 @@ -24,7 +24,7 @@ def main(): pygame.display.init() pygame.font.init() - display = pygame.display.set_mode(SCREEN) + display = pygame.display.set_mode(SCREEN, SRCALPHA) shell = MainShell(display) shell.run()
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gamelib/scenes/cryo.py Sun Aug 22 18:48:32 2010 +0200 @@ -0,0 +1,12 @@ +from gamelib.state import Scene + +class Cryo(Scene): + + FOLDER = "cryo" + BACKGROUND = "cryo_room.png" + + def __init__(self, state): + super(Cryo, self).__init__(state) + + +SCENES = [Cryo]
--- a/gamelib/state.py Sun Aug 22 18:44:19 2010 +0200 +++ b/gamelib/state.py Sun Aug 22 18:48:32 2010 +0200 @@ -3,10 +3,12 @@ from albow.resource import get_image, get_sound from pygame.locals import BLEND_ADD + def initial_state(): """Load the initial state.""" state = State() - # TODO: populate state + state.load_scenes("cryo") + state.set_current_scene("cryo") return state @@ -26,18 +28,44 @@ self.items = {} # map of item name -> Item object in inventory self.inventory = {} + # current scene + self.current_scene = None + + def add_scene(self, scene): + self.scenes[scene.name] = scene + + def load_scenes(self, modname): + mod = __import__("gamelib.scenes.%s" % (modname,), fromlist=[modname]) + for scene_cls in mod.SCENES: + self.add_scene(scene_cls(self)) + + def set_current_scene(self, name): + self.current_scence = self.scenes[name] + + def draw(self, surface): + self.current_scene.draw(surface) class Scene(object): """Base class for scenes.""" + # sub-folder to look for resources in FOLDER = None + + # name of background image resource BACKGROUND = None - def __init__(self): + # name of scene (optional, defaults to folder) + NAME = None + + def __init__(self, state): + # scene name + self.name = self.NAME if self.NAME is not None else self.FOLDER + # link back to state object + self.state = state # map of thing names -> Thing objects self.things = {} - self._background = get_image([self.FOLDER, self.BACKGROUND]) + self._background = get_image(self.FOLDER, self.BACKGROUND) def draw_background(self, surface): surface.blit(self._background, (0, 0), None, BLEND_ADD)