changeset 11:5d58a5b13731

Extremely rudimentary game engine.
author Simon Cross <hodgestar@gmail.com>
date Sun, 30 Aug 2009 15:11:44 +0000
parents 0ecf1e3ab087
children 8a7319e4853a
files gamelib/engine.py gamelib/gameboard.py gamelib/main.py gamelib/mainmenu.py
diffstat 4 files changed, 67 insertions(+), 34 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gamelib/engine.py	Sun Aug 30 15:11:44 2009 +0000
@@ -0,0 +1,53 @@
+"""Game engine and states."""
+
+from pgu.engine import Game, State, Quit
+import pygame
+from pygame.locals import USEREVENT, QUIT, KEYDOWN, K_ESCAPE
+
+class Engine(Game):
+    def __init__(self, main_menu_app):
+        self.main_menu_app = main_menu_app
+        self.clock = pygame.time.Clock()
+
+    def tick(self):
+        """Tic toc."""
+        pygame.time.wait(10)
+
+class MainMenuState(State):
+    def event(self, e):
+        if events_equal(e, START_DAY):
+            return DayState(self.game)
+        elif e.type is KEYDOWN and e.key == K_ESCAPE:
+            return Quit(self.game)
+        elif e.type is not QUIT:
+            self.game.main_menu_app.event(e)
+
+    def paint(self, screen):
+        screen.fill((0,0,0))
+        self.game.main_menu_app.paint(screen)
+        pygame.display.flip()
+
+class DayState(State):
+    def event(self, e):
+        if events_equal(e, START_NIGHT):
+            return NightState(self.game)
+        elif e.type is KEYDOWN and e.key == K_ESCAPE:
+            return MainMenuState(self.game)
+        elif events_equal(e, GO_MAIN_MENU):
+            return MainMenuState(self.game)
+
+class NightState(State):
+    def event(self, e):
+        if events_equal(e, START_DAY):
+            return DayState(self.game)
+
+# pygame events
+
+def events_equal(e1, e2):
+    """Compare two user events."""
+    return (e1.type is e2.type and e1.name == e2.name)
+
+START_DAY = pygame.event.Event(USEREVENT, name="START_DAY")
+START_NIGHT = pygame.event.Event(USEREVENT, name="START_NIGHT")
+GO_MAIN_MENU = pygame.event.Event(USEREVENT, name="GO_MAIN_MENU")
+QUIT = pygame.event.Event(QUIT)
--- a/gamelib/gameboard.py	Sun Aug 30 15:11:21 2009 +0000
+++ b/gamelib/gameboard.py	Sun Aug 30 15:11:44 2009 +0000
@@ -1,9 +1,7 @@
 import random
 
-import pygame
 from pgu import tilevid
 
-import constants
 import data
 
 
--- a/gamelib/main.py	Sun Aug 30 15:11:21 2009 +0000
+++ b/gamelib/main.py	Sun Aug 30 15:11:44 2009 +0000
@@ -8,39 +8,14 @@
 
 import pygame
 from pgu import gui
-from pygame.locals import SWSURFACE, QUIT, KEYDOWN, K_ESCAPE, USEREVENT
+from pygame.locals import SWSURFACE
 
 from mainmenu import MainMenu
+from engine import Engine, MainMenuState
 import constants
 
-def gameloop(screen, app):
-    """Main game loop."""
-    clock = pygame.time.Clock()
-    done = False
-    while not done:
-        for e in pygame.event.get():
-            if e.type is QUIT:
-                done = True
-            elif e.type is KEYDOWN and e.key == K_ESCAPE:
-                done = True
-            elif e.type is USEREVENT:
-                print e.event
-            else:
-                app.event(e)
-
-        # Clear the screen and render the stars
-        dt = clock.tick()/1000.0
-        screen.fill((0,0,0))
-        app.paint(screen)
-        pygame.display.flip()
-        pygame.time.wait(10)
-
-
-def main():
-    """Main script."""
-    screen = pygame.display.set_mode(constants.SCREEN, SWSURFACE)
-
-    form = gui.Form()
+def create_menu_app():
+    """Create the menu app."""
     app = gui.App()
     main_menu = MainMenu()
 
@@ -48,5 +23,11 @@
     c.add(main_menu, 0, 0)
 
     app.init(c)
+    return app
 
-    gameloop(screen, app)
+def main():
+    """Main script."""
+    screen = pygame.display.set_mode(constants.SCREEN, SWSURFACE)
+    main_menu_app = create_menu_app()
+    engine = Engine(main_menu_app)
+    engine.run(MainMenuState(engine), screen)
--- a/gamelib/mainmenu.py	Sun Aug 30 15:11:21 2009 +0000
+++ b/gamelib/mainmenu.py	Sun Aug 30 15:11:44 2009 +0000
@@ -3,6 +3,7 @@
 from pgu import gui
 import pygame
 import constants
+import engine
 
 class MainMenu(gui.Table):
     def __init__(self, **params):
@@ -12,10 +13,10 @@
             pygame.display.toggle_fullscreen()
 
         def quit_pressed():
-            pygame.event.post(pygame.event.Event(pygame.QUIT))
+            pygame.event.post(engine.QUIT)
 
         def start_pressed():
-            pygame.event.post(pygame.event.Event(pygame.USEREVENT, event="<Our Start Event Class>"))
+            pygame.event.post(engine.START_DAY)
 
         start_button = gui.Button("Start")
         start_button.connect(gui.CLICK, start_pressed)