Example #1
0
 def create_explosion(self, x, y):
     explosion = SpriteSheet(Res.explosion)
     explosion.init(16, 4, 0.1)
     explosion.position = sf.Vector2(x, y)
     explosion.set_frame_loop(0, 9, False)
     self.explosions.append(explosion)
     Res.explosion_sound.loop = False
     Res.explosion_sound.volume = 10.0
     #Res.explosion_sound.position = (x, y, 0)
     Res.explosion_sound.play()
Example #2
0
class UFO(sf.Drawable, Collideable):
    def __init__(self, x, y, world):
        Collideable.__init__(self, x, y, 128, 64, True)
        self._sprite = SpriteSheet(Res.ufo)
        self._sprite.init(7, 7, 0.1)
        self._sprite.set_frame_loop(0, 5)
        self._sprite.position = sf.Vector2(x, y)
        self.world = world
        self.time = 3
        self.timer = 0
        self.shoot_time = 1
        self.shoot_timer = 0
        self.direction = 1
        
    def draw(self, target, render_states):
        target.draw(self._sprite, render_states)
    
    def update(self, dt):
        self._sprite.update(dt)
        
        self.timer += dt
        if self.timer >= self.time:
            # Switch directions
            self.direction *= -1
            # Reset the timer
            self.timer=0
        
        self.shoot_timer += dt
        if self.shoot_timer >= self.shoot_time:
            # Reset the timer
            self.shoot_timer=0
            self.world.create_fireball(self.position.x, self.position.y+70)

        self.position = self.position + sf.Vector2(75*dt, 0)*self.direction
            
        # Update player sprite position
        self._sprite.position = self.position
        
    def on_collision_begin(self, other, side):
        return False

    def on_collision_end(self, other):
        pass
Example #3
0
class Player(sf.Drawable, Collideable, KeyHandler):
    def __init__(self, x, y):
        Collideable.__init__(self, x, y, 24, 44)
        self._sprite = SpriteSheet(Res.blue_peewee)
        self._sprite.init(36, 6, 0.1)
        self._sprite.set_frame_loop(0, 5)
        self._sprite.position = sf.Vector2(x, y)
        self.current_checkpoint = sf.Vector2(x, y)
        
        self.health = 5
        
        # Controls
        self._move_left = False
        self._move_right = False
        self._on_ground = False

        # Direction -- for animating stuff
        self._direction = DIR_RIGHT
        
        self._vertical_velocity = 0
    
    def draw(self, target, render_states):
        target.draw(self._sprite, render_states)
    
    def update(self, dt):
        self._sprite.update(dt)

        ### TUTORIAL 1:
        # Uncomment the following 4 lines
        if self._move_left:
            self.position.x -= PLAYER_MOVE_SPEED*dt
        elif self._move_right:
            self.position.x += PLAYER_MOVE_SPEED*dt
        
        # Apply Vertical velocity
        self.position.y += self._vertical_velocity*dt

        if not self._on_ground:
            # Apply gravity
            self._vertical_velocity += 1000*dt

            # Animations when player is off ground
            if self._vertical_velocity <= 0: # Going up
                if self._direction == DIR_LEFT:
                    self._sprite.set_frame_loop(33, 33)
                elif self._direction == DIR_RIGHT:
                    self._sprite.set_frame_loop(30, 30)
            elif self._vertical_velocity > 0: # Going down
                if self._direction == DIR_LEFT:
                    self._sprite.set_frame_loop(33, 35, False)
                elif self._direction == DIR_RIGHT:
                    self._sprite.set_frame_loop(30, 32, False)
        else: # On ground
            if self._move_left:
                self._sprite.set_frame_loop(12, 17)
            elif self._move_right:
                self._sprite.set_frame_loop(18, 23)
            else: # Not moving
                if self._direction == DIR_LEFT:
                    self._sprite.set_frame_loop(1, 1)
                elif self._direction == DIR_RIGHT:
                    self._sprite.set_frame_loop(6, 6)

        # Update player sprite position
        self._sprite.position = self.position-sf.Vector2(23, 10)
    
    def on_key_pressed(self, key_code):
        if key_code == sf.Keyboard.A and not self._move_right:
            self._move_left = True
            self._direction = DIR_LEFT
        elif key_code == sf.Keyboard.D and not self._move_left:
            self._move_right = True
            self._direction = DIR_RIGHT
        elif key_code == sf.Keyboard.SPACE and self._on_ground:
            self._vertical_velocity = -PLAYER_JUMP_VELOCITY
    
    def on_key_released(self, key_code):
        if key_code == sf.Keyboard.A:
            self._move_left = False
        elif key_code == sf.Keyboard.D:
            self._move_right = False
    
    def on_collision_begin(self, other, side):       
        if other.stationary:
            if side == physics.side_down or side == physics.side_up:
                self._vertical_velocity = 0
            if side == physics.side_down:
                self._on_ground = True
        return True
    
    def on_collision_end(self, other):
        self._on_ground = False