def _create_triggers(self): self.triggers = [ MinigameTrigger( 12 * 16, 6 * 16 + 4, 32, 16, Vector2( 16, Camera.BOUNDS.height / 2 ), SceneType.COFFEE_MINIGAME ), MinigameTrigger( 7 * 16, 1 * 16 + 4, 32, 16, Vector2( Camera.BOUNDS.width / 2, Camera.BOUNDS.height / 2 + 16 ), SceneType.FISH_MINIGAME ), CollisionTrigger( 0, Camera.BOUNDS.height, Camera.BOUNDS.width, 16, Vector2(18 * 16, 1 * 16), SceneType.VILLAGE ) ]
def set_location(self, x, y): self.x = x self.y = y self.location = Vector2(self.x, self.y) self.center = Vector2(self.x + self.width / 2, self.y + self.height / 2) self.bounds = Rect(self.x, self.y, self.width, self.height)
def __cohesion(self): steer = Vector2(0, 0) cumulative = Vector2(0, 0) distance = 0 total = 0 for e in self.query_result: if e is self: continue distance = Vector2.distance_between(e.location, self.location) if distance > 0 and distance < self.view_radius: cumulative.add(e.location) total += 1 if total > 0: cumulative.divide(total) cumulative.subtract(self.location) cumulative.set_magnitude(self.default_move_speed) steer = Vector2(cumulative.x - self.velocity.x, cumulative.y - self.velocity.y) steer.limit(self.max_force) return steer
def __scaled_location(x, y, camera_type): if camera_type == CameraType.DYNAMIC: return Vector2(x * Camera.scale - Camera.top_left.x, y * Camera.scale - Camera.top_left.y) if camera_type == CameraType.STATIC: return Vector2(x * StaticCamera.scale - StaticCamera.top_left.x, y * StaticCamera.scale - StaticCamera.top_left.y)
def __init__(self, x, y, width, height): self.x = x self.y = y self.width = width self.height = height self.location = Vector2(self.x, self.y) self.center = Vector2(self.x + self.width / 2, self.y + self.height / 2) self.bounds = Rect(self.x, self.y, self.width, self.height)
def _collision(self, scene_data): if self.y + self.height > scene_data.scene_bounds.height + 64: self.remove = True if Vector2.distance_between( Vector2(scene_data.actor.x + scene_data.actor.width, scene_data.actor.y + scene_data.actor.height), self.center) < self.radius: scene_data.actor.get_yeeted()
class Camera: BOUNDS = pygame.Rect(0, 0, 0, 0) scale = 0 top_left = Vector2() def __init__(self, top_left=Vector2(0, 0)): Camera.scale = StaticCamera.scale Camera.top_left.x = top_left.x - StaticCamera.horizontal_letterbox Camera.top_left.y = top_left.y - StaticCamera.vertical_letterbox Camera.BOUNDS = pygame.Rect( Camera.top_left.x, Camera.top_left.y, StaticCamera.BOUNDS.width, StaticCamera.BOUNDS.height) def stay_within_bounds(self, top_left, world_bounds): if world_bounds.width == 0 or world_bounds.height == 0: world_bounds = pygame.Rect( 0, 0, Camera.BOUNDS.width, Camera.BOUNDS.height) if top_left.x < world_bounds.left: top_left.x = world_bounds.left if top_left.x + Camera.BOUNDS.width > world_bounds.right: top_left.x = world_bounds.right - Camera.BOUNDS.width if top_left.y < world_bounds.top: top_left.y = world_bounds.top if top_left.y + Camera.BOUNDS.height > world_bounds.bottom: top_left.y = world_bounds.bottom - Camera.BOUNDS.height Camera.top_left.x = top_left.x * Camera.scale - StaticCamera.horizontal_letterbox Camera.top_left.y = top_left.y * Camera.scale - StaticCamera.vertical_letterbox def get_viewport_top_left(self): return Vector2((Camera.top_left.x + StaticCamera.horizontal_letterbox) / Camera.scale, (Camera.top_left.y + StaticCamera.vertical_letterbox) / Camera.scale) def update(self, top_left=Vector2(0, 0), world_bounds=pygame.Rect(0, 0, 0, 0)): Camera.scale = StaticCamera.scale self.stay_within_bounds(top_left, world_bounds)
def __init__(self): self.camera = Camera() self.camera_location = Vector2(0, 0) self.bounds = Rect(0, 0, Camera.BOUNDS.width, Camera.BOUNDS.height) self.camera_viewport = Rectangle( -Scene.VIEWPORT_BUFFER, -Scene.VIEWPORT_BUFFER, Camera.BOUNDS.width + Scene.VIEWPORT_BUFFER * 2, Camera.BOUNDS.height + Scene.VIEWPORT_BUFFER * 2, Color.RED, 2) self.sprites = [] self.entities = [] self.shapes = [] self.triggers = [] self.leave_transition_type = TransitionType.PINHOLE_CLOSE self.enter_transition_type = TransitionType.PINHOLE_OPEN # this is to be set in SceneManager (add scene method) self.manager = None self.player = None # to be set by respective classes self.song = "" self.money_ui = Text(8 + 16 + 4, 8, str(pygine.globals.money)) self.dollar_sign = Text(8, 8, "$")
def __init__(self, top_left=Vector2(0, 0)): Camera.scale = StaticCamera.scale Camera.top_left.x = top_left.x - StaticCamera.horizontal_letterbox Camera.top_left.y = top_left.y - StaticCamera.vertical_letterbox Camera.BOUNDS = pygame.Rect(Camera.top_left.x, Camera.top_left.y, StaticCamera.BOUNDS.width, StaticCamera.BOUNDS.height)
def __init__(self, dimensions, scale): StaticCamera.horizontal_letterbox = 0 StaticCamera.vertical_letterbox = 0 StaticCamera.top_left = Vector2(-StaticCamera.horizontal_letterbox, -StaticCamera.vertical_letterbox) StaticCamera.scale = scale StaticCamera.BOUNDS = pygame.Rect(0, 0, dimensions[0], dimensions[1])
def __init__(self, x, y, width, height, speed): super(Kinetic, self).__init__(x, y, width, height) self.velocity = Vector2() self.default_move_speed = speed self.move_speed = 0 self.facing = Direction.NONE self.collision_rectangles = [] self.collision_width = 0
def __init__(self, x, y): super(Boid, self).__init__(x, y, 8, 8, 50) self.circle = Circle(self.x, self.y, self.width / 2, Color.WHITE, 1) self.steer = Vector2(0, 0) self.acceleration = Vector2(0, 0) self.velocity = Vector2( -self.default_move_speed + randint(0, 5) * self.default_move_speed * 2 / 5, -self.default_move_speed + randint(0, 5) * self.default_move_speed * 2 / 5, ) self.max_force = 0.5 self.view_radius = 16 self.query_result = None
class StaticCamera: BOUNDS = pygame.Rect(0, 0, 0, 0) scale = 0 horizontal_letterbox = 0 vertical_letterbox = 0 top_left = Vector2() letterboxes = [] def __init__(self, dimensions, scale): StaticCamera.horizontal_letterbox = 0 StaticCamera.vertical_letterbox = 0 StaticCamera.top_left = Vector2(-StaticCamera.horizontal_letterbox, -StaticCamera.vertical_letterbox) StaticCamera.scale = scale StaticCamera.BOUNDS = pygame.Rect(0, 0, dimensions[0], dimensions[1]) def apply_horizontal_letterbox(self, horizontal_letterbox): StaticCamera.horizontal_letterbox = horizontal_letterbox StaticCamera.top_left.x = -StaticCamera.horizontal_letterbox def apply_vertical_letterbox(self, vertical_letterbox): StaticCamera.vertical_letterbox = vertical_letterbox StaticCamera.top_left.y = -StaticCamera.vertical_letterbox def draw(self, surface): # Top pygame.draw.rect( surface, Color.BLACK, (-32 * StaticCamera.scale, -32 * StaticCamera.scale, StaticCamera.BOUNDS.width * StaticCamera.scale + 64 * StaticCamera.scale, StaticCamera.vertical_letterbox + 32 * StaticCamera.scale)) # Bottom pygame.draw.rect( surface, Color.BLACK, (-32 * StaticCamera.scale, StaticCamera.vertical_letterbox + StaticCamera.BOUNDS.height * StaticCamera.scale, StaticCamera.BOUNDS.width * StaticCamera.scale + 64 * StaticCamera.scale, StaticCamera.vertical_letterbox + 32 * StaticCamera.scale)) # Left pygame.draw.rect( surface, Color.BLACK, (-32 * StaticCamera.scale, -32 * StaticCamera.scale, StaticCamera.horizontal_letterbox + 32 * StaticCamera.scale, StaticCamera.BOUNDS.height * StaticCamera.scale + 64 * StaticCamera.scale)) # Right pygame.draw.rect( surface, Color.BLACK, (StaticCamera.horizontal_letterbox + StaticCamera.BOUNDS.width * StaticCamera.scale, -32 * StaticCamera.scale, StaticCamera.horizontal_letterbox, StaticCamera.BOUNDS.height * StaticCamera.scale + 64 * StaticCamera.scale))
def update_camera(self): self.camera_location = Vector2( self.player.x + self.player.width / 2 - self.camera.BOUNDS.width / 2, self.player.y + self.player.height / 2 - self.camera.BOUNDS.height / 2 ) self.camera.update(self.camera_location, self.bounds) self.camera_viewport.set_location( self.camera.get_viewport_top_left().x - Scene.VIEWPORT_BUFFER, self.camera.get_viewport_top_left().y - Scene.VIEWPORT_BUFFER)
def _create_triggers(self): self.triggers.append( CollisionTrigger( 5 * 16, 11 * 16 + 10, 32, 16, Vector2(16 + 48 * 1 + 16 + 16, 16 + 64), SceneType.VILLAGE ) )
def _create_triggers(self): self.triggers.append( CollisionTrigger( Camera.BOUNDS.width - 8, 0, 8, Camera.BOUNDS.height, Vector2(16, Camera.BOUNDS.height / 2 - 16), SceneType.VILLAGE ) )
def revive(self): self.grounded = False self.jumping = False self.attempt_block_shift = False self.attacked = False self.restart = False self.queue_restart = False self.velocity = Vector2(0, 0) self.sprite.set_frame(0, 6)
def _create_triggers(self): self.triggers = [ OnButtonPressTrigger( InputType.A, Vector2( self.scene_bounds.width / 2, self.scene_bounds.height / 2 ), SceneType.LEVEL ) ]
def __separation(self): steer = Vector2(0, 0) cumulative = Vector2(0, 0) force = Vector2(0, 0) distance = 0 total = 0 for e in self.query_result: if e is self: continue distance = Vector2.distance_between(e.location, self.location) if distance > 0 and distance < self.width: force = Vector2(self.location.x - e.location.x, self.location.y - e.location.y) force.divide(distance**2) cumulative.add(force) total += 1 if total > 0: cumulative.divide(total) cumulative.set_magnitude(self.default_move_speed) steer = Vector2(cumulative.x - self.velocity.x, cumulative.y - self.velocity.y) steer.limit(self.max_force) return steer
def __update_flocking_behavior(self, scene_data): self.query_result = scene_data.kinetic_quad_tree.query( Rect(self.x - self.view_radius, self.y - self.view_radius, self.view_radius * 2, self.view_radius * 2)) seperation = self.__separation() alignment = self.__alignment() cohesion = self.__cohesion() seperation.multiply(3) alignment.multiply(1) cohesion.multiply(0.5) self.steer = Vector2(seperation.x + alignment.x + cohesion.x, seperation.y + alignment.y + cohesion.y)
def __init__(self, x, y): super(Boulder, self).__init__(x, y, 0, 0, 0) if randint(1, 10) % 2 == 0: self.sprite = Sprite(self.x, self.y, SpriteType.FALLING_ROCK_BIG) self.radius = 16 self.set_width(32) self.set_height(32) else: self.sprite = Sprite(self.x, self.y, SpriteType.FALLING_ROCK_SMALL) self.radius = 8 self.set_width(16) self.set_height(16) self.center = Vector2(self.x + self.radius, self.y + self.radius) self.default_gravity = 16 * randint(3, 6) self.gravity = 0 self.circle = Circle(self.center.x, self.center.y, self.radius, Color.GREEN, 2)
def __init__(self): self.scene_bounds = Rect( 0, 0, Camera.BOUNDS.width, Camera.BOUNDS.height ) self.camera = Camera() self.camera_location = Vector2(0, 0) self.camera_viewport = Rectangle( -Scene.VIEWPORT_BUFFER, -Scene.VIEWPORT_BUFFER, Camera.BOUNDS.width + Scene.VIEWPORT_BUFFER * 2, Camera.BOUNDS.height + Scene.VIEWPORT_BUFFER * 2, Color.RED, 2 ) self.entities = [] self.sprites = [] self.shapes = [] self.triggers = [] self.sprite_quad_tree = Quadtree(self.scene_bounds, 4) self.shape_quad_tree = Quadtree(self.scene_bounds, 4) self.entity_quad_tree = Quadtree(self.scene_bounds, 4) self.kinetic_quad_tree = Quadtree(self.scene_bounds, 4) self.entity_bin = Bin(self.scene_bounds, 4) self.query_result = None self.first_pass = True self.entities_are_uniform = False self.optimal_bin_size = 0 self.leave_transition_type = TransitionType.PINHOLE_CLOSE self.enter_transition_type = TransitionType.PINHOLE_OPEN self.manager = None self.actor = None self.scene_data = SceneDataRelay() self.scene_data.set_scene_bounds(self.scene_bounds)
def update(self, delta_time): super(Boss, self).update(delta_time) if self.actor.transitioning == True: self.actor.transitioning = False if self.boss.injured: if not self.glory_delay.started: self.glory_delay.start() self.glory_delay.update(delta_time) if self.glory_delay.done: self.closing_transition.update(delta_time) #self.manager.get_scene(SceneType.TITLE).relay_actor(self.actor) #self.manager.queue_next_scene(SceneType.TITLE) #self.glory_delay.reset() if self.closing_transition.done: self.manager.I_GIVE_UP() if self.actor.transitioning == True: self.actor.transitioning = False self.delay.update(delta_time) if not self.delay.done: self.actor.pause = True self.actor.velocity = Vector2(0, 0) self.actor.set_location(self.scene_bounds.width / 2 - self.actor.width / 2, -64) if self.boss.hurt: if self.queue_song: self.queue_song = False play_song("liocarcinus.wav") if self.boss.special_attack: self.boss.special_attack = False self.boss.crab_smash = False self.boss.sync_smash = 0 self.__create_boulders() if self.actor.grounded and self.actor.pause: self.actor.pause = False if isinstance(self.actor, Player) and self.actor.restart: self.start_transition = True if self.transition.first_half_complete: self.actor.revive() self.__restart_level() if self.start_transition: self.transition.update(delta_time) if self.transition.done: self.transition.reset() self.start_transition = False if self.actor.x + self.actor.width + 4 > self.scene_bounds.width: self.actor.set_location(self.scene_bounds.width - self.actor.width - 4, self.actor.y)
def set_width(self, width): self.width = width self.center = Vector2(self.x + self.width / 2, self.y + self.height / 2) self.bounds = Rect(self.x, self.y, self.width, self.height)
def set_height(self, height): self.height = height self.center = Vector2(self.x + self.width / 2, self.y + self.height / 2) self.bounds = Rect(self.x, self.y, self.width, self.height)
def __set_correct_exit(self, manager): from pygine.scenes import SceneType for t in manager.get_scene(self.next_scene).triggers: if t.next_scene == SceneType.VILLAGE: t.end_location = Vector2(self.x, self.y)
def update(self, top_left=Vector2(0, 0), world_bounds=pygame.Rect(0, 0, 0, 0)): Camera.scale = StaticCamera.scale self.stay_within_bounds(top_left, world_bounds)
def get_viewport_top_left(self): return Vector2( (Camera.top_left.x + StaticCamera.horizontal_letterbox) / Camera.scale, (Camera.top_left.y + StaticCamera.vertical_letterbox) / Camera.scale)
def _update_input(self, delta_time): if self.attacked: return if not self.pause and pressing( InputType.LEFT) and not pressing(InputType.RIGHT): self.velocity.x -= self.lateral_acceleration if self.velocity.x < -self.move_speed: self.velocity.x = -self.move_speed if not self.jumping: self.sprite.set_frame(self.walk_animation.current_frame, 6) self.direction = Direction.LEFT elif not self.pause and pressing( InputType.RIGHT) and not pressing(InputType.LEFT): self.velocity.x += self.lateral_acceleration if self.velocity.x > self.move_speed: self.velocity.x = self.move_speed if not self.jumping: self.sprite.set_frame(self.walk_animation.current_frame, 6) self.direction = Direction.RIGHT elif ((not pressing(InputType.LEFT) and not pressing(InputType.RIGHT)) or (pressing(InputType.LEFT) and pressing(InputType.RIGHT))): if self.grounded: self.velocity.lerp(Vector2(0, self.velocity.y), self.ground_friction) else: self.velocity.lerp(Vector2(0, self.velocity.y), self.air_friction) if self.velocity.x > -0.1 and self.velocity.x < 0.1: self.velocity.x = 0 self.sprite.set_frame(0, 6) if not self.grounded: if self.velocity.y < 0: self.sprite.set_frame(6, 6) if self.velocity.y > 0: self.sprite.set_frame(7, 6) else: if self.velocity.x == 0: if pressing(InputType.UP): self.sprite.set_frame(8, 6) if pressing(InputType.DOWN): self.sprite.set_frame(9, 6) if self.direction == Direction.LEFT: self.sprite.flip_horizontally(True) elif self.direction == Direction.RIGHT: self.sprite.flip_horizontally(False) if pressed(InputType.A) and self.grounded and not self.jumping: self.__jump(delta_time) self.jumping = True if self.jumping and self.velocity.y < -self.jump_initial_velocity / 2 and not pressing( InputType.A): self.velocity.y = -self.jump_initial_velocity / 2 self.jumping = False if pressed(InputType.X): self.attempt_block_shift = True
def set_location(self, x, y): super(Boulder, self).set_location(x, y) self.sprite.set_location(self.x, self.y) self.center = Vector2(self.x + self.radius, self.y + self.radius) self.circle.set_location(self.center.x, self.center.y)