fungus /

#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""Fungus - Live and survive in a world filled with mold.

The main script for starting Fungus games. 

    - python
      start the default Scene. 
    - python gamefile.Scene
      start with a custom Scene

    - python fungus_01_intro.Scene
      Start the fungus Intro

For developers: 
    - Just clone the fungus project from the Mercurial repository and add your scenes inside it. 
      Distribute them along with the fungus engine. 

Mercurial repository: 

To adjust the default scene, just import another one as Scene in

### Imports ###

# Load sleep to limit the CPU usage
from time import sleep

# Load necessary pyglet classes
from pyglet import window
from import *

# load the fungus core
from fungus_core import Core

# Load a basic scene for testing. 
from fungus_scene import DummyScene as Scene

### Classes ###

class Game(object):
    """The main game class. 

- The Game class acts as basic game layer and provides an API which the scenes can use. 
- It starts the scenes and passes them a core object. 
- It also contains the main event loop in which it calls the update function of 
the scenes. 
- Additionally it forwards events to the scene. 

    def __init__(self, name="Fungus", width=480, height=360, fullscreen=False, graphics_dir = "graphics", first_scene = Scene, *args, **kwds):
        """Initialize the game.
        @param first_scene: The Scene the game starts with. 
        @type first_scene: BaseScene
        # First get a pyglet window
        if not fullscreen: 
   = window.Window(width=width, height=height, fullscreen=fullscreen, caption=name)
   = window.Window(fullscreen=fullscreen, resizable=False, caption=name)
        # And activate tranparency for pngs
        # Then load a core object with basic functionality for the scenes
        self.core = Core(graphics_dir = graphics_dir)
        # Pass the core a rference to the window, so the scenes can access it =

        # Now add the first scene.
        self.scene = first_scene(self.core)

    def event_loop(self):
        """Start the main event loop."""
        while not
            # First let pyglet call the event listener functions.
            # Wait a moment, so we don't take up all processing power
            # Then clear the screen, update everything and show it. 

    def update(self):
        """Update the screen. 

This means first updating the state of everything and then blitting it on the 

Also do scene switching, when the scene calls for it. 
            for i in self.scene.visible:
            for i in self.scene.overlay:
        # If the scene defined a scene to switch to, we replace the scene with that new scene. 
        if self.scene.switch_to_scene: 
            self.scene = self.scene.switch_to_scene

    def on_key_press(self, symbol, modifiers):
        """Forward all key events to the scene, if the scene takes them. 

            - catch some key events directly as game controls (right, left, up, down, fire, ...), so we can define a keyboard layout at the game level and have every scene take that automatically. 
            self.scene.on_key_press(symbol, modifiers)

### Command Line UI ###

def _help():
    return "\n".join(__doc__.splitlines()[2:])

### Self-Test ###

if __name__ == "__main__": 
    from sys import argv
    # Firstoff: If the user wants help, then help out :) 
    if "--help" in argv or "-h" in argv: 
        print _help()
    # Initialize the game
    if "--fullscreen" in argv: 
	game = Game(fullscreen = True)
	game = Game()

    # Get the first scene via the commandline. 
    # Remove this, if your players should be able to skip scenes. 
    if len(argv) > 1: 
        mod = eval("__import__('" + argv[1].split(".")[0] + "')")
        game.scene = mod.__dict__[argv[1].split(".")[1]](game.core)
    # Activate supported events
    # key press
    def on_key_press(symbol, modifiers): 
        game.on_key_press(symbol, modifiers)
    # key release
    def on_key_release(symbol, modifiers): 
        game.scene.on_key_release(symbol, modifiers)
    # mouse press
    def on_mouse_press(x, y, buttons, modifiers): 
        game.scene.on_mouse_press(x, y, buttons, modifiers)
    # mouse drag
    def on_mouse_drag(x, y, dx, dy, buttons, modifiers): 
        game.scene.on_mouse_drag(x, y, dx, dy, buttons, modifiers)
    # mouse press
    def on_mouse_release(x, y, buttons, modifiers): 
        game.scene.on_mouse_release(x, y, buttons, modifiers)
    # output all events
    # Start the game loop.