Mercurial > rinkhals
annotate gamelib/main.py @ 6:c0abad23a055
Add start, quit and toggle fullscreen buttons to menu.
author | Simon Cross <hodgestar@gmail.com> |
---|---|
date | Sun, 30 Aug 2009 13:25:04 +0000 |
parents | 67b79658b047 |
children | 99c4f2226314 |
rev | line source |
---|---|
2 | 1 '''Game main module. |
2 | |
3 Contains the entry point used by the run_game.py script. | |
4 | |
5 Feel free to put all your game code here, or in other modules in this "gamelib" | |
6 package. | |
7 ''' | |
8 | |
4
e8ddbc95cbf3
Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents:
2
diff
changeset
|
9 import pygame |
e8ddbc95cbf3
Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents:
2
diff
changeset
|
10 from pgu import gui |
6
c0abad23a055
Add start, quit and toggle fullscreen buttons to menu.
Simon Cross <hodgestar@gmail.com>
parents:
5
diff
changeset
|
11 from pygame.locals import SWSURFACE, QUIT, KEYDOWN, K_ESCAPE, USEREVENT |
4
e8ddbc95cbf3
Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents:
2
diff
changeset
|
12 |
e8ddbc95cbf3
Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents:
2
diff
changeset
|
13 from mainmenu import MainMenu |
5
67b79658b047
Refactor and simplify menu.
Simon Cross <hodgestar@gmail.com>
parents:
4
diff
changeset
|
14 import constants |
4
e8ddbc95cbf3
Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents:
2
diff
changeset
|
15 |
5
67b79658b047
Refactor and simplify menu.
Simon Cross <hodgestar@gmail.com>
parents:
4
diff
changeset
|
16 def gameloop(screen, app): |
67b79658b047
Refactor and simplify menu.
Simon Cross <hodgestar@gmail.com>
parents:
4
diff
changeset
|
17 """Main game loop.""" |
4
e8ddbc95cbf3
Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents:
2
diff
changeset
|
18 clock = pygame.time.Clock() |
e8ddbc95cbf3
Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents:
2
diff
changeset
|
19 done = False |
e8ddbc95cbf3
Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents:
2
diff
changeset
|
20 while not done: |
e8ddbc95cbf3
Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents:
2
diff
changeset
|
21 for e in pygame.event.get(): |
5
67b79658b047
Refactor and simplify menu.
Simon Cross <hodgestar@gmail.com>
parents:
4
diff
changeset
|
22 if e.type is QUIT: |
4
e8ddbc95cbf3
Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents:
2
diff
changeset
|
23 done = True |
5
67b79658b047
Refactor and simplify menu.
Simon Cross <hodgestar@gmail.com>
parents:
4
diff
changeset
|
24 elif e.type is KEYDOWN and e.key == K_ESCAPE: |
4
e8ddbc95cbf3
Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents:
2
diff
changeset
|
25 done = True |
6
c0abad23a055
Add start, quit and toggle fullscreen buttons to menu.
Simon Cross <hodgestar@gmail.com>
parents:
5
diff
changeset
|
26 elif e.type is USEREVENT: |
c0abad23a055
Add start, quit and toggle fullscreen buttons to menu.
Simon Cross <hodgestar@gmail.com>
parents:
5
diff
changeset
|
27 print e.event |
4
e8ddbc95cbf3
Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents:
2
diff
changeset
|
28 else: |
e8ddbc95cbf3
Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents:
2
diff
changeset
|
29 app.event(e) |
e8ddbc95cbf3
Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents:
2
diff
changeset
|
30 |
e8ddbc95cbf3
Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents:
2
diff
changeset
|
31 # Clear the screen and render the stars |
e8ddbc95cbf3
Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents:
2
diff
changeset
|
32 dt = clock.tick()/1000.0 |
e8ddbc95cbf3
Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents:
2
diff
changeset
|
33 screen.fill((0,0,0)) |
e8ddbc95cbf3
Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents:
2
diff
changeset
|
34 app.paint(screen) |
e8ddbc95cbf3
Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents:
2
diff
changeset
|
35 pygame.display.flip() |
e8ddbc95cbf3
Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents:
2
diff
changeset
|
36 pygame.time.wait(10) |
e8ddbc95cbf3
Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents:
2
diff
changeset
|
37 |
2 | 38 |
39 def main(): | |
5
67b79658b047
Refactor and simplify menu.
Simon Cross <hodgestar@gmail.com>
parents:
4
diff
changeset
|
40 """Main script.""" |
67b79658b047
Refactor and simplify menu.
Simon Cross <hodgestar@gmail.com>
parents:
4
diff
changeset
|
41 screen = pygame.display.set_mode(constants.SCREEN, SWSURFACE) |
67b79658b047
Refactor and simplify menu.
Simon Cross <hodgestar@gmail.com>
parents:
4
diff
changeset
|
42 |
67b79658b047
Refactor and simplify menu.
Simon Cross <hodgestar@gmail.com>
parents:
4
diff
changeset
|
43 form = gui.Form() |
67b79658b047
Refactor and simplify menu.
Simon Cross <hodgestar@gmail.com>
parents:
4
diff
changeset
|
44 app = gui.App() |
67b79658b047
Refactor and simplify menu.
Simon Cross <hodgestar@gmail.com>
parents:
4
diff
changeset
|
45 main_menu = MainMenu() |
67b79658b047
Refactor and simplify menu.
Simon Cross <hodgestar@gmail.com>
parents:
4
diff
changeset
|
46 |
67b79658b047
Refactor and simplify menu.
Simon Cross <hodgestar@gmail.com>
parents:
4
diff
changeset
|
47 c = gui.Container(align=-1, valign=-1) |
67b79658b047
Refactor and simplify menu.
Simon Cross <hodgestar@gmail.com>
parents:
4
diff
changeset
|
48 c.add(main_menu, 0, 0) |
67b79658b047
Refactor and simplify menu.
Simon Cross <hodgestar@gmail.com>
parents:
4
diff
changeset
|
49 |
67b79658b047
Refactor and simplify menu.
Simon Cross <hodgestar@gmail.com>
parents:
4
diff
changeset
|
50 app.init(c) |
67b79658b047
Refactor and simplify menu.
Simon Cross <hodgestar@gmail.com>
parents:
4
diff
changeset
|
51 |
67b79658b047
Refactor and simplify menu.
Simon Cross <hodgestar@gmail.com>
parents:
4
diff
changeset
|
52 gameloop(screen, app) |