Mercurial > rinkhals
annotate gamelib/loadlevel.py @ 412:1e24eedbf40f
Implement non-gui parts of level loading communication
author | Neil Muller <drnlmuller@gmail.com> |
---|---|
date | Sat, 21 Nov 2009 11:34:12 +0000 |
parents | |
children | c6dd21b75bf5 |
rev | line source |
---|---|
412
1e24eedbf40f
Implement non-gui parts of level loading communication
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
1 """Help screen.""" |
1e24eedbf40f
Implement non-gui parts of level loading communication
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
2 |
1e24eedbf40f
Implement non-gui parts of level loading communication
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
3 from pgu import gui |
1e24eedbf40f
Implement non-gui parts of level loading communication
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
4 import os |
1e24eedbf40f
Implement non-gui parts of level loading communication
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
5 import pygame |
1e24eedbf40f
Implement non-gui parts of level loading communication
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
6 import constants |
1e24eedbf40f
Implement non-gui parts of level loading communication
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
7 import level |
1e24eedbf40f
Implement non-gui parts of level loading communication
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
8 import engine |
1e24eedbf40f
Implement non-gui parts of level loading communication
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
9 import data |
1e24eedbf40f
Implement non-gui parts of level loading communication
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
10 import imagecache |
1e24eedbf40f
Implement non-gui parts of level loading communication
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
11 |
1e24eedbf40f
Implement non-gui parts of level loading communication
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
12 def make_load_screen(level): |
1e24eedbf40f
Implement non-gui parts of level loading communication
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
13 """Create a screen for selecting the levels""" |
1e24eedbf40f
Implement non-gui parts of level loading communication
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
14 load_screen = LoadScreen(level, width=600) |
1e24eedbf40f
Implement non-gui parts of level loading communication
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
15 |
1e24eedbf40f
Implement non-gui parts of level loading communication
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
16 c = LoadContainer(align=0, valign=0) |
1e24eedbf40f
Implement non-gui parts of level loading communication
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
17 c.add(load_screen, 0, 0) |
1e24eedbf40f
Implement non-gui parts of level loading communication
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
18 |
1e24eedbf40f
Implement non-gui parts of level loading communication
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
19 return c, load_screen |
1e24eedbf40f
Implement non-gui parts of level loading communication
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
20 |
1e24eedbf40f
Implement non-gui parts of level loading communication
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
21 class LoadContainer(gui.Container): |
1e24eedbf40f
Implement non-gui parts of level loading communication
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
22 def paint(self, s): |
1e24eedbf40f
Implement non-gui parts of level loading communication
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
23 pygame.display.set_caption('Load Level') |
1e24eedbf40f
Implement non-gui parts of level loading communication
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
24 splash = imagecache.load_image("images/splash.png", ["lighten_most"]) |
1e24eedbf40f
Implement non-gui parts of level loading communication
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
25 pygame.display.get_surface().blit(splash, (0, 0)) |
1e24eedbf40f
Implement non-gui parts of level loading communication
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
26 gui.Container.paint(self, s) |
1e24eedbf40f
Implement non-gui parts of level loading communication
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
27 |
1e24eedbf40f
Implement non-gui parts of level loading communication
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
28 class LoadScreen(gui.Document): |
1e24eedbf40f
Implement non-gui parts of level loading communication
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
29 def __init__(self, start_level, **params): |
1e24eedbf40f
Implement non-gui parts of level loading communication
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
30 self.levels = {} |
1e24eedbf40f
Implement non-gui parts of level loading communication
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
31 self.cur_level = start_level |
1e24eedbf40f
Implement non-gui parts of level loading communication
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
32 for name in os.listdir(data.filepath('levels/')): |
1e24eedbf40f
Implement non-gui parts of level loading communication
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
33 if name.endswith('.conf'): |
1e24eedbf40f
Implement non-gui parts of level loading communication
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
34 try: |
1e24eedbf40f
Implement non-gui parts of level loading communication
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
35 this_level = level.Level(name) |
1e24eedbf40f
Implement non-gui parts of level loading communication
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
36 except RuntimeError: |
1e24eedbf40f
Implement non-gui parts of level loading communication
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
37 continue # Skip levels that fail to load |
1e24eedbf40f
Implement non-gui parts of level loading communication
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
38 if os.path.exists(this_level.map): |
1e24eedbf40f
Implement non-gui parts of level loading communication
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
39 # Skip level if we can't see the map |
1e24eedbf40f
Implement non-gui parts of level loading communication
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
40 self.levels[this_level.level_name] = this_level |
1e24eedbf40f
Implement non-gui parts of level loading communication
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
41 if not start_level.level_name in self.levels: |
1e24eedbf40f
Implement non-gui parts of level loading communication
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
42 print 'Start level not found' |
1e24eedbf40f
Implement non-gui parts of level loading communication
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
43 |
1e24eedbf40f
Implement non-gui parts of level loading communication
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
44 self.cur_level = self.levels.values()[0] |
1e24eedbf40f
Implement non-gui parts of level loading communication
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
45 |
1e24eedbf40f
Implement non-gui parts of level loading communication
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
46 gui.Document.__init__(self, **params) |
1e24eedbf40f
Implement non-gui parts of level loading communication
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
47 |
1e24eedbf40f
Implement non-gui parts of level loading communication
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
48 def done_pressed(): |
1e24eedbf40f
Implement non-gui parts of level loading communication
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
49 pygame.event.post(engine.DO_LOAD_LEVEL) |
1e24eedbf40f
Implement non-gui parts of level loading communication
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
50 |
1e24eedbf40f
Implement non-gui parts of level loading communication
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
51 def cancel_pressed(): |
1e24eedbf40f
Implement non-gui parts of level loading communication
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
52 pygame.event.post(engine.GO_MAIN_MENU) |
1e24eedbf40f
Implement non-gui parts of level loading communication
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
53 |
1e24eedbf40f
Implement non-gui parts of level loading communication
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
54 done_button = gui.Button("Load This Level") |
1e24eedbf40f
Implement non-gui parts of level loading communication
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
55 done_button.connect(gui.CLICK, done_pressed) |
1e24eedbf40f
Implement non-gui parts of level loading communication
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
56 |
1e24eedbf40f
Implement non-gui parts of level loading communication
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
57 cancel_button = gui.Button("Cancel & return to main menu") |
1e24eedbf40f
Implement non-gui parts of level loading communication
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
58 cancel_button.connect(gui.CLICK, cancel_pressed) |
1e24eedbf40f
Implement non-gui parts of level loading communication
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
59 |
1e24eedbf40f
Implement non-gui parts of level loading communication
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
60 self.add(done_button, align=0) |
1e24eedbf40f
Implement non-gui parts of level loading communication
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
61 self.add(cancel_button, align=0) |
1e24eedbf40f
Implement non-gui parts of level loading communication
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
62 |
1e24eedbf40f
Implement non-gui parts of level loading communication
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
63 def get_level(self): |
1e24eedbf40f
Implement non-gui parts of level loading communication
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
64 return self.cur_level_name |
1e24eedbf40f
Implement non-gui parts of level loading communication
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
65 |