Exemplo n.º 1
0
 def test_is_point_in_body_method(self):
     self.assertTrue(self.triangles[0].is_point_in_body(
         self.triangles[0].position))
     self.assertFalse(self.triangles[0].is_point_in_body(Vector(100, 100)))
     self.assertTrue(self.triangles[1].is_point_in_body(
         self.triangles[1].position))
     self.assertFalse(self.triangles[1].is_point_in_body(Vector(100, 100)))
Exemplo n.º 2
0
def test_boundary():

    thing = Boxed(Rect(0, 0, 40, 40))

    # Near the west wall

    thing.position = Vector(-5, 20)
    near = thing.nearest_boundary()
    assert near[0] == -5
    assert near[1] == COMPASS_WEST

    # Near the east wall
    thing.position = Vector(45, 20)
    near = thing.nearest_boundary()
    assert near[0] == -5
    assert near[1] == COMPASS_EAST

    # Near the north wall
    thing.position = Vector(20, -5)
    near = thing.nearest_boundary()
    assert near[0] == -5
    assert near[1] == COMPASS_NORTH

    # Near the south wall
    thing.position = Vector(20, 45)
    near = thing.nearest_boundary()
    assert near[0] == -5
    assert near[1] == COMPASS_SOUTH
 def test_rope_calculation(self):
     self.graple.rect.center = Vector(1, 0)
     self.graple.aim = Vector(0, 1)
     self.graple.calculate_rope()
     self.assertAlmostEqual(self.graple.distance, 1.41, 2)
     self.assertEqual(self.graple.rope,
                      Vector(-0.7071067811865475, 0.7071067811865475))
Exemplo n.º 4
0
 def test_collide_circle(self):
     result = self.circles[0].collide_circle(self.circles[1])
     self.assertFalse(result[0])
     result = self.circles[0].collide_circle(self.circles[2])
     self.assertTrue(result[0])
     self.assertEqual(Vector(round(result[1].x), round(result[1].y)),
                      Vector(-8, -8))
Exemplo n.º 5
0
 def reset(self):
     self.pos = Vector(SPAWNPOINT)
     self.vel = Vector()
     self.acc = Vector()
     self.fitness = 0
     self.completed, self.crashed = False, False
     self.count = 0
Exemplo n.º 6
0
 def __init__(self, position, value, values, width, height, puck_radius,
              text, text_colour, text_font, text_box_width, text_box_height,
              slider_type="default"):
     self.__value = value
     self.values = values
     self.timer = 0
     position = position + Vector(0, text_box_height / 2)
     Rectangle.__init__(self, width, height, position)
     self.puck = Circle(puck_radius, position)
     self.states = {}
     for state in ["active", "hover", "normal"]:
         self.puck.load_avatar(
             r"GUI/Slider/{0}/{1}.png".format(slider_type, state))
         self.puck.scale_avatar(
             self.puck.radius * 2, self.puck.radius * 2)
         self.states[state] = self.puck.image_master
     self.sound_effect = SoundEffect(r"slider_{0}.wav".format(slider_type))
     self.state = "normal"
     self.text_box = TextBox(
         text_box_width, text_box_height, position +
         Vector(0, -max(self.puck.radius, height / 2) -
                text_box_height / 2), text,
         text_colour, text_font)
     self.left_fill = (253, 238, 0)
     self.right_fill = (0, 0, 0)
     self.slider_type = slider_type
     self.sync_puck()
Exemplo n.º 7
0
 def test_circle_with_rectangle_not_colliding(self):
     rectangle = Rectangle(10, 10, Vector(0, 0), 1)
     circle = Circle(20, Vector(220, 20))
     circle_rectangle_collision = circle.check_if_collide(rectangle)
     rectangle_circle_collision = rectangle.check_if_collide(circle)
     self.assertFalse(circle_rectangle_collision[0])
     self.assertFalse(rectangle_circle_collision[0])
Exemplo n.º 8
0
 def calculate_vertices(self):
     self.vertices = {
         "A": Vector((0, 0)), "B": Vector((self.edge_lenghts["AB"], 0))}
     x_c = (self.edge_lenghts["AB"] ** 2 + self.edge_lenghts["AC"] ** 2 -
            self.edge_lenghts["BC"] ** 2) / (2 * self.edge_lenghts["AB"])
     y_c = (self.edge_lenghts["AC"] ** 2 - x_c ** 2) ** 0.5
     self.vertices["C"] = Vector((x_c, y_c))
Exemplo n.º 9
0
    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
Exemplo n.º 10
0
    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)
Exemplo n.º 11
0
 def test_collides_rectangle(self):
     self.assertTrue(
         self.rectangle.collides_rectangle(
             Rectangle(10, 10, Vector(5, 0), 1)))
     self.assertFalse(
         self.rectangle.collides_rectangle(
             Rectangle(10, 10, Vector(100, 0), 1)))
