def __init__(self, points, jump_through=False, waypoints=None, speed=1): self.waypoints = waypoints self.speed = speed self.target_index = 0 is_moving = bool(self.waypoints) # Create the body type if is_moving: self.body = pymunk.Body(pymunk.inf, pymunk.inf) else: self.body = SPACE.static_body for i in range(len(points)-1): # Make and configure the physics object seg = pymunk.Segment(self.body, points[i], points[i+1], 5) seg.friction = 1 seg.group = 1 if jump_through: seg.collision_type = JUMP_THROUGH_COLLISION_TYPE seg.color = (255, 255, 0, 255) if is_moving: seg.color = THECOLORS["blue"] seg.body.position = Vec2d(self.waypoints[0]) # Add it to the world SPACE.add(seg)
def __init__(self, points, jump_through=False, waypoints=None, speed=1): self.waypoints = waypoints self.speed = speed self.target_index = 0 is_moving = bool(self.waypoints) # Create the body type if is_moving: self.body = pymunk.Body(pymunk.inf, pymunk.inf) else: self.body = SPACE.static_body for i in range(len(points) - 1): # Make and configure the physics object seg = pymunk.Segment(self.body, points[i], points[i + 1], 5) seg.friction = 1 seg.group = 1 if jump_through: seg.collision_type = JUMP_THROUGH_COLLISION_TYPE seg.color = (255, 255, 0, 255) if is_moving: seg.color = THECOLORS["blue"] seg.body.position = Vec2d(self.waypoints[0]) # Add it to the world SPACE.add(seg)
def __init__(self, owner, gravity=False): self.radius = 3 self.speed = 500 self.ttl = 40 self.cooldown = 10 self.shot_sound = pygame.mixer.Sound("res/sfx/C_28P.ogg") self.shot_sound.play() facing_left = owner.current_facing == owner.left_key vel = Vec2d(-self.speed, 0) if facing_left else Vec2d(self.speed, 0) offset = Vec2d(0, -5) if facing_left else Vec2d(20, -5) self.body = pymunk.Body(1, pymunk.inf) self.body.position = owner.body.position + offset self.body.velocity = owner.body.velocity + vel self.body.bullet = weakref.proxy(self) if not gravity: self.body.velocity_func = _bullet_velocity_func self.shape = pymunk.Circle(self.body, self.radius, (0, 0)) self.shape.collision_type = BULLET_COLLISION_TYPE self.shape.friction = 0 SPACE.add(self.body, self.shape) self.active = True
def __init__(self, name, img, up=None, left=None, right=None, down=None, shoot=None): self.name = name # sprites img = pygame.image.load(img) flip_img = pygame.transform.flip(img, True, False) images = (img, flip_img) # animations self.idle_loop = Animation(images, 1, 8, loop=True) self.walk_loop = Animation(images, 3, 8, loop=True) self.jump_loop = Animation(images, 5, 2, start_frame=4, loop=True) self.spin_loop = Animation( images, 7, 4, loop=True, frame_rate=0.4) self.death_sequence = Animation(images, 8, 7) # sounds self.fall_sound = pygame.mixer.Sound("res/sfx/fall.wav") self.fall_sound.set_volume(0.5) self.jump_sound = pygame.mixer.Sound("res/sfx/jump.wav") self.jump_sound.set_volume(0.5) # keybindings self.jump_key = up self.left_key = left self.right_key = right self.down_key = down self.shoot_key = shoot # physics self.body = pymunk.Body(5, pymunk.inf) # mass, moment self.body.position = 100, 100 self.body.player = weakref.proxy(self) # TODO: heads should be a separate collision type self.head = pymunk.Circle(self.body, 14, (12, 7)) self.head.collision_type = PLAYER_COLLISION_TYPE self.head.friction = 0 self.feet = pymunk.Circle(self.body, 14, (12, -16)) self.feet.collision_type = 1 # TODO: Make this zero whilst falling self.feet.friction = 2 SPACE.add(self.body, self.head, self.feet) # Character stats self.max_jumps = 2 self.remaining_jumps = self.max_jumps self.speed = 200 # 100 self.jump_speed = 400 self.max_health = 10 self.health = self.max_health self.shot_cooldown = 0 # State tracking self.landed = False self.landed_hard = False self.ground_velocity = Vec2d.zero() self.ground_slope = 0 self.current_facing = self.right_key self.bullets = []
def __init__(self, name, img, up=None, left=None, right=None, down=None, shoot=None): self.name = name # sprites img = pygame.image.load(img) flip_img = pygame.transform.flip(img, True, False) images = (img, flip_img) # animations self.idle_loop = Animation(images, 1, 8, loop=True) self.walk_loop = Animation(images, 3, 8, loop=True) self.jump_loop = Animation(images, 5, 2, start_frame=4, loop=True) self.spin_loop = Animation(images, 7, 4, loop=True, frame_rate=0.4) self.death_sequence = Animation(images, 8, 7) # sounds self.fall_sound = pygame.mixer.Sound("res/sfx/fall.wav") self.fall_sound.set_volume(0.5) self.jump_sound = pygame.mixer.Sound("res/sfx/jump.wav") self.jump_sound.set_volume(0.5) # keybindings self.jump_key = up self.left_key = left self.right_key = right self.down_key = down self.shoot_key = shoot # physics self.body = pymunk.Body(5, pymunk.inf) # mass, moment self.body.position = Vec2d(100, 100) self.body.player = weakref.proxy(self) # TODO: heads should be a separate collision type self.head = pymunk.Circle(self.body, 14, (12, 7)) self.head.collision_type = PLAYER_COLLISION_TYPE self.head.friction = 0 self.feet = pymunk.Circle(self.body, 14, (12, -16)) self.feet.collision_type = 1 # TODO: Make this zero whilst falling self.feet.friction = 2 SPACE.add(self.body, self.head, self.feet) # Character stats self.max_jumps = 2 self.remaining_jumps = self.max_jumps self.speed = 200 self.jump_speed = 500 self.max_health = 10 self.health = self.max_health self.shot_cooldown = 0 # State tracking self.landed = False self.landed_hard = False self.ground_velocity = Vec2d.zero() self.ground_slope = 0 self.current_facing = self.right_key self.bullets = []