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 __init__(self, sprite_filename, target, speed): """sprite_filename is the sprite file target is the Sprite that the bullet follows """ ASprite.__init__(self, sprite_filename, speed) self.target = target self.speed = speed
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 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)
def __init__(self, window_size, sprite_filename, speed, x, y): """Creates the enemy and places in the default upper left position window_size is a tuple with the window dimensions (width, height) sprite_filename is the sprite for the ship speed is the number of pixels it can move at once x, y are the sprite's column and row (resp.) in the formation """ ASprite.__init__(self, sprite_filename, speed) self.window_size = window_size self.col = x self.row = y
def __init__(self, window_size, sprite_filename, speed): """Creates the player's ship window_size is a tuple with the window dimensions (width, height) sprite_filename is the sprite for the ship speed is the number of pixels per frame that can be moved """ ASprite.__init__(self, sprite_filename, speed) self.window_size = window_size #The ship always starts at the bottom center of the screen bottom_pos = window_size[1] - 10 x_pos = window_size[0] / 2 self.rect = self.image.get_rect(bottom = bottom_pos, centerx = x_pos)
def __init__(self, window_size, sprite_filename, speed, position, direction): """Creates a bullet that moves vertically window_size is a tuple with the window dimensions (width, height) sprite_filename is the sprite for the bullet speed is the number of pixels moved each frame position is a tuple for the coordinates of the center of the bullet direction is a vector """ ASprite.__init__(self, sprite_filename, speed) self.window_size = window_size self.direction = direction self.rect = self.image.get_rect(center = position)
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 __init__(self, manager, target, sprite_filename): """manager is the base Cannon object target is the player's sprite """ State.__init__(self, manager) self.sprite = ASprite(sprite_filename, 0) self.target = target self.STATE_NUMBER = manager.STANDBY
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
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
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()
def __init__(self, sprite_filename, speed, position, direction): ASprite.__init__(self, sprite_filename, speed) self.rect.center = position self.direction = direction