Source

fungus / fungus_collisions.py

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

"""Fungus collisions :) 

"""

#### 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
    from fungus_core import call_this_scene
    # pass the commandline args
    from sys import argv
    call_this_scene(__file__, argv)


#### Imports ####

from fungus_scene import BaseScene

### Things needed for the scene

IMAGE_BLOB = "blob.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."""
        
        ## 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)
	
        ## Tests - not necessary for every scene. 
        # Add a blob to the visible items. 	
        self.blob = self.core.sprite(IMAGE_BLOB, x=212, y=208,
                                     update_func=self.blob_update)

        self.visible.append(self.blob)
	# and give it some random velocity
	from random import random
	self.random = random
	self.blob.dy = 3*random()
	self.blob.dx = 3*random()
	# also we want to be able to spawn blobs
	self.spawns = []

    def blob_update(self, x, y, dx, dy): 
        """Update the tank position."""
        # Move the tank down. 
        # If we hit an edge, we turn around and spawn another blob which can't be controlled. 
        if y < 0:
	    dy *= -1
	    y += 1
	    blob = self.core.sprite(IMAGE_BLOB, x=x, y=y,
                                     update_func=self.blob_update)
	    blob.dx = 3*self.random()
	    blob.dy = 3*self.random()
	    self.visible.append(blob)
	    self.spawns.append(blob)
	elif y + self.blob.height > self.core.win.height: 
	    dy *= -1
	    y -= 1
	    blob = self.core.sprite(IMAGE_BLOB, x=x, y=y,
                                     update_func=self.blob_update)
	    blob.dx = -3*self.random()
	    blob.dy = -3*self.random()
	    self.visible.append(blob)
	    self.spawns.append(blob)
        if x < 0: 
	    dx *= -1
	    x += 1
	    blob = self.core.sprite(IMAGE_BLOB, x=x, y=y,
                                     update_func=self.blob_update)
	    blob.dx = 3*self.random()
	    blob.dy = 3*self.random()
	    self.visible.append(blob)
	    self.spawns.append(blob)
	elif x + self.blob.width > self.core.win.width:
	    dx *= -1
	    x -= 1
	    blob = self.core.sprite(IMAGE_BLOB, x=x, y=y,
                                     update_func=self.blob_update)
	    blob.dx = -3*self.random()
	    blob.dy = -3*self.random()
	    self.visible.append(blob)
	    self.spawns.append(blob)
	    
        return x, y, dx, dy
	
    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. 
"""	
	for spawn in self.spawns: 
	    spawn.update()
        self.blob.update()
	# keep at most 30 spawns so it stays fast
	from random import choice
	while self.spawns[20:]: 
	    to_die = choice(self.spawns)
	    self.visible.remove(to_die)
	    self.spawns.remove(to_die)