def __init__(self, grid_pos, color=(10, 200, 200), size=(20, 20), grid_move=False, mic_control=False, mic=None): # vars self.grid_pos = grid_pos self.grid_move = grid_move self.mic_control = mic_control self.mic = mic # MRO check super().__init__(np.array(grid_pos)*size, color, size) # input handler if self.mic_control: self.input_handler = InputMicHandler(self, mic=self.mic, grid_move=self.grid_move) else: self.input_handler = InputKeyHandler(self, grid_move=self.grid_move) # moveable init Moveable.__init__(self, move_sprite=self, move_rect=self.rect, move_speed=[3, 3], has_gravity=False, grid_move=self.grid_move) # interactions self.obstacle_sprites = pygame.sprite.Group() self.is_active = True # save init pos self.init_pos = self.position self.init_grid_pos = self.grid_pos.copy() # the grid self.move_wall_grid = None
def setUp(self): self.test_x = 0 self.test_y = 0 self.test_range = 3.0 self.mover_a = Moveable(x=self.test_x, y=self.test_y) self.mover_b = Moveable(x=self.test_x, y=self.test_y, max_dist=self.test_range)
def test_constructor(self): with self.assertRaises(TypeError): Moveable((1, 2)) with self.assertRaises(TypeError): Moveable(1, "asdf", max_dist=1.0) with self.assertRaises(TypeError): Moveable(1, 2, max_dist="asdf")
def test_move(self): x = 100 y = -100 self.mover_a.move(x, y) self.assertEqual(Moveable(x, y), self.mover_a) self.mover_b.move(x, y) self.assertEqual( self.mover_b.distance(Coord(self.test_x, self.test_y)), self.test_range) self.assertEqual( self.mover_b, Moveable(self.test_x + self.test_range * math.cos(-math.pi / 4), self.test_y + self.test_range * math.sin(-math.pi / 4), self.test_range)) pos = self.mover_b.copy() self.mover_b.move(self.test_x, self.test_y) self.assertEqual(self.mover_b.distance(pos), self.test_range) self.assertEqual( self.mover_b, Moveable( round( pos.x + self.test_range * math.cos(math.pi - math.pi / 4), 9), round( pos.y + self.test_range * math.sin(math.pi - math.pi / 4), 9), self.test_range)) x = -4365.23453 y = 12.23452 self.mover_a.move(x, y) self.assertEqual(Moveable(x, y), self.mover_a) mover_b_before = self.mover_b.copy() self.mover_b.move(x, y) self.assertEqual( self.mover_b.distance(Coord(self.test_x, self.test_y)), self.test_range) pos = self.mover_b.copy() self.mover_b.move(self.test_x, self.test_y) self.assertEqual(self.mover_b.distance(pos), self.test_range) self.assertEqual(self.mover_b, mover_b_before)
def test_in_interact_range(self): x = 1000 y = 100 pos = Coord(x, y) dist = self.entity.distance(pos) self.assertEqual(dist <= self.test_interact_range, self.entity.in_range(pos)) entity = Entity(0, 0, 1.0) self.assertFalse(entity.in_range(Moveable(1, 1))) self.assertTrue(entity.in_range(Entity(.5, .5, 1)))
def __init__(self, position, scale=(3, 3), has_gravity=True, grid_move=False): # arguments self.position = position self.scale = scale # character sprite self.character_sprite = CharacterSprite(self.position, self.scale) # moveable init Moveable.__init__(self, move_sprite=self.character_sprite, move_rect=self.character_sprite.rect, move_speed=[3, 3], has_gravity=has_gravity, grid_move=grid_move) # save init pos self.init_pos = position # input handler self.input_handler = InputKeyHandler(self, grid_move=grid_move) # interactions self.obstacle_sprites = pygame.sprite.Group() self.thing_sprites = pygame.sprite.Group() self.things_collected = 0 self.is_active = True
class MoveableTestCase(unittest.TestCase): def setUp(self): self.test_x = 0 self.test_y = 0 self.test_range = 3.0 self.mover_a = Moveable(x=self.test_x, y=self.test_y) self.mover_b = Moveable(x=self.test_x, y=self.test_y, max_dist=self.test_range) def tearDown(self): del self.mover_a del self.mover_b def test_constructor(self): with self.assertRaises(TypeError): Moveable((1, 2)) with self.assertRaises(TypeError): Moveable(1, "asdf", max_dist=1.0) with self.assertRaises(TypeError): Moveable(1, 2, max_dist="asdf") def test_range(self): self.assertEqual(self.test_range, self.mover_b.range) self.assertNotEqual(self.test_range - 1, self.mover_b.range) def test_position(self): self.assertEqual(self.test_x, self.mover_a.x) self.assertEqual(self.test_y, self.mover_a.y) def test_copy(self): copy_a = self.mover_a.copy() copy_b = self.mover_b.copy() self.assertEqual(copy_a, self.mover_a) self.assertNotEqual(copy_a, self.mover_b) self.assertEqual(copy_b, self.mover_b) self.assertNotEqual(copy_b, self.mover_a) def test_distance(self): copy = self.mover_a.copy() self.assertEqual(0, self.mover_a.distance(copy)) self.assertEqual(0, self.mover_a.distance(self.mover_a)) self.assertEqual(0, copy.distance(self.mover_a)) self.assertEqual(0, copy.distance(copy)) dist = 2**0.5 self.assertEqual(dist, Moveable(x=0, y=0).distance(Moveable(x=1, y=1))) self.assertEqual(dist, Moveable(x=0, y=0).distance(Moveable(x=1, y=-1))) self.assertEqual(dist, Moveable(x=0, y=0).distance(Moveable(x=-1, y=1))) self.assertEqual(dist, Moveable(x=0, y=0).distance(Moveable(x=-1, y=-1))) dist = 2 self.assertEqual(dist, Moveable(x=0, y=0).distance(Moveable(x=0, y=2))) self.assertEqual(dist, Moveable(x=0, y=0).distance(Moveable(x=0, y=-2))) self.assertEqual(dist, Moveable(x=0, y=0).distance(Moveable(x=2, y=0))) self.assertEqual(dist, Moveable(x=0, y=0).distance(Moveable(x=-2, y=0))) def test_string_representation(self): self.assertEqual("({}, {})".format(self.test_x, self.test_y), str(self.mover_a)) self.assertEqual( "({}, {}) - range: {}".format(self.test_x, self.test_y, self.test_range), str(self.mover_b)) def test_move(self): x = 100 y = -100 self.mover_a.move(x, y) self.assertEqual(Moveable(x, y), self.mover_a) self.mover_b.move(x, y) self.assertEqual( self.mover_b.distance(Coord(self.test_x, self.test_y)), self.test_range) self.assertEqual( self.mover_b, Moveable(self.test_x + self.test_range * math.cos(-math.pi / 4), self.test_y + self.test_range * math.sin(-math.pi / 4), self.test_range)) pos = self.mover_b.copy() self.mover_b.move(self.test_x, self.test_y) self.assertEqual(self.mover_b.distance(pos), self.test_range) self.assertEqual( self.mover_b, Moveable( round( pos.x + self.test_range * math.cos(math.pi - math.pi / 4), 9), round( pos.y + self.test_range * math.sin(math.pi - math.pi / 4), 9), self.test_range)) x = -4365.23453 y = 12.23452 self.mover_a.move(x, y) self.assertEqual(Moveable(x, y), self.mover_a) mover_b_before = self.mover_b.copy() self.mover_b.move(x, y) self.assertEqual( self.mover_b.distance(Coord(self.test_x, self.test_y)), self.test_range) pos = self.mover_b.copy() self.mover_b.move(self.test_x, self.test_y) self.assertEqual(self.mover_b.distance(pos), self.test_range) self.assertEqual(self.mover_b, mover_b_before)
def test_distance(self): copy = self.mover_a.copy() self.assertEqual(0, self.mover_a.distance(copy)) self.assertEqual(0, self.mover_a.distance(self.mover_a)) self.assertEqual(0, copy.distance(self.mover_a)) self.assertEqual(0, copy.distance(copy)) dist = 2**0.5 self.assertEqual(dist, Moveable(x=0, y=0).distance(Moveable(x=1, y=1))) self.assertEqual(dist, Moveable(x=0, y=0).distance(Moveable(x=1, y=-1))) self.assertEqual(dist, Moveable(x=0, y=0).distance(Moveable(x=-1, y=1))) self.assertEqual(dist, Moveable(x=0, y=0).distance(Moveable(x=-1, y=-1))) dist = 2 self.assertEqual(dist, Moveable(x=0, y=0).distance(Moveable(x=0, y=2))) self.assertEqual(dist, Moveable(x=0, y=0).distance(Moveable(x=0, y=-2))) self.assertEqual(dist, Moveable(x=0, y=0).distance(Moveable(x=2, y=0))) self.assertEqual(dist, Moveable(x=0, y=0).distance(Moveable(x=-2, y=0)))