def update(self, timer, world, events): self.functionality(events, world) way_point = Vector(self.aim) - Vector(self.rect.center) bearing = way_point.normalize() self.image = pygame.transform.rotate(self.image_master, way_point.angle_to(Vector(1, 0))) self.rect = Rectangle.get_rect(self.image, self.rect.center) if not self.should_retract: self.hook_image = pygame.transform.rotate( self.hook_image_master, way_point.angle_to(Vector(1, 0))) self.hook_rect = Rectangle.get_rect(self.hook_image, self.hook_rect.center) self.hitmask = get_hitmask(self.hook_rect, self.hook_image, 0) self.x = self.rect.x self.y = self.rect.y if self.shooter: self.shoot(timer) self.should_retract = True self.should_aim = False elif not self.should_retract: self.hook_rect = Rectangle.get_rect( self.hook_image, (self.rect.center[0] + bearing.x * self.displacement, self.rect.center[1] + bearing.y * self.displacement)) if self.should_retract and not self.shooter: self.retract(timer) if self.should_release: self.release(timer)
def update(self, dt): """Destroy scene bricks when hit them.""" hit = False if Game.current_scene.testCollision(self.rect): Game.current_scene.damage(self.rect, Vector2.normalize(self.vel)) hit = True for obj in Game.all_objects: if (hasattr(obj, "testCollision") and self != obj and self.parent != obj and type(self.parent) != type(obj) and obj.testCollision(self.rect) and obj.is_active is True): if (type(self.parent) == PlayerTank): self.parent.kills += 1 Game.all_objects.remove(obj) elif type(obj) == PlayerTank: obj.hp -= 50 if obj.hp <= 0: Game.all_objects.remove(obj) main_menu_event = pygame.event.Event( pygame.USEREVENT, user_type='MAINMENU') pygame.event.post(main_menu_event) hit = True if hit: Game.all_objects.remove(self) super().update(dt)
def __init__(self, direction: Vector2, start: Vector2, speed=1.0): super().__init__() self.direction = direction.normalize() self.direction.y *= -1 # Y-axis is inverted... + is down, - is up self.start = start self.bday = pygame.time.get_ticks() self.speed = speed self.image = pygame.transform.rotate( Potato.POTATO, math.atan2(direction.y, direction.x)) self.rect = self.image.get_rect(center=self.start)
def __init__(self, pos: tuple[int, int], size: tuple[int, int], color: tuple[int, int, int], direction: Vector2, speed: float, damage: int) -> None: super(Projectile, self).__init__(pos, size, color, centered=True) self.__pos: Vector2 = Vector2(pos) self.__direction: Vector2 = direction.normalize() self.__velocity: Vector2 = self.__direction * speed self.__alive: bool = True self.__damage: int = damage
def circlecast_collision(self, pos: Vector2, direction: Vector2, range: float, radius: float, screen: pygame.display = None): destination = direction.normalize() * range length = destination.length() dx = destination.x / length dy = destination.y / length for i in range(0, round(length)): rx = round(dx * i) ry = round(dy * i) info = self.circle_collision(Vector2(rx, ry), radius, screen) if info != None: return info return None
def update(self, player_list, base_list): self.check_collide_with_player(player_list[self.owner_index]) self.check_collide_with_base(base_list[self.owner_index]) if self.status == 0: self.direction = Vec(0, 0) self.cd -= 1 if self.cd == 0: self.change_status(1) else: target = (player_list[self.owner_index].position if self.status == 1 \ else base_list[self.owner_index].center) self.direction = Vec.normalize(target - self.position) self.position += self.direction * self.speed
def update(self, oils, bases, players, ev_manager): if self.item is not None and self.item.active: self.item.update(ev_manager) self.update_speed() if self.magnet_attract: for oil in oils: if Vec.magnitude(oil.position - self.position) <= oil.radius + model_const.magnet_attract_radius: oil.update_position(Vec.normalize(self.position - oil.position) * model_const.magnet_attract_speed) if not self.freeze and not self.theworld: new_x = self.position[0] + self.direction[0] * self.speed new_y = self.position[1] + self.direction[1] * self.speed if new_x < self.radius or new_x > view_const.game_size[0] - self.radius: self.direction[0] = 0 if new_y < self.radius or new_y > view_const.game_size[1] - self.radius: self.direction[1] = 0 self.position += Vec(self.direction) * self.speed self.pick_oil(oils, ev_manager) self.store_price(bases, ev_manager)
def move(self, direction: Vector2): try: self._speed = direction.normalize() * self.velocity * self.level.tile_size except ValueError: self._speed = Vector2(0, 0)
class SawBlock(): def __init__(self, x, y, length): """ x and y should be the coordinates of the pivot """ self.rope_height = length self.x, self.y = x, y self.rope_width = SAW_ROPE_WIDTH self.saw_image_master = image.load( "../ArtWork/Environment/{0}".format(SAW_IMAGE)).convert_alpha() self.saw_image_master = transform.scale(self.saw_image_master, SAW_DIMENSION) self.image = self.saw_image_master self.center_old = Vector(x, y + self.rope_height + 15) self.rect = Rectangle.get_rect(self.image, self.center_old) self.collision_circle = Circle(25, self.center_old) self.step = ROTATION_STEP self.rotation = 0 self.time = 0 self.last_time = 0 self.current_time = 0 self.direction = Vector((0, 0)) self.velocity = Vector((0, 0)) self.bob = Pendulum(SAW_ROPE_ANGLE, self.rope_height, (self.x, self.y)) self.is_severed = False def rotate_saw(self, time): self.image = transform.rotate(self.saw_image_master, self.rotation) self.rect = Rectangle.get_rect(self.image, self.center_old) self.rotation += 300 * time / 1000 if self.rotation > 360: self.rotation = self.step def swing_rope(self): self.current_time = time.get_ticks() if self.current_time - self.last_time >= TICKS_FOR_60_FPS: self.bob.recompute_angle() self.center_old = self.bob.rect.center self.rect.center = self.center_old self.collision_circle.move(self.center_old - self.collision_circle.position) self.last_time = self.current_time def deploy(self): self.is_severed = True self.direction = Vector((self.x, self.y)) - Vector(self.center_old) self.direction = self.direction.normalize() # pseudo velocity vector - defines only direction not speed self.velocity = self.direction.rotate(-90 * self.sign(self.bob.d_theta)) self.velocity = self.velocity.normalize() def sign(self, number): if number >= 0: return 1 else: return -1 def update(self, time): if self.is_severed: self.time += 0.5 self.rect.advance( (self.velocity.x * 10 + math.sin(math.fabs(self.bob.theta))), (self.velocity.y * 8 * Vector(0, 1).x * 10 + self.time)) self.collision_circle.move(self.rect.center - self.collision_circle.position) self.center_old = self.rect.center else: self.rotate_saw(time) self.swing_rope() def draw(self, surface, camera): if self.is_severed: surface.blit(self.image, camera.apply((self.rect.x, self.rect.y))) else: surface.blit(self.image, camera.apply((self.rect.x, self.rect.y))) draw.line(surface, (0, 0, 0), camera.apply((self.x, self.y)), camera.apply((self.rect.center[0], self.rect.center[1])), self.rope_width)
class Batarang(): def __init__(self, x, y, world): """x and y are the coordinates of player's hand, world are all objects batarang can collide with """ self.width = BATARANG_WIDTH self.height = BATARANG_HEIGHT self.world = world.level_blocks self.saws = world.level_saws self.x, self.y = x, y self.triangle = Triangle([Vector(self.x, self.y), Vector(self.x + self.width, self.y), Vector(self.x + self.width // 2, self.y + self.height)], (self.x, self.y)) self.triangle.load_avatar("/Environment/{0}".format(BATARANG_IMAGE)) self.triangle.scale_avatar(self.width, self.height) self.direction = Vector(0, 0) self.rotation = 0 self.step = ROTATION_STEP self.speed = 0 self.last_update = 0 self.mouse_position = 0 self.should_fly = False def rotate(self, timer): self.triangle.rotate(self.rotation * (1000 / timer)) self.triangle.move(Vector((self.x, self.y)) - self.triangle.position) self.rotation += self.step if self.rotation > 360: self.rotation = self.step def move(self, timer): self.speed = 50 * 10 * (timer / 1000) self.x += self.direction.x * self.speed self.y += self.direction.y * self.speed self.triangle.move(self.direction * self.speed) def get_next_position(self): return self.x + self.direction.x * self.speed, \ self.y + self.direction.y * self.speed def direct(self, mouse_x, mouse_y): self.direction = Vector(mouse_x - self.x, mouse_y - self.y) self.direction = self.direction.normalize() def take_action(self, camera): self.last_update = time.get_ticks() self.mouse_position = camera.reverse_apply(mouse.get_pos()) self.direct(self.mouse_position[0], self.mouse_position[1]) self.should_fly = True def update(self): timer = time.get_ticks() - self.last_update + 1 self.last_update += timer if self.should_fly: self.move(timer) self.rotate(timer) self.collides(self.world) return self.should_fly def draw(self, surface, camera=0): if camera != 0: self.triangle.display_avatar(surface, camera) else: return def collide_saw(self, saw): rope = Line(Point(saw.x, saw.y), Point(saw.collision_circle.position.x, saw.collision_circle.position.y)) next_position = self.get_next_position() path = Line(Point(self.x, self.y), Point(next_position[0], next_position[1])) intersection = Line.get_intersection(rope, path) return bool(intersection) def collides(self, world): for obstacle in world: if self.triangle.check_if_collide(obstacle.rect)[0]: self.should_fly = False break for saw in self.saws: if not saw.is_severed and self.collide_saw(saw): saw.deploy() def reposition(self, coordinates, orientation): self.x, self.y = coordinates self.triangle.move(Vector(coordinates) - self.triangle.position) self.triangle.direction = orientation.normalize()