def __init__(self, position, statics): super(Powerup, self).__init__() self.x, self.y = position self.rect = pygame.rect.Rect(0, 0, 0, 0) self.statics = statics self.rect.centerx, self.rect.bottom = position while distfromground(self.rect, self.statics) > 0: # Ensure this thing is on the ground self.y += 1 self.rect.bottom = self.y if distfromground(self.rect, self.statics) == game_constants.big_distance: # fallen off the screen self.kill() break self.image = None # needs to be overloaded by other classes self.loadanims()
def tick(self): self.delayframes += 1 if self.state in (States.falling, States.jumping, States.hit): if self.velocity > game_constants.terminal_velocity: self.velocity -= .25 # If we're falling, check how far we are from the nearest ground. If we're further than one tick's distance, # move jumpspeed units. If we're closer than one tick's distance, move directly to the ground. if self.state in (States.falling, States.hit): dist = distfromground(self.rect, self.colliders) if dist <= abs(self.velocity): self.move_vertical(dist) if self.state == States.hit: if self.delayframes > game_constants.hitframes: self.moving = True self.state = States.running elif self.moving: self.state = States.running else: self.state = States.idle else: self.move_vertical(-self.velocity) elif self.state == States.jumping: dist = distfromceiling(self.rect, self.colliders) if dist <= self.velocity: self.move_vertical(-dist) self.state = States.falling self.velocity = 0 else: self.move_vertical(-self.velocity) if self.velocity <= 0 or self.collide(): self.state = States.falling elif self.state == States.running: newdist = distfromground(self.rect, self.colliders) if newdist > 0: self.state = States.falling if not self.moving: self.state = States.idle # Not moving but run animation is being displayed if self.moving: jump_slow = False if self.state == States.jumping and self.jump_target <> None: # Determine if moving laterally too fast will botch the jump # to wit: will the top of the player rect be higher than the bottom of the platform # before the edge of the player closest to the platform is within the platform # Ugly fudge factor of five pixel units included. if self.direction == 'l': if (self.rect.left - game_constants.speed - 5) < self.jump_target.rect.right and \ (self.rect.top > self.jump_target.rect.bottom): jump_slow = True elif self.direction == 'r': if (self.rect.right + game_constants.speed + 5) > self.jump_target.rect.left and \ (self.rect.top > self.jump_target.rect.bottom): jump_slow = True if self.state in ( States.running, States.jumping, States.falling ) and not jump_slow: # Left-Right movement is allowed oldpos, oldrect = self.position, self.rect if self.direction == 'l': self.move_horizontal(-game_constants.speed) elif self.direction == 'r': self.move_horizontal(game_constants.speed) if self.collide(): self.position, self.rect = oldpos, oldrect self.setanim() self.current_anim.tick() if self.selected_opponent: self.point(self.selected_opponent ) # Point weapon in direction of selected thing for bullet in self.bullets: bullet.move() # Move all the laser-things that might be on screen self.moving = False # Require the move() function to refresh this every tick
def tick(self): self.delayframes += 1 if self.state in (States.falling, States.jumping, States.hit): if self.velocity > game_constants.terminal_velocity: self.velocity -= .25 # If we're falling, check how far we are from the nearest ground. If we're further than one tick's distance, # move jumpspeed units. If we're closer than one tick's distance, move directly to the ground. if self.state in (States.falling, States.hit): dist = distfromground(self.rect, self.colliders) if dist <= abs(self.velocity): self.move_vertical(dist) if self.state == States.hit: if self.delayframes > game_constants.hitframes: self.moving = True self.state = States.running elif self.moving: self.state = States.running else: self.state = States.idle else: self.move_vertical(-self.velocity) elif self.state == States.jumping: dist = distfromceiling(self.rect, self.colliders) if dist <= self.velocity: self.move_vertical(-dist) self.state = States.falling self.velocity = 0 else: self.move_vertical(-self.velocity) if self.velocity <= 0 or self.collide(): self.state = States.falling elif self.state == States.running: newdist = distfromground(self.rect, self.colliders) if newdist > 0: self.state = States.falling if not self.moving: self.state = States.idle # Not moving but run animation is being displayed if self.moving: jump_slow = False if self.state == States.jumping and self.jump_target <> None: # Determine if moving laterally too fast will botch the jump # to wit: will the top of the player rect be higher than the bottom of the platform # before the edge of the player closest to the platform is within the platform # Ugly fudge factor of five pixel units included. if self.direction == 'l': if (self.rect.left - game_constants.speed - 5) < self.jump_target.rect.right and \ (self.rect.top > self.jump_target.rect.bottom): jump_slow = True elif self.direction == 'r': if (self.rect.right + game_constants.speed + 5) > self.jump_target.rect.left and \ (self.rect.top > self.jump_target.rect.bottom): jump_slow = True if self.state in (States.running, States.jumping, States.falling) and not jump_slow: # Left-Right movement is allowed oldpos, oldrect = self.position, self.rect if self.direction == 'l': self.move_horizontal(-game_constants.speed) elif self.direction == 'r': self.move_horizontal(game_constants.speed) if self.collide(): self.position, self.rect = oldpos, oldrect self.setanim() self.current_anim.tick() if self.selected_opponent: self.point(self.selected_opponent) # Point weapon in direction of selected thing for bullet in self.bullets: bullet.move() # Move all the laser-things that might be on screen self.moving = False # Require the move() function to refresh this every tick