Mercurial > pyntnclick
comparison gamelib/state.py @ 28:0f25f7b9b37a
Add loading of initial state.
author | Simon Cross <hodgestar+bzr@gmail.com> |
---|---|
date | Sun, 22 Aug 2010 18:48:32 +0200 |
parents | 1e90a3e4618e |
children | 6322d92dc8f0 |
comparison
equal
deleted
inserted
replaced
27:5c7bbbdf9296 | 28:0f25f7b9b37a |
---|---|
1 """Utilities and base classes for dealing with scenes.""" | 1 """Utilities and base classes for dealing with scenes.""" |
2 | 2 |
3 from albow.resource import get_image, get_sound | 3 from albow.resource import get_image, get_sound |
4 from pygame.locals import BLEND_ADD | 4 from pygame.locals import BLEND_ADD |
5 | 5 |
6 | |
6 def initial_state(): | 7 def initial_state(): |
7 """Load the initial state.""" | 8 """Load the initial state.""" |
8 state = State() | 9 state = State() |
9 # TODO: populate state | 10 state.load_scenes("cryo") |
11 state.set_current_scene("cryo") | |
10 return state | 12 return state |
11 | 13 |
12 | 14 |
13 class State(object): | 15 class State(object): |
14 """Complete game state. | 16 """Complete game state. |
24 self.scenes = {} | 26 self.scenes = {} |
25 # map of item name -> Item object | 27 # map of item name -> Item object |
26 self.items = {} | 28 self.items = {} |
27 # map of item name -> Item object in inventory | 29 # map of item name -> Item object in inventory |
28 self.inventory = {} | 30 self.inventory = {} |
31 # current scene | |
32 self.current_scene = None | |
33 | |
34 def add_scene(self, scene): | |
35 self.scenes[scene.name] = scene | |
36 | |
37 def load_scenes(self, modname): | |
38 mod = __import__("gamelib.scenes.%s" % (modname,), fromlist=[modname]) | |
39 for scene_cls in mod.SCENES: | |
40 self.add_scene(scene_cls(self)) | |
41 | |
42 def set_current_scene(self, name): | |
43 self.current_scence = self.scenes[name] | |
44 | |
45 def draw(self, surface): | |
46 self.current_scene.draw(surface) | |
29 | 47 |
30 | 48 |
31 class Scene(object): | 49 class Scene(object): |
32 """Base class for scenes.""" | 50 """Base class for scenes.""" |
33 | 51 |
52 # sub-folder to look for resources in | |
34 FOLDER = None | 53 FOLDER = None |
54 | |
55 # name of background image resource | |
35 BACKGROUND = None | 56 BACKGROUND = None |
36 | 57 |
37 def __init__(self): | 58 # name of scene (optional, defaults to folder) |
59 NAME = None | |
60 | |
61 def __init__(self, state): | |
62 # scene name | |
63 self.name = self.NAME if self.NAME is not None else self.FOLDER | |
64 # link back to state object | |
65 self.state = state | |
38 # map of thing names -> Thing objects | 66 # map of thing names -> Thing objects |
39 self.things = {} | 67 self.things = {} |
40 self._background = get_image([self.FOLDER, self.BACKGROUND]) | 68 self._background = get_image(self.FOLDER, self.BACKGROUND) |
41 | 69 |
42 def draw_background(self, surface): | 70 def draw_background(self, surface): |
43 surface.blit(self._background, (0, 0), None, BLEND_ADD) | 71 surface.blit(self._background, (0, 0), None, BLEND_ADD) |
44 | 72 |
45 def draw_things(self, surface): | 73 def draw_things(self, surface): |