Mercurial > sypikslang
comparison gamelib/main.py @ 52:1d3d20bdc8b9
Move engine stuff into it's own file
author | Neil Muller <drnlmuller@gmail.com> |
---|---|
date | Mon, 07 May 2012 21:55:55 +0200 |
parents | ac637e84f8f8 |
children | 655a6912e0ae |
comparison
equal
deleted
inserted
replaced
51:ac637e84f8f8 | 52:1d3d20bdc8b9 |
---|---|
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, | |
10 USEREVENT) | |
11 from pygame.time import Clock | |
12 | 9 |
13 from gamelib.gui_base import Window | 10 from gamelib.gui_base import Window |
14 from gamelib.gui import BigButton | 11 from gamelib.gui import BigButton |
15 from gamelib.constants import WIDTH, HEIGHT, SCREEN, FPS | 12 from gamelib.engine import Engine, AddWindow, PopWindow |
13 | |
14 from gamelib.constants import WIDTH, HEIGHT, SCREEN | |
16 | 15 |
17 | 16 |
18 pygame.init() | 17 pygame.init() |
19 | |
20 | |
21 class UserEvent(object): | |
22 # Handy event wrapper | |
23 | |
24 TYPE = "unknown" | |
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() | |
54 | 18 |
55 | 19 |
56 class ExitGameButton(BigButton): | 20 class ExitGameButton(BigButton): |
57 | 21 |
58 def __init__(self): | 22 def __init__(self): |
107 def main(): | 71 def main(): |
108 screen = pygame.display.set_mode(SCREEN) | 72 screen = pygame.display.set_mode(SCREEN) |
109 engine = Engine(screen) | 73 engine = Engine(screen) |
110 window = MainMenu(screen) | 74 window = MainMenu(screen) |
111 engine.run(window) | 75 engine.run(window) |
112 | |
113 | |
114 class Engine(object): | |
115 | |
116 def __init__(self, screen): | |
117 self._window_stack = [] | |
118 self._running = False | |
119 self._mouse_down = False | |
120 self._screen = screen | |
121 | |
122 def run(self, window): | |
123 clock = Clock() | |
124 self._window_stack.append(window) | |
125 self._running = True | |
126 self._mouse_down = False | |
127 while self._running: | |
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() |