Source

gallox / gallox / sprites / static.py

Full commit
import pygame

from gallox.utils import load_image, load_sound
                
class Plattform(pygame.sprite.Sprite):
    def __init__(self, pos, size, params, map, *groups):
        pygame.sprite.Sprite.__init__(self, *groups)
        self.map = map
        self.tank_size = self.fuel = params.get("tankSize", 20)
        self.fuel_speed = params.get("fuelSpeed", 7)
        self.restock_rate = params.get("restockRate", 3)
        self.image, self.rect = load_image("plattform")
        self.image = pygame.transform.scale(self.image, size)
        self.rect = self.image.get_rect(topleft=pos)
#        self.docked_sound = load_sound("plattform2.wav")
        self.fueling_channel = None
        self.docked = None
        self.fueling = False
        
    def dock(self, ship):
        print "ship docked successfully"
#        self.docked_sound.play(loops=-1)
        self.docked = ship
        
        # check for fuelling
        if ship.fuel < 100 and self.fuel > 0 and self.fuel_speed:
            self.fueling = True
            self.fueling_channel = self.map.sound("fueling", self.rect.center)
            #! start fueling sound

    def undock(self):
        print "undocked"
#        self.docked_sound.stop()
        self.docked = None
        # stop sound (don't care if its actually still playing)
        self.map.hush(self.fueling_channel)
        self.fueling_chanel = None
        
    def update(self, events, ticks):
        if self.docked and self.fueling:
            amount = min(self.fuel_speed * ticks / 1000., self.fuel)
            self.fuel -= amount
            self.docked.fuel += amount
            # self empty or ship full
            if self.fuel == 0 or self.docked.fuel == 100:
                self.fueling = False
                # stop sound
                self.map.hush(self.fueling_channel)
                self.fueling_chanel = None
                
        elif self.fuel != self.tank_size:
            # restock
            self.fuel += min(self.restock_rate * ticks / 1000., 
                             self.tank_size - self.fuel)