class Fireball(sf.Drawable, Collideable): def __init__(self, x, y, world): Collideable.__init__(self, x, y, 100, 100, False) self._sprite = SpriteSheet(Res.fireball) self._sprite.init(10, 10, 0.1) self._sprite.position = sf.Vector2(x, y) self.world = world self.dead = False def draw(self, target, render_states): target.draw(self._sprite, render_states) def update(self, dt): self._sprite.update(dt) self.position.y += FIREBALL_MOVE_SPEED*dt # Update player sprite position self._sprite.position = self.position def on_collision_begin(self, other, side): if self.dead: return False self.world.create_explosion(self.position.x, self.position.y) self.dead = True if hasattr(other, "health"): other.health -= 1 return False def on_collision_end(self, other): pass
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
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