def __init__(self, img, position, world, velocity=None): super().__init__() self.image = img self.position = position * CELL_SIZE self.rect = pygame.sprite.Rect(position.x, position.y, 0.5 * CELL_SIZE, CELL_SIZE) self.acceleration = bs.Point(0, 0) self.velocity = bs.Point(0, 0) if (velocity is None) else velocity self.world = world if (world is not None) else None self.jumping = False self.ground_detector = GroundDetector( self.position + bs.Point(0, CELL_SIZE), self.world) self.direction = bs.Direction.RIGHT
def accelerate(self, ax, ay): """ Change the velocity of the object. Args: ax: Acceleration in the direction of the x axis. ay: Acceleration in the direction of the y axis. """ # Do not let jump, if not on a solid ground. if (not self.ground_detector.is_on_ground() and ay < 0): ay = 0 # Actual acceleration. self.acceleration += bs.Point(ax / INERTIA_X, ay / INERTIA_Y) self.velocity += self.acceleration # Speed limit in each direction. x_direction = 1 if (self.velocity.x > 0) else -1 y_direction = 1 if (self.velocity.y > 0) else -1 if (abs(self.velocity.x) > SPEED_LIMIT): self.velocity.x = SPEED_LIMIT * x_direction if (abs(self.velocity.y) > SPEED_LIMIT): self.velocity.y = SPEED_LIMIT * y_direction # Set directions if (self.velocity.x > 0): self.direction = bs.Direction.RIGHT elif (self.velocity.x < 0): self.direction = bs.Direction.LEFT
def __init__(self, img, x, y, level): super().__init__(img, bs.Point(x, y), level) self.foe_health = 3 self.immortality_timer = 0 self.reload_timer = 0 self.bullets = FOE_BULLETS_PER_BURST self.current_animation_model = 0
def move(self): """ Change the position of the object. Method uses the velocity and the current position of an object to determine the position of an object after the unit time. It changes the value of the attribute position to the new positions value and also in case of collision sets velocity along the appropriate axes to 0. """ # Move. old_position = self.position self.position += self.velocity + 0.5 * self.acceleration self.rect.x = round(self.position.x) self.rect.y = round(self.position.y) self.ground_detector.update(self.rect) # Handle collisions. # Complexity is constant, because the character is allowed to # have only 8 neighboring sprites being platform or bridge. # In practise there is up to 3 (or 4) collisions possible. collisions = ( pygame.sprite.spritecollide(self, self.world.floors, False) + pygame.sprite.spritecollide(self, self.world.corners, False) + pygame.sprite.spritecollide(self, self.world.bridges, False)) ground_collisions = self.ground_detector.ground_sprites() for collision in collisions: if (collision in ground_collisions and old_position.y + CELL_SIZE <= collision.rect.top): self.rect.bottom = collision.rect.top self.velocity.y = 0 # Check collisions with walls - horizontal ones collisions = ( pygame.sprite.spritecollide(self, self.world.walls, False) + pygame.sprite.spritecollide(self, self.world.corners, False)) for collision in collisions: if (collision.rect.centerx > self.rect.centerx): self.rect.right = collision.rect.left self.velocity.x = 0 else: self.rect.left = collision.rect.right self.velocity.x = 0 # Update position after collisions. self.position.x = self.rect.x self.position.y = self.rect.y self.ground_detector.update(self.rect) # Clear the acceleration to avoid jiggling on the floor (it simulates # reaction force of the ground)- the 'update' method will take care # of gravity. self.acceleration = bs.Point(0, 0)
def __init__(self, img, x, y, level): super().__init__(img, bs.Point(x, y), level) self.boss_health = 5 self.boss_direct = bs.Direction.RIGHT self.immortality_timer = 0 self.reload_timer = 0
def __init__(self, x=1, y=1): # Default is (1, 1), because physics objects like heroes scale the # vectors by themselves to match the grid. self.position = bs.Point(x, y)
def die(self): self.health = HERO_HEALTH self.position = self.level.spawn.position * CELL_SIZE self.velocity = bs.Point(0, 0) self.acceleration = bs.Point(0, 0) self.immortality_timer = IMMORTALITY_TIME