Source

fungus / fungus_01_intro.py

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

"""An example for a fungus scene definition. """

#### Call the correct fungus_game when called from the command line ####

if __name__ == "__main__": 
    # Call this Scene via fungus_game
    from fungus_core import call_this_scene
    # pass the commandline args
    from sys import argv
    call_this_scene(__file__, argv)

#### Imports ####

from os import path
from pyglet.window import key
from time import time
# TODO: Remove the need to import images via pyglet - use the core object we pass to the scene. 

from fungus_scene import BaseScene
from fungus_core import __copyright__

#### An example scene ####

### Things needed for the scene ###

## A Level ##

# We want to display a level: That's simply a list of tiles. 
# For convenient level creation we show it as a list of letters. 
# This is for still level parts. 

# Levels currently always get shown from the top left corner. If you want 
# scrolling levels, add additional parts at the right or the bottom of the 
# level. 

# TODO: Add an option "top left corner" or similar, which signifies the level 
# position. 

# To make this cleaner and the game look nicer, this init of objects makes 
# (obj, objRect) tile tuples

o = 'floor1.png' # clear
t = 'tile2.png' # tree
g = 'tile3.png' # ground
r = 'tile4.png' # road

level = [
[o, o, o, o, o, o, r, r, r, o, o, o, o, o, o, o, o], 
[o, o, o, t, o, o, r, r, r, o, o, t, o, o, o, o, o], 
[o, o, o, o, o, o, r, r, r, o, o, t, o, o, o, o, o], 
[o, o, g, o, o, o, r, r, r, t, o, o, o, o, o, o, o], 
[o, o, o, o, o, o, r, r, r, o, o, o, o, o, o, o, o], 
[o, o, o, o, o, o, r, r, r, o, o, o, o, o, o, o, o], 
[o, o, o, o, o, o, r, r, r, o, g, o, o, o, o, o, o], 
[o, o, o, o, o, g, r, r, r, o, o, o, o, o, o, o, o], 
[o, o, o, o, o, o, r, r, r, o, o, o, o, o, o, o, o], 
[o, o, o, o, o, o, r, r, r, o, o, o, o, o, o, o, o], 
[o, o, o, o, o, o, r, r, r, o, o, o, o, o, o, o, o], 
[o, o, o, o, o, o, r, r, r, o, o, o, o, o, o, o, o], 
[o, o, o, o, o, o, r, r, r, o, o, o, o, o, o, o, o], 
[o, o, o, o, o, o, r, r, r, o, o, o, o, o, o, o, o], 
[o, o, o, o, o, o, r, r, r, o, o, o, o, o, o, o, o], 
[o, o, o, o, o, o, r, r, r, o, o, o, o, o, o, o, o], 
[o, o, o, o, o, o, r, r, r, o, o, o, o, o, o, o, o], 
[o, o, o, o, o, o, r, r, r, o, o, o, o, o, o, o, o], 
[o, o, o, o, o, o, r, r, r, o, o, o, o, o, o, o, o], 
[o, o, o, o, o, o, r, r, r, o, o, o, o, o, o, o, o], 
[o, o, o, o, o, o, r, r, r, o, o, o, o, o, o, o, o], 
]


### The Scene itself. 

class Scene(BaseScene): 
    """A dummy scene - mostly just the Scene API."""
    def __init__(self, core, *args, **kwds): 
        """Initialize the scene with a core object for basic functions."""
	
        ## Get the necessary attributes for any scene. 
        # This gets the 'visible', 'colliding' and 'overlay' lists 
        # as well as the scene switch 'switch_to_scene' 
        # which can be assigned a scene to switch to. 
        super(Scene, self).__init__(core, *args, **kwds)
	
	if "--show-fps" in args[0]:
	    self.show_fps = True
	    args[0].remove("--show-fps")
	else: 
	    self.show_fps = False
	
	if self.show_fps: 
	    self.time = time()
	
	
	# First add the batches for background, foreground, sprites and overlay. They allow for showing multiple sprites together (and optimized). 
	self.background = self.core.batch()
	#self.foreground = self.core.batch() # for items which will show in front of the actor. 
	self.sprites = self.core.batch()
	self.overlay = self.core.batch()
	# and make them visible
	self.visible.append(self.background)
	#self.visible.append(self.foreground)
	self.visible.append(self.sprites)
	self.visible.append(self.overlay)
	
	
        # Add an actor, who should be controllable with the keyboard.
        self.actor = self.core.sprite("tank5.png", x=217, y=183, batch=self.sprites)
	# (self.actor is controllable via keyboard - the events are passed to self.actor)
        
        # Also add a speach box to the overlay
        self.text_box = self.core.sprite("Dialog-Background-small.png", y=-2, batch=self.overlay)
        # along with a text to show in that. 
        self.text = self.core.load_text('Just get us back to the bunker before sunset.', x=14, y=48, batch=self.overlay)

        # Load the level
	self.level = self.load_level(level)

    def load_level(self, level): 
        """Load a level with image names and return it as level with image objects."""
        new_level = []
        for y in range(len(level)): 
            new_level.append([])
            for x in range(len(level[y])): 
                # Load a sprite with the coordinates from the level. 
                sprite = self.core.sprite(level[y][x], x=32*x, y=32*y, batch=self.background)
                new_level[y].append(sprite)
        return new_level

    def update(self): 
        """Update the stats of all scene objects. 

Don't blit them, though. That's done by the Game itself.

To show something, add it to the self.visible list. 
To add a collider, add it to the self.colliding list. 
To add an overlay sprite, add it to the self.overlay list. 
"""
        # Update the actor
        self.actor.update()
        # move all level tiles
        for x in self.level: 
            for y in x: 
                # Move the tile
                y.y +=1
                # If we leave the screen, come back from the other side. 
                if y.y >= 512:
                    y.y = -31
        
	if self.show_fps: 
	    print 1.0 / max(0.00001, time() - self.time)
	    self.time = time()
    
    def on_key_press(self, symbol, modifiers): 
        """Forwarded keyboard input."""
        # Use the escape key as a means to exit the game. 
        if symbol == key.ESCAPE: 
            self.core.win.has_exit = True
        # TODO: Remove this debug output
        else: 
            print modifiers, symbol
        #: Basic actor movement with the keyboard
        self.core.keyboard_movement_key_press(self.actor, symbol, modifiers)
            
    def on_key_release(self, symbol, modifiers): 
        """Forwarded keyboard input."""
        #: Basic actor movement with the keyboard
        self.core.keyboard_movement_key_release(self.actor, symbol, modifiers)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.