def new_ball(self): if (not self.current_sequence_next or len(self.current_sequence_next) == 0): return if self.current_time_offset < self.get_current_time(): if not self.current_sequence_started: ball = Ball( self.ellipse.next_point(self.ellipse.start_point, self.radius), self.colors[self.current_sequence_next[-1]]) sequence = Sequence(ball, self.ellipse, self.radius) self.sequences.append(sequence) self.current_sequence_next.pop() self.current_sequence_started = True self.add_color(ball.color) return if (math_ext.get_angle( self.sequences[-1].left) > math_ext.get_angle( self.ellipse.next_point(self.ellipse.start_point, self.radius)) or len(self.sequences[-1].balls) == 0): color = self.colors[self.current_sequence_next[-1]] self.add_color(color) self.sequences[-1].add_ball(color) self.current_sequence_next.pop() return
def previous_point(self, point, distance, start=None): if not start: start = self.start angle = math.pi - get_angle(point) rev_point = self.get_coordinates(angle) rev_next_point = self.next_point( rev_point, distance, finish=math.pi - start) if not rev_next_point: return None previous_point = self.get_coordinates( mathExt.normalise_angle(math.pi - get_angle(rev_next_point))) return previous_point
def process_hit(self, shot): cond = False intersection = self.ellipse.get_coordinates(shot.angle, self.turret) intersection_angle = math_ext.get_angle(intersection) if len(self.sequences) == 0: return for sequence in self.sequences: if (math_ext.get_angle(sequence.left) <= intersection_angle <= math_ext.get_angle(sequence.right)): if shot.penetrate: sequence.get_penetrated(intersection) else: if self.sound_unit is not None: self.sound_unit.ball_insertion_sound() sequence.insert_ball(intersection, shot.color) cond = True return cond
def test_get_biased_coordinates(self): random.seed = 100 for repeat in range(100000): start, finish, width, height, ellipse = self.get_random_ellipse() angle = random.random() % math.pi height = random.random() % ellipse.height point = ellipse.get_coordinates(angle, (0, height)) self.assertIn(point, ellipse) self.assertAlmostEqual( math_ext.get_angle((point[0], point[1] - height)), angle)
def add_ball(self, color): if len(self.balls) == 0: point = self.ellipse.next_point(self.ellipse.start_point, self.radius) else: if mathExt.get_angle(self.left) < self.ellipse.start: raise ValueError point = self.left self.balls.append(Ball(point, color)) self._left = None
def test_update(self): random.seed = 100 for repeat in range(100000): x = random.random() y = random.random() speed = random.random() % 100 angle = random.random() % (2 * math.pi) shot = Shot(x, y, None, angle, speed) shot.update() x1 = shot.x y1 = shot.y self.assertAlmostEqual(math_ext.get_angle((x1 - x, y1 - y)), angle) self.assertAlmostEqual(math_ext.get_distance((x1, y1), (x, y)), speed)
def next_point(self, point, distance, finish=None): if not finish: finish = self.finish start = get_angle(point) middle = tern_search(start, finish, lambda x: get_distance( self.get_coordinates(x), point)) angle1 = bin_search(start, middle, lambda x: get_distance( self.get_coordinates(x), point), distance) angle2 = bin_search(middle, finish, lambda x: -get_distance( self.get_coordinates(x), point), distance) dist1 = get_distance(self.get_coordinates(angle1), point) dist2 = get_distance(self.get_coordinates(angle2), point) if abs(dist1 - distance) < 1e-6: return self.get_coordinates(angle1) if abs(dist2 - distance) < 1e-6: return self.get_coordinates(angle2) return None
def check_point_finished(self, point, distance): point_angle = get_angle(point) return ( self.finish < point_angle or get_distance(point, self.finish_point) < distance )
def check_point_pre_started(self, point, distance): point_angle = get_angle(point) return ( point_angle < self.start or get_distance(point, self.start_point) < distance )