Exemplo n.º 12
0
    def draw(self):
        self.field_image.fill((0, 0, 0))
        pygame.draw.lines(self.field_image, (255,255,255),True,\
                          [self.field_pos,\
                           self.field_pos + Vector(self.field_size.x, 0),\
                           self.field_pos + self.field_size,\
                           self.field_pos + Vector(0, self.field_size.y)])
        self.grid_group.draw(self.field_image)


        pygame.draw.lines(self.next_box_image, (255,255,255), True,\
                          [Vector(0,0),\
                           Vector(self.next_box_size.x, 0),\
                           self.next_box_size,\
                           Vector(0, self.next_box_size.y)])
        self.next_grid_group.draw(self.next_box_image)

        self.image.blit(self.field_image, self.field_pos)
        self.image.blit(self.next_box_image, self.next_box_pos)

        text = self.score_text_font.render(self.score_text, True,
                                           (255, 255, 255), (0, 0, 0))
        text_rect = text.get_rect()
        text_rect.center = self.score_text_pos
        self.image.blit(text, text_rect)
Exemplo n.º 13
0
 def test_triangle_with_rectangle_not_colliding(self):
     rectangle = Rectangle(10, 10, Vector(200, 0), 1)
     triangle = Triangle([Vector(
         0, 0), Vector(1, 0), Vector(0, 1)], Vector(0, 0), 1)
     triangle_rectangle_collision = triangle.check_if_collide(rectangle)
     rectangle_triangle_collision = rectangle.check_if_collide(triangle)
     self.assertFalse(triangle_rectangle_collision[0])
     self.assertFalse(rectangle_triangle_collision[0])
Exemplo n.º 14
0
 def create_joint(self, joint):
     self.body_parts[self.joint_placement[joint][2]].pivot = Vector(
         self.joint_placement[joint][3]) / self.proportions[1]
     return RevoluteJoint(
         self.body_parts[self.joint_placement[joint][0]],
         Vector(self.joint_placement[joint][1]) / self.proportions[1],
         self.body_parts[self.joint_placement[joint][2]],
         Vector(self.joint_placement[joint][3]) / self.proportions[1])
Exemplo n.º 15
0
 def test_if_move_works_properly(self):
     expected_vertices = [round_vector(_ + Vector(5, 5))
                          for _ in self.text_box.vertices]
     self.text_box.move(Vector(5, 5))
     actual_vertices = [round_vector(_)
                        for _ in self.text_box.vertices]
     self.assertTrue(all([first == second for first, second in zip(
         expected_vertices, actual_vertices)]))
Exemplo n.º 16
0
 def test_circle_with_rectangle_collision(self):
     rectangle = Rectangle(10, 10, Vector(0, 0), 1)
     circle = Circle(20, Vector(20, 20))
     circle_rectangle_collision = circle.check_if_collide(rectangle)
     rectangle_circle_collision = rectangle.check_if_collide(circle)
     self.assertTrue(circle_rectangle_collision[0])
     self.assertTrue(rectangle_circle_collision[0])
     self.assertTupleEqual(tuple(circle_rectangle_collision[1]), (0, -5))
     self.assertTupleEqual(tuple(rectangle_circle_collision[1]), (0, 5))
Exemplo n.º 17
0
 def test_calculate_point_of_intersection(self):
     self.assertEqual(
         self.line.calculate_point_of_intersection(self.segment), None)
     self.assertEqual(round_vector(
         self.segment.calculate_point_of_intersection(Line(
             Vector(0.01, 0), Vector(1, 1)))), Vector(0, 0))
     self.assertEqual(
         self.segment.calculate_point_of_intersection(Line(
             Vector(-0.1, 0), Vector(1, 1))), None)
Exemplo n.º 18
0
 def test_sync_position(self):
     vertices = {
         'A': Vector(-0.3, -0.3),
         'B': Vector(0.7, -0.3),
         'C': Vector(-0.3, 0.7)
     }
     self.triangles[0].sync_position()
     for key, vertex in self.triangles[0].vertices.items():
         self.assertEqual(Vector(round(vertex.x, 1), round(vertex.y, 1)),
                          vertices[key])
Exemplo n.º 19
0
 def update_head_graphics(self):
     head_rotation = self.body[1] - self.body[0]
     if head_rotation == Vector(1, 0):
         self.head = self.headLeft
     elif head_rotation == Vector(-1, 0):
         self.head = self.headRight
     elif head_rotation == Vector(0, 1):
         self.head = self.headUp
     elif head_rotation == Vector(0, -1):
         self.head = self.headDown
Exemplo n.º 20
0
    def test_movement(self):
        self.batarang.direction = Vector(0, 0)
        self.batarang.move(17)
        self.assertEqual(self.batarang.x, 20.0)
        self.assertEqual(self.batarang.y, 10.0)

        self.batarang.direction = Vector(1, 0)
        self.batarang.move(17)
        self.assertAlmostEqual(self.batarang.x, 28.50, 2)
        self.assertEqual(self.batarang.y, 10.0)
Exemplo n.º 21
0
 def update_tail_graphics(self):
     tail_rotation = self.body[-2] - self.body[-1]
     if tail_rotation == Vector(1, 0):
         self.tail = self.tailLeft
     elif tail_rotation == Vector(-1, 0):
         self.tail = self.tailRight
     elif tail_rotation == Vector(0, 1):
         self.tail = self.tailUp
     elif tail_rotation == Vector(0, -1):
         self.tail = self.tailDown
Exemplo n.º 22
0
 def test_angle_calculation(self):
     self.graple.rope = Vector(0, -1)
     self.graple.calculate_angle()
     self.assertEqual(self.graple.angle, 90)
     self.graple.rope = Vector(0, 1)
     self.graple.calculate_angle()
     self.assertEqual(self.graple.angle, -90)
     self.graple.rope = Vector(1, 0)
     self.graple.calculate_angle()
     self.assertEqual(self.graple.angle, 0)
Exemplo n.º 23
0
    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()
Exemplo n.º 24
0
 def test_calculate_altitude_foot(self):
     self.assertEqual(round_vector(Line(
         Vector(3, 3), Vector(3, 8)).calculate_altitude_foot(Vector(2, 2))),
         Vector(3, 2))
     self.assertEqual(
         round_vector(self.line.calculate_altitude_foot(Vector(2, 2))),
         Vector(2, 0))
     self.assertEqual(round_vector(Line(
         Vector(0, 0), Vector(1, 1)).calculate_altitude_foot(Vector(2, 2))),
         Vector(2, 2))
Exemplo n.º 25
0
 def is_point_in_body(self, point_location_m, camera=0):
     if camera != 0:
         centroid_to_point = point_location_m - \
             camera.apply(Vector(self.position))
     else:
         centroid_to_point = point_location_m - Vector(self.position)
     centroid_to_point = centroid_to_point.rotate(
         self.direction.angle_to(Vector((1, 0))))
     return abs(centroid_to_point.x) <= self.width / 2 and \
         abs(centroid_to_point.y) <= self.height / 2
Exemplo n.º 26
0
 def draw(self):
     colors = BlockSprite.color_maps[self.type]
     offset = Vector(self.rect[2],
                     self.rect[3]) * BlockSprite.top_bottom_ratio
     size = Vector(self.rect[2], self.rect[3])
     in_upleft = offset
     in_upright = Vector(size.x - offset.x, offset.y)
     in_downleft = Vector(offset.x, size.y - offset.y)
     in_downright = Vector(size.x - offset.x, size.y - offset.y)
     up_left = Vector(0, 0)
     up_right = Vector(size.x, 0)
     down_left = Vector(0, size.y)
     down_right = Vector(size.x, size.y)
     self.image.fill(colors["normal"])
     pygame.draw.polygon(
         self.image, colors["light"],
         [up_left, in_upleft, in_upright, up_right, up_left])
     pygame.draw.polygon(
         self.image, colors["dark"],
         [down_left, in_downleft, in_downright, down_right, down_left])
     pygame.draw.polygon(
         self.image, colors["side"],
         [down_left, up_left, in_upleft, in_downleft, down_left])
     pygame.draw.polygon(
         self.image, colors["side"],
         [down_right, up_right, in_upright, in_downright, down_right])
     pygame.draw.line(self.image, (0, 0, 0), up_left, in_upleft)
     pygame.draw.line(self.image, (0, 0, 0), down_left, in_downleft)
     pygame.draw.line(self.image, (0, 0, 0), up_right, in_upright)
     pygame.draw.line(self.image, (0, 0, 0), down_right, in_downright)
Exemplo n.º 27
0
 def test_retraction(self):
     self.graple.distance = 200
     self.graple.rope = Vector(1, 0)
     self.graple.retract(17)
     self.assertEqual(self.graple.rect.center, Vector(11.9, 0.0))
     self.graple.distance = 200
     self.graple.rope = Vector(0, 1)
     self.graple.retract(17)
     self.assertEqual(self.graple.rect.center, Vector(11.9, 11.9))
     self.assertEqual(self.graple.x, -28.1)
     self.assertEqual(self.graple.y, -8.1)
Exemplo n.º 28
0
 def test_value_getter(self):
     self.checkbox.update_state(
         self.checkbox.position + Vector(1000, 0), False)
     self.assertTrue(self.checkbox.value)
     self.checkbox.update_state(self.checkbox.position, None)
     self.assertTrue(self.checkbox.value)
     self.checkbox.update_state(self.checkbox.position, True)
     self.assertFalse(self.checkbox.value)
     self.checkbox.update_state(
         self.checkbox.position + Vector(1000, 0), None)
     self.assertFalse(self.checkbox.value)
Exemplo n.º 29
0
    def __init__(self, width, height, balls):
        self.balls = balls
        self.walls = []
        self.walls.append(Wall(Vector(400, 300), Vector(100, 100), 0))

        self.width = width
        self.height = height

        self.background = Background((width, height))

        self.user_actions = []
Exemplo n.º 30
0
    def __init__(self, pos, size, angle):
        self.color = Color(120, 89, 94)

        points = [
            pos,
            Vector(pos.x + size.x, pos.y),
            Vector(pos.x + size.x, pos.y + size.y),
            Vector(pos.x, pos.y + size.y)
        ]
        self.points = points
        self.points = rotate_rectangle(self.points, angle)