Source

hexbattle / tank.py

Full commit
#!/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 core import run
    run(__file__)

#### Imports ####

from os import path
from pyglet.window import key
from pyglet.text import Label
from pyglet.graphics import Batch
from pyglet.resource import texture
from pyglet.clock import schedule_interval, schedule_once
from time import time
# TODO: Remove the need to import images via pyglet - use the core object we pass to the scene. 

from core import __copyright__, core, Sprite

#### 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, g, 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, g, 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(object): 
    """A dummy scene - mostly just the Scene API."""
    def __init__(self, *args, **kwds): 
        """Initialize the scene with a core object for basic functions."""
    
        self.core = core
        self.visible = []
        # First add the batches for background, foreground, sprites and overlay. They allow for showing multiple sprites together (and optimized). 
        self.background = Batch()
        #self.foreground = Batch() # for items which will show in front of the actor. 
        self.sprites = Batch()
        self.overlay = 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 = Sprite("tank5.png", x=217, y=183, batch=self.sprites)
        
        # Also add a speach box to the overlay
        self.text_box = Sprite("Dialog-Background-small.png", x=0, y=-2, batch=self.overlay)
        # along with a text to show in that. 
        self.text = Label('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 sprites."""
        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 = Sprite(level[y][x], x=32*x, y=32*y, batch=self.background)
                new_level[y].append(sprite)
        return new_level

    def update(self, dt=0): 
        """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 y in self.level: 
            for x in y: 
                # Move the tile
                x.y += 1
                # If we leave the screen, come back from the other side. 
                if x.y >= core.win.height:
                    x.y -= core.win.height + 31
        
    
    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
        pass
            
    def on_key_release(self, symbol, modifiers): 
        """Forwarded keyboard input."""
        pass

def start(dt):
    """Start the scene."""
    # make all visible items show themselves 
    # anytime the screen is refreshed
    scene = Scene()
    @core.win.event
    def on_draw():
        core.win.clear()
        for i in scene.visible:
            i.draw()

    # update the scene every 10ms - independent of showing the scene
    schedule_interval(scene.update, 0.01)
    core.win.push_handlers(scene)