diff 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
line wrap: on
line diff
--- a/gamelib/main.py	Mon May 07 21:51:36 2012 +0200
+++ b/gamelib/main.py	Mon May 07 21:55:55 2012 +0200
@@ -6,53 +6,17 @@
 package.
 '''
 import pygame
-from pygame.locals import (MOUSEBUTTONDOWN, MOUSEBUTTONUP, MOUSEMOTION, QUIT,
-        USEREVENT)
-from pygame.time import Clock
 
 from gamelib.gui_base import Window
 from gamelib.gui import BigButton
-from gamelib.constants import WIDTH, HEIGHT, SCREEN, FPS
+from gamelib.engine import Engine, AddWindow, PopWindow
+
+from gamelib.constants import WIDTH, HEIGHT, SCREEN
 
 
 pygame.init()
 
 
-class UserEvent(object):
-    # Handy event wrapper
-
-    TYPE = "unknown"
-
-    @classmethod
-    def post(cls, **kw):
-        event = pygame.event.Event(USEREVENT, utype=cls.TYPE, **kw)
-        pygame.event.post(event)
-
-    @classmethod
-    def matches(cls, event):
-        return event.type == USEREVENT and event.utype == cls.TYPE
-
-
-class AddWindow(UserEvent):
-    # Add the given window to the top of the window stack
-
-    TYPE = "ADD_WINDOW"
-
-    @classmethod
-    def post(cls, window):
-        super(AddWindow, cls).post(window=window)
-
-
-class PopWindow(UserEvent):
-    # Pop a window off the top of the window stack
-
-    TYPE = "POP_WINDOW"
-
-    @classmethod
-    def post(cls):
-        super(PopWindow, cls).post()
-
-
 class ExitGameButton(BigButton):
 
     def __init__(self):
@@ -109,45 +73,3 @@
     engine = Engine(screen)
     window = MainMenu(screen)
     engine.run(window)
-
-
-class Engine(object):
-
-    def __init__(self, screen):
-        self._window_stack = []
-        self._running = False
-        self._mouse_down = False
-        self._screen = screen
-
-    def run(self, window):
-        clock = Clock()
-        self._window_stack.append(window)
-        self._running = True
-        self._mouse_down = False
-        while self._running:
-            self.process_input()
-            self.draw()
-            clock.tick(FPS)
-
-    def draw(self):
-        for view in self._window_stack:
-            view.draw(self._screen)
-        pygame.display.flip()
-
-    def process_input(self):
-        for event in pygame.event.get():
-            if self._mouse_down:
-                if event.type == MOUSEBUTTONUP:
-                    self._mouse_down = False
-                    self._window_stack[-1].on_mouse_up(event.pos)
-                elif event.type == MOUSEMOTION:
-                    self._window_stack[-1].on_mouse_move(event.pos)
-            elif not self._mouse_down and event.type == MOUSEBUTTONDOWN:
-                self._mouse_down = True
-                self._window_stack[-1].on_mouse_down(event.pos)
-            elif event.type == QUIT:
-                self._running = False
-            elif AddWindow.matches(event):
-                self._window_stack.append(event.window)
-            elif PopWindow.matches(event):
-                self._window_stack.pop()