Mercurial > mamba
annotate mamba/data.py @ 57:cd5ad2c45e2d
Minor level fiddlery.
author | Jeremy Thurgood <firxen@gmail.com> |
---|---|
date | Sun, 11 Sep 2011 16:19:05 +0200 |
parents | b48c47af7801 |
children | ca2206efeeb1 |
rev | line source |
---|---|
0 | 1 '''Simple data loader module. |
2 | |
3 Loads data files from the "data" directory shipped with a game. | |
4 | |
5 Enhancing this to handle caching etc. is left as an exercise for the reader. | |
6 | |
7 Note that pyglet users should probably just add the data directory to the | |
8 pyglet.resource search path. | |
9 ''' | |
10 | |
11 import os | |
12 | |
8
66ae99f6903e
First stab at tile/level stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
0
diff
changeset
|
13 import pygame |
66ae99f6903e
First stab at tile/level stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
0
diff
changeset
|
14 |
66ae99f6903e
First stab at tile/level stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
0
diff
changeset
|
15 |
0 | 16 data_py = os.path.abspath(os.path.dirname(__file__)) |
17 data_dir = os.path.normpath(os.path.join(data_py, '..', 'data')) | |
18 | |
19 | |
20 def filepath(filename): | |
21 '''Determine the path to a file in the data directory. | |
22 ''' | |
29
b48c47af7801
Fix filepath bug in data.py (same as last pyweek)
Neil Muller <drnlmuller@gmail.com>
parents:
8
diff
changeset
|
23 filename = os.path.join(*filename.split('/')) |
0 | 24 return os.path.join(data_dir, filename) |
25 | |
26 | |
8
66ae99f6903e
First stab at tile/level stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
0
diff
changeset
|
27 def load_file(filename, mode='rb'): |
0 | 28 '''Open a file in the data directory. |
29 | |
30 "mode" is passed as the second arg to open(). | |
31 ''' | |
32 return open(os.path.join(data_dir, filename), mode) | |
8
66ae99f6903e
First stab at tile/level stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
0
diff
changeset
|
33 |
66ae99f6903e
First stab at tile/level stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
0
diff
changeset
|
34 |
66ae99f6903e
First stab at tile/level stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
0
diff
changeset
|
35 IMAGES = {} |
66ae99f6903e
First stab at tile/level stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
0
diff
changeset
|
36 |
66ae99f6903e
First stab at tile/level stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
0
diff
changeset
|
37 |
66ae99f6903e
First stab at tile/level stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
0
diff
changeset
|
38 def load_image(filename): |
66ae99f6903e
First stab at tile/level stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
0
diff
changeset
|
39 if filename not in IMAGES: |
66ae99f6903e
First stab at tile/level stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
0
diff
changeset
|
40 IMAGES[filename] = pygame.image.load(filepath(filename)) |
66ae99f6903e
First stab at tile/level stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
0
diff
changeset
|
41 if pygame.display.get_init(): |
66ae99f6903e
First stab at tile/level stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
0
diff
changeset
|
42 IMAGES[filename] = IMAGES[filename].convert_alpha( |
66ae99f6903e
First stab at tile/level stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
0
diff
changeset
|
43 pygame.display.get_surface()) |
66ae99f6903e
First stab at tile/level stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
0
diff
changeset
|
44 return IMAGES[filename] |