Mercurial > mamba
annotate mamba/data.py @ 273:dcd0642e12e0
Ever helpful data helpers
author | Neil Muller <drnlmuller@gmail.com> |
---|---|
date | Thu, 15 Sep 2011 17:35:27 +0200 |
parents | 49c125e8bc2a |
children | 00c36ff53de9 |
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 = {} |
65
ca2206efeeb1
Add mutators concept to image loader.
Simon Cross <hodgestar@gmail.com>
parents:
29
diff
changeset
|
36 MUTATED_IMAGES = {} |
8
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 |
65
ca2206efeeb1
Add mutators concept to image loader.
Simon Cross <hodgestar@gmail.com>
parents:
29
diff
changeset
|
39 def load_image(filename, mutators=()): |
256
9827ce469834
Add more validation exceptions to new map loading code. Handle trying to load when display hasn't been initiliased better
Neil Muller <drnlmuller@gmail.com>
parents:
226
diff
changeset
|
40 if not pygame.display.get_init(): |
9827ce469834
Add more validation exceptions to new map loading code. Handle trying to load when display hasn't been initiliased better
Neil Muller <drnlmuller@gmail.com>
parents:
226
diff
changeset
|
41 # Just check image exists |
9827ce469834
Add more validation exceptions to new map loading code. Handle trying to load when display hasn't been initiliased better
Neil Muller <drnlmuller@gmail.com>
parents:
226
diff
changeset
|
42 testfile = open(filepath(filename), 'rb') |
9827ce469834
Add more validation exceptions to new map loading code. Handle trying to load when display hasn't been initiliased better
Neil Muller <drnlmuller@gmail.com>
parents:
226
diff
changeset
|
43 testfile.close() |
9827ce469834
Add more validation exceptions to new map loading code. Handle trying to load when display hasn't been initiliased better
Neil Muller <drnlmuller@gmail.com>
parents:
226
diff
changeset
|
44 return None |
65
ca2206efeeb1
Add mutators concept to image loader.
Simon Cross <hodgestar@gmail.com>
parents:
29
diff
changeset
|
45 key = (filename, mutators) |
ca2206efeeb1
Add mutators concept to image loader.
Simon Cross <hodgestar@gmail.com>
parents:
29
diff
changeset
|
46 if key in MUTATED_IMAGES: |
ca2206efeeb1
Add mutators concept to image loader.
Simon Cross <hodgestar@gmail.com>
parents:
29
diff
changeset
|
47 return MUTATED_IMAGES[key] |
ca2206efeeb1
Add mutators concept to image loader.
Simon Cross <hodgestar@gmail.com>
parents:
29
diff
changeset
|
48 if filename in IMAGES: |
ca2206efeeb1
Add mutators concept to image loader.
Simon Cross <hodgestar@gmail.com>
parents:
29
diff
changeset
|
49 image = IMAGES[filename] |
ca2206efeeb1
Add mutators concept to image loader.
Simon Cross <hodgestar@gmail.com>
parents:
29
diff
changeset
|
50 else: |
ca2206efeeb1
Add mutators concept to image loader.
Simon Cross <hodgestar@gmail.com>
parents:
29
diff
changeset
|
51 image = pygame.image.load(filepath(filename)) |
ca2206efeeb1
Add mutators concept to image loader.
Simon Cross <hodgestar@gmail.com>
parents:
29
diff
changeset
|
52 image = image.convert_alpha(pygame.display.get_surface()) |
ca2206efeeb1
Add mutators concept to image loader.
Simon Cross <hodgestar@gmail.com>
parents:
29
diff
changeset
|
53 IMAGES[filename] = image |
ca2206efeeb1
Add mutators concept to image loader.
Simon Cross <hodgestar@gmail.com>
parents:
29
diff
changeset
|
54 for mutator in mutators: |
ca2206efeeb1
Add mutators concept to image loader.
Simon Cross <hodgestar@gmail.com>
parents:
29
diff
changeset
|
55 image = mutator(image) |
ca2206efeeb1
Add mutators concept to image loader.
Simon Cross <hodgestar@gmail.com>
parents:
29
diff
changeset
|
56 MUTATED_IMAGES[key] = image |
ca2206efeeb1
Add mutators concept to image loader.
Simon Cross <hodgestar@gmail.com>
parents:
29
diff
changeset
|
57 return image |
212
e768056b0588
Refactor tile image loading stuff. More magic is good, right?
Jeremy Thurgood <firxen@gmail.com>
parents:
126
diff
changeset
|
58 |
e768056b0588
Refactor tile image loading stuff. More magic is good, right?
Jeremy Thurgood <firxen@gmail.com>
parents:
126
diff
changeset
|
59 |
e768056b0588
Refactor tile image loading stuff. More magic is good, right?
Jeremy Thurgood <firxen@gmail.com>
parents:
126
diff
changeset
|
60 def load_tile_image(image_name, tileset='common', mutators=()): |
e768056b0588
Refactor tile image loading stuff. More magic is good, right?
Jeremy Thurgood <firxen@gmail.com>
parents:
126
diff
changeset
|
61 filename = "tiles/%s/%s.png" % (tileset, image_name) |
e768056b0588
Refactor tile image loading stuff. More magic is good, right?
Jeremy Thurgood <firxen@gmail.com>
parents:
126
diff
changeset
|
62 return load_image(filename, mutators) |
226
cd5377697c9e
Add some helpers to data.py
Neil Muller <drnlmuller@gmail.com>
parents:
212
diff
changeset
|
63 |
cd5377697c9e
Add some helpers to data.py
Neil Muller <drnlmuller@gmail.com>
parents:
212
diff
changeset
|
64 |
cd5377697c9e
Add some helpers to data.py
Neil Muller <drnlmuller@gmail.com>
parents:
212
diff
changeset
|
65 def check_tileset_legal(tileset): |
cd5377697c9e
Add some helpers to data.py
Neil Muller <drnlmuller@gmail.com>
parents:
212
diff
changeset
|
66 return os.path.isdir(os.path.join(data_dir, 'tiles', tileset)) |
cd5377697c9e
Add some helpers to data.py
Neil Muller <drnlmuller@gmail.com>
parents:
212
diff
changeset
|
67 |
cd5377697c9e
Add some helpers to data.py
Neil Muller <drnlmuller@gmail.com>
parents:
212
diff
changeset
|
68 |
cd5377697c9e
Add some helpers to data.py
Neil Muller <drnlmuller@gmail.com>
parents:
212
diff
changeset
|
69 def check_level_exists(level): |
cd5377697c9e
Add some helpers to data.py
Neil Muller <drnlmuller@gmail.com>
parents:
212
diff
changeset
|
70 return os.path.isfile(os.path.join(data_dir, 'levels', '%s.txt' % level)) |
271 | 71 |
72 | |
73 def list_levels(): | |
74 files = [os.path.splitext(x) for x in os.listdir(filepath('levels'))] | |
75 return [x[0] for x in files if x[1] == '.txt'] | |
273
dcd0642e12e0
Ever helpful data helpers
Neil Muller <drnlmuller@gmail.com>
parents:
271
diff
changeset
|
76 |
dcd0642e12e0
Ever helpful data helpers
Neil Muller <drnlmuller@gmail.com>
parents:
271
diff
changeset
|
77 |
dcd0642e12e0
Ever helpful data helpers
Neil Muller <drnlmuller@gmail.com>
parents:
271
diff
changeset
|
78 def list_tilesets(): |
dcd0642e12e0
Ever helpful data helpers
Neil Muller <drnlmuller@gmail.com>
parents:
271
diff
changeset
|
79 tiles = os.listdir(filepath('tiles')) |
dcd0642e12e0
Ever helpful data helpers
Neil Muller <drnlmuller@gmail.com>
parents:
271
diff
changeset
|
80 return [x for x in tiles if check_tileset_legal(x)] |