def __init__(self, start, end, arrow_size=10, stroke_color=(0,0,0), stroke_width=1, stroke_opacity=1): SvgCollection.__init__(self) self.type = "arrow" self.add(Line(start, end, stroke_color, stroke_width, stroke_opacity)) arrow_direction = util.direction(start, end) d1 = util.scale_vector(util.rotate_2D_vector(arrow_direction, math.pi * 5/6), arrow_size) d2 = util.scale_vector(util.rotate_2D_vector(arrow_direction, math.pi * -5/6), arrow_size) e1 = util.add_vector(end, d1) e2 = util.add_vector(end, d2) self.add(Line(end, e1, stroke_color, stroke_width, stroke_opacity)) self.add(Line(end, e2, stroke_color, stroke_width, stroke_opacity))
def spawn_engine_particles(self, destination_list): if self.engine_particles_to_spawn <= 0: return location_offset = util.scale_vector(self.direction_vector, -3) location = util.add_vector(self.get_rect().center, location_offset) vel_offset = util.scale_vector(self.direction_vector, self.particle_direction) particle_velocity = util.add_vector(self.get_velocity(), vel_offset) particle.spawn_particles(destination_list, count=self.engine_particles_to_spawn, location=location, velocity=particle_velocity, color=dark_red, fade_to_color=light_yellow) self.engine_particles_to_spawn = 0
def _fade_get_next_color(self, delta_time): delta_color = util.scale_vector(self.amount_to_fade_per_milli, delta_time) delta_color = util.vector_op(delta_color, self.buffered_fade, add) delta_color, self.buffered_fade = util.separate_whole_decimal( delta_color) return util.vector_op(self.current_color, delta_color, sub)
def fire_laser(self, delta_time): if self.last_fire >= self.fire_rate: # print("laser fired! " + str(delta_time)) self.last_fire = 0 location = self.get_rect().center offset = util.scale_vector(self.direction_vector, 5) final_location = util.add_vector(offset, location) laser = lasermod.Laser(final_location, self.direction_vector, self.get_velocity()) self.lasers.append(laser)
def fade(self, fade_duration, begin_color, end_color, draw_image_func=fill_surface): self.current_color = begin_color self.target_color = end_color self.fade_duration = fade_duration dif_vec = util.vector_op(begin_color, end_color, sub) self.amount_to_fade_per_milli = util.scale_vector( dif_vec, 1 / fade_duration) self.draw_image_func = draw_image_func self._update_func = self._fade
def init_image(hp): # todo make the drawing cooler. direction_of_draw = pygame.math.Vector2((0, -1)) total_points = hp + 2 degrees_per_point = 360 / total_points diameter = diameter_from_hp(hp) radius = int(diameter / 2) surface = pygame.Surface((diameter, diameter), pygame.SRCALPHA) center_point = surface.get_rect().center points = [] previous_scalar = radius for _ in range(total_points): point_scalar = max(min(util.gauss(previous_scalar + 1, 3), radius), 1) previous_scalar = point_scalar point = util.scale_vector(direction_of_draw, point_scalar) point = util.add_vector(point, center_point) points.append(point) direction_of_draw = direction_of_draw.rotate(degrees_per_point) pygame.draw.polygon(surface, asteroid_color, points) return surface
def _apply_velocity(self, delta_time): to_move = util.scale_vector(self.velocity_vector, delta_time) to_move = util.vector_op(to_move, self.buffered_move, add) to_move, self.buffered_move = util.separate_whole_decimal(to_move) self.move(to_move[0], to_move[1])
def update_weight(self, weight, x_i, y_i, r_t, c): base = scale_vector(weight, 1-r_t) penalty = [0 for _ in base] if y_i*(dot_prod(weight, x_i)) <= 1: penalty = scale_vector(x_i, r_t*c*y_i) return verctor_add(penalty, base)