Mercurial > sypikslang
comparison gamelib/main.py @ 51:ac637e84f8f8
Consilidate engine stuff and eventify window stack manipulation
author | Neil Muller <drnlmuller@gmail.com> |
---|---|
date | Mon, 07 May 2012 21:51:36 +0200 |
parents | a2980cc9a060 |
children | 1d3d20bdc8b9 |
comparison
equal
deleted
inserted
replaced
50:56cb4a8a6aa6 | 51:ac637e84f8f8 |
---|---|
4 | 4 |
5 Feel free to put all your game code here, or in other modules in this "gamelib" | 5 Feel free to put all your game code here, or in other modules in this "gamelib" |
6 package. | 6 package. |
7 ''' | 7 ''' |
8 import pygame | 8 import pygame |
9 from pygame.locals import MOUSEBUTTONDOWN, MOUSEBUTTONUP, MOUSEMOTION, QUIT | 9 from pygame.locals import (MOUSEBUTTONDOWN, MOUSEBUTTONUP, MOUSEMOTION, QUIT, |
10 USEREVENT) | |
10 from pygame.time import Clock | 11 from pygame.time import Clock |
11 | 12 |
12 from gamelib.gui_base import Window | 13 from gamelib.gui_base import Window |
13 from gamelib.gui import BigButton | 14 from gamelib.gui import BigButton |
14 from gamelib.constants import WIDTH, HEIGHT, SCREEN, FPS | 15 from gamelib.constants import WIDTH, HEIGHT, SCREEN, FPS |
15 | 16 |
16 | 17 |
17 pygame.init() | 18 pygame.init() |
18 | 19 |
19 GAME_IS_RUNNING = True | |
20 | 20 |
21 WINDOW_STACK = [] | 21 class UserEvent(object): |
22 # Handy event wrapper | |
22 | 23 |
23 # input variables | 24 TYPE = "unknown" |
24 MOUSE_DOWN = False | 25 |
26 @classmethod | |
27 def post(cls, **kw): | |
28 event = pygame.event.Event(USEREVENT, utype=cls.TYPE, **kw) | |
29 pygame.event.post(event) | |
30 | |
31 @classmethod | |
32 def matches(cls, event): | |
33 return event.type == USEREVENT and event.utype == cls.TYPE | |
34 | |
35 | |
36 class AddWindow(UserEvent): | |
37 # Add the given window to the top of the window stack | |
38 | |
39 TYPE = "ADD_WINDOW" | |
40 | |
41 @classmethod | |
42 def post(cls, window): | |
43 super(AddWindow, cls).post(window=window) | |
44 | |
45 | |
46 class PopWindow(UserEvent): | |
47 # Pop a window off the top of the window stack | |
48 | |
49 TYPE = "POP_WINDOW" | |
50 | |
51 @classmethod | |
52 def post(cls): | |
53 super(PopWindow, cls).post() | |
25 | 54 |
26 | 55 |
27 class ExitGameButton(BigButton): | 56 class ExitGameButton(BigButton): |
28 | 57 |
29 def __init__(self): | 58 def __init__(self): |
30 super(ExitGameButton, self).__init__(((WIDTH - 128), 10), 'Exit') | 59 super(ExitGameButton, self).__init__(((WIDTH - 128), 10), 'Exit') |
31 | 60 |
32 def on_click(self): | 61 def on_click(self): |
33 WINDOW_STACK.pop() | 62 PopWindow.post() |
34 | 63 |
35 | 64 |
36 class GameWindow(Window): | 65 class GameWindow(Window): |
37 """Main window for the game""" | 66 """Main window for the game""" |
38 | 67 |
49 'Start') | 78 'Start') |
50 self.screen = screen | 79 self.screen = screen |
51 | 80 |
52 def on_click(self): | 81 def on_click(self): |
53 game_window = GameWindow(self.screen) | 82 game_window = GameWindow(self.screen) |
54 WINDOW_STACK.append(game_window) | 83 AddWindow.post(game_window) |
55 | 84 |
56 | 85 |
57 class QuitButton(BigButton): | 86 class QuitButton(BigButton): |
58 | 87 |
59 def __init__(self): | 88 def __init__(self): |
62 | 91 |
63 def on_click(self): | 92 def on_click(self): |
64 pygame.event.post(pygame.event.Event(pygame.QUIT)) | 93 pygame.event.post(pygame.event.Event(pygame.QUIT)) |
65 | 94 |
66 | 95 |
67 def main(): | 96 class MainMenu(Window): |
68 clock = Clock() | 97 |
69 screen = pygame.display.set_mode(SCREEN) | 98 def __init__(self, screen): |
70 window = Window(screen) | 99 super(MainMenu, self).__init__(screen) |
71 window.background_colour = (0, 0, 0) | 100 self.background_colour = (0, 0, 0) |
72 button1 = StartButton(screen) | 101 button1 = StartButton(screen) |
73 window.add_child(button1) | 102 self.add_child(button1) |
74 button2 = QuitButton() | 103 button2 = QuitButton() |
75 window.add_child(button2) | 104 self.add_child(button2) |
76 WINDOW_STACK.append(window) | |
77 while GAME_IS_RUNNING: | |
78 process_input() | |
79 draw(screen) | |
80 clock.tick(FPS) | |
81 | 105 |
82 | 106 |
83 def draw(screen): | 107 def main(): |
84 for view in WINDOW_STACK: | 108 screen = pygame.display.set_mode(SCREEN) |
85 view.draw(screen) | 109 engine = Engine(screen) |
86 pygame.display.flip() | 110 window = MainMenu(screen) |
111 engine.run(window) | |
87 | 112 |
88 | 113 |
89 def process_input(): | 114 class Engine(object): |
90 global MOUSE_DOWN | 115 |
91 global GAME_IS_RUNNING | 116 def __init__(self, screen): |
92 for event in pygame.event.get(): | 117 self._window_stack = [] |
93 if MOUSE_DOWN: | 118 self._running = False |
94 if event.type == MOUSEBUTTONUP: | 119 self._mouse_down = False |
95 MOUSE_DOWN = False | 120 self._screen = screen |
96 WINDOW_STACK[-1].on_mouse_up(event.pos) | 121 |
97 elif event.type == MOUSEMOTION: | 122 def run(self, window): |
98 WINDOW_STACK[-1].on_mouse_move(event.pos) | 123 clock = Clock() |
99 elif not MOUSE_DOWN and event.type == MOUSEBUTTONDOWN: | 124 self._window_stack.append(window) |
100 MOUSE_DOWN = True | 125 self._running = True |
101 WINDOW_STACK[-1].on_mouse_down(event.pos) | 126 self._mouse_down = False |
102 elif event.type == QUIT: | 127 while self._running: |
103 GAME_IS_RUNNING = False | 128 self.process_input() |
129 self.draw() | |
130 clock.tick(FPS) | |
131 | |
132 def draw(self): | |
133 for view in self._window_stack: | |
134 view.draw(self._screen) | |
135 pygame.display.flip() | |
136 | |
137 def process_input(self): | |
138 for event in pygame.event.get(): | |
139 if self._mouse_down: | |
140 if event.type == MOUSEBUTTONUP: | |
141 self._mouse_down = False | |
142 self._window_stack[-1].on_mouse_up(event.pos) | |
143 elif event.type == MOUSEMOTION: | |
144 self._window_stack[-1].on_mouse_move(event.pos) | |
145 elif not self._mouse_down and event.type == MOUSEBUTTONDOWN: | |
146 self._mouse_down = True | |
147 self._window_stack[-1].on_mouse_down(event.pos) | |
148 elif event.type == QUIT: | |
149 self._running = False | |
150 elif AddWindow.matches(event): | |
151 self._window_stack.append(event.window) | |
152 elif PopWindow.matches(event): | |
153 self._window_stack.pop() |