fungus /

The default branch has multiple heads

#!/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

      show the game in fullscreen mode (no scaling, just a larger screen)
    --width xxx --height yyy 
      Show the image with the specified width and height (xxx and yyy as numbers)

    - 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
# first disable GL debugging (massive speed penalty).
import pyglet
pyglet.options['debug_gl'] = False
# now import the rest
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 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=800, height=600, 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(mod=None):
    """Provide help from this main module and optionally from the selected scene.
    @param mod: module in which the selected scene is defined - this contains the second help string to use.
    if mod is None or mod.__doc__ is None: 
	return __doc__
	return mod.__doc__

### 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: 
	# remove the help argument
	if "--help" in argv: 
	if "-h" in argv: 
	# if a module is in the argv, try to import it, so its help gets shown alongside. 
	# This allows calling ./ --help
	# In that case the argv looks like this: 
	# ['./', 'fungus_scene.Scene', '--help']
	if argv[1:] and argv[1].endswith(".Scene"): 
		mod = eval("__import__('" + argv[1].split(".")[0] + "')")
		print _help(mod)
	    except: print _help()
	    print _help()
    # Initialize the game
    if "--fullscreen" in argv: 
	game = Game(fullscreen = True)
    elif "--width" in argv and "--height" in argv: 
	width = argv[argv.index("--width") + 1]
	height = argv[argv.index("--height") + 1]
	width = int(width)
	height = int(height)
	game = Game(width=width, height=height)
	game = Game()

    # Get the first scene via the commandline. 
    # Remove this, if your players should not be able to skip scenes. 

    # Note: At this point all command line args 
    # which were added before the scene name 
    # must already be removed from argv! 

    if argv[1:]: 
        mod = eval("__import__('" + argv[1].split(".")[0] + "')")
	# Pass all later args to the scene when importing it. 
        game.scene = mod.__dict__[argv[1].split(".")[1]](game.core, argv[2:])
    # 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 motion
    def on_mouse_motion(x, y, dx, dy):
        game.scene.on_mouse_motion(x, y, dx, dy)
    # 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)
    def on_mouse_enter(x, y):
        game.scene.on_mouse_enter(x, y)
    def on_mouse_leave(x, y):
        game.scene.on_mouse_leave(x, y)
    def on_mouse_scroll(x, y, scroll_x, scroll_y):
        game.scene.on_mouse_scroll(x, y, scroll_x, scroll_y)

    # output all events
    # Start the game loop.