Source

fungus / fungus_advection.py

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

"""Showing the moving blobs.

Requires ./blobs_on_plb_velocity_field.py in the path.
"""

from sys import path
import sys
path.append("/home/arne/Projekte/Diplom/sonstiges/Textursynthese")
path.append("/home/arne/Diplomarbeit/sonstiges/Textursynthese")

#### Call the correct fungus_game when called from the command line or clicked in a GUI ####

if __name__ == "__main__": 
    # Call this Scene via fungus_game
    # For this to work, the main scene inside the module MUST be the class with the name "Scene"
    from fungus_core import call_this_scene
    # pass the commandline args
    from sys import argv
    call_this_scene(__file__, argv)


#### Imports ####

from fungus_core import Sprite
from pyglet.window import key

# display
from fungus_scene import BaseScene

# simulation
from blobs_on_plb_velocity_field import Simulator, blobs

### Scene data

IMAGE_BLOB = "blob_transparent_borders_monotone.png"

### 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."""
        super(Scene, self).__init__(core, *args, **kwds)

        # first we need the Simulator
        self.sim = Simulator()

        # and the simulator output
        global blobs
        self.blobs = blobs

    def add_blob(self, blob):
        """Add a new blob"""
        blobby = self.core.sprite(IMAGE_BLOB, x=blob.x, y=blob.y)
        self.visible.append(blobby)

    def update(self): 
        """Update the stats of all scene objects."""
        # update the backend data
        self.sim.next_step()

        # synchronize the number of blobs with the backend
        for blob in self.blobs[len(self.visible):]:
            self.add_blob(blob)
        for blob in self.visible[len(self.blobs):]:
            self.visible.remove(blob)
        
        # synchronize positions
        for i in range(len(self.blobs)):
            self.visible[i].x = self.blobs[i].x
            self.visible[i].y = self.blobs[i].y

        if self.sim.finished:
            print "FINISHED"
            raw_input("Press any key to exit")
            self.core.win.has_exit = True