def update(self): self.sprite.update() ASprite.move(self.sprite, self.direction) rect = self.sprite.rect if rect.left <= 0 or rect.top <= 0 or rect.bottom >= options.height: self.transition_to(self.manager.MOVING)
def move(self, direction): """moves the sprite in the given direction direction should be a tuple representing a unit vector """ if direction in DIRECTIONS.keys(): self.current_dir = direction else: self.current_dir = round_to_45(direction) ASprite.move(self, direction)
class MovingBase(State): """State 1: Moves up and down along right side of screen. MovingBase should be followable by the shield. """ def __init__(self, manager, sprite_filename, speed, top, bottom, avg_transition): """manager is the root EnemyBase object top, bottom are the boundaries of movement (rect.top <= top, etc) """ State.__init__(self, manager) self.sprite = ASprite(sprite_filename, speed) self.sprite.rect.topright = (options.width, top) self.top = top self.bottom = bottom self.current_dir = vector.SOUTH self.transition_probability = 1.0 / (avg_transition * options.max_framerate) self.STATE_NUMBER = manager.MOVING self.IS_FOLLOWABLE = True def update(self): self.sprite.move(self.current_dir) if random() <= self.transition_probability: self.transition_to(self.manager.SPINNING) if self.sprite.rect.bottom >= self.bottom: self.current_dir = vector.NORTH elif self.sprite.rect.top <= self.top: self.current_dir = vector.SOUTH def transition_to(self, new_state_number): if new_state_number == self.manager.SPINNING: self.manager.start_spinner(self.sprite.rect.center) return True return False
def move(self, direction): """Moves the ship in the specified direction unless stopped by borders direction is a vector """ ASprite.move(self, direction) width, height = self.window_size #if ship is off top or bottom edges, nudge back to the edge if self.rect.top < 0: self.rect.top = 0 if self.rect.bottom > height: self.rect.bottom = height #if ship is off left or right edges, nudge back to the edge if self.rect.left < 0: self.rect.left = 0 if self.rect.right > width: self.rect.right = width
class FiringCannon(State): """State 3: moves in one direction (initially right) across screen Note: will probably be animated in the future; this doesn't change behavior""" def __init__(self, manager, position, sprite_filename, speed): """manager is the base Cannon object position is the sprite's initial rect.center coordinate """ State.__init__(self, manager) self.sprite = ASprite(sprite_filename, speed) self.sprite.rect.center = position self.direction = vector.EAST self.STATE_NUMBER = manager.FIRING def update(self): """move in the direction""" self.sprite.move(self.direction) rect = self.sprite.rect if rect.left < 0 or rect.right >= options.width: self.transition_to(self.manager.DEACTIVATED) def transition_to(self, new_state_number): if new_state_number == self.manager.DEACTIVATED: self.manager.start_deactivated() return True if new_state_number == self.manager.RETURNING: self.manager.start_returning(self.sprite.rect.center) return True return False
def update(self): ASprite.move(self, self.direction) if (self.rect.left < 0 or self.rect.top < 0 or self.rect.right > options.width or self.rect.bottom > options.height): self.kill()