def test_should_return_none_when_calling_get_by_name(self): # given simple_scene_manager = SimpleSceneManager() test_sprite_1 = XPGESprite(simple_scene_manager) test_sprite_1.name = "test_name_2" test_sprite_2 = XPGESprite(simple_scene_manager) test_sprite_3 = XPGESprite(simple_scene_manager) sprite_list = [test_sprite_1, test_sprite_2, test_sprite_3] simple_scene_manager._sprites = sprite_list # when result = simple_scene_manager.get_by_name("test_name") # then self.assertIsNone(result)
def test_should_load_scene(self): # given simple_scene_manager = SimpleSceneManager() simple_scene_manager._sprites = MagicMock() test_sprite = XPGESprite(simple_scene_manager) component_mock = Mock(spec=SpriteBehaviour) test_sprite.components.append(component_mock) sprite_list = [test_sprite] simple_scene_manager._sprites.__iter__ = Mock( return_value=iter(sprite_list)) test_scene_name = "test name" class TestSimpleScene(SimpleScene): def __init__(self, scene_manager): super().__init__(scene_manager) self.sprites = sprite_list simple_scene_manager.register_scene(TestSimpleScene, test_scene_name) # when simple_scene_manager.load_scene(test_scene_name) # then self.assertIsInstance(simple_scene_manager._current_scene, TestSimpleScene) simple_scene_manager._sprites.clear.assert_called_once() simple_scene_manager._sprites.append.assert_called_once_with( test_sprite) component_mock.on_scene_loaded.assert_called_once()
def test_should_get_sprite_by_name(self): # given simple_scene_manager = SimpleSceneManager() test_sprite_1 = XPGESprite(simple_scene_manager) test_sprite_1.name = "test_name" test_sprite_2 = XPGESprite(simple_scene_manager) test_sprite_3 = XPGESprite(simple_scene_manager) sprite_list = [test_sprite_1, test_sprite_2, test_sprite_3] simple_scene_manager._sprites = sprite_list # when result = simple_scene_manager.get_by_name("test_name") # then self.assertIsNotNone(result) self.assertEqual(test_sprite_1, result)
def test_should_return_empty_list_when_calling_find_by_name(self): # given simple_scene_manager = SimpleSceneManager() test_sprite_1 = XPGESprite(simple_scene_manager) test_sprite_1.name = "test_name_2" test_sprite_2 = XPGESprite(simple_scene_manager) test_sprite_3 = XPGESprite(simple_scene_manager) sprite_list = [test_sprite_1, test_sprite_2, test_sprite_3] simple_scene_manager._sprites = sprite_list # when result = simple_scene_manager.find_by_name("test_name") # then self.assertIsNotNone(result) self.assertEqual(0, len(result))
def test_should_add_sprite_to_group_on_creation(self): # given when group = Group() sprite = XPGESprite(None, group) # then self.assertTrue(group.has(sprite)) self.assertEqual(0, sprite.groups().index(group))
def __init__(self, scene_manager): super().__init__(scene_manager) star1 = pygame.image.load(os.path.join("demo_resources", "star.png")).convert() star2 = pygame.image.load(os.path.join("demo_resources", "star2.png")).convert() sprite1 = XPGESprite(self.scene_manager) sprite1.image = star1 sprite1.position = (10, 10) sprite1.components.append(ToggleSprite(sprite1)) sprite2 = XPGESprite(self.scene_manager) sprite2.name = "sprite2" sprite2.image = star2 sprite2.position = (200, 10) self.sprites.append(sprite1) self.sprites.append(sprite2)
def test_should_add_sprite_to_two_groups_on_creation(self): # given when group1 = Group() group2 = Group() sprite = XPGESprite(None, group1, group2) # then self.assertTrue(group1.has(sprite)) self.assertTrue(group2.has(sprite)) self.assertEqual(0, sprite.groups().index(group1)) self.assertEqual(1, sprite.groups().index(group2))
def setUp(self): self.sprite = XPGESprite(None) self.sprite.image = Surface((SPRITE_SIZE_X, SPRITE_SIZE_Y)) self.sprite.position = (SPRITE_X, SPRITE_Y) self.component_1 = Mock(spec=TestComponent1) self.component_2 = Mock(spec=TestComponent2) self.component_3 = Mock(spec=TestComponent3) self.sprite.components.append(self.component_1) self.sprite.components.append(self.component_2) self.sprite.components.append(self.component_3)
def __init__(self, scene_manager): super().__init__(scene_manager) star = pygame.image.load(os.path.join("demo_resources", "star.png")).convert() demo_sprite = XPGESprite(self.scene_manager) demo_sprite.image = star demo_sprite.position = (270, 190) demo_sprite.components.append(DemoSpriteBehaviour(demo_sprite)) self.sprites.append(demo_sprite)
def __init__(self, scene_manager): super().__init__(scene_manager) basket_image = pygame.Surface((100, 25)) basket_image.fill((255, 0, 0)) basket = XPGESprite(self.scene_manager) basket.image = basket_image basket.position = (350, 575) basket.name = "basket" basket.components.append(BasketController(basket)) basket.components.append(AppleSpawner(basket)) self.sprites.append(basket)
def test_should_throw_error_on_killing_when_sprite_not_existing(self): # given simple_scene_manager = SimpleSceneManager() test_sprite = XPGESprite(simple_scene_manager) test_sprite.name = "test sprite" mock_component = Mock(spec=SpriteBehaviour) test_sprite.components.append(mock_component) # when then with self.assertRaises(ValueError): simple_scene_manager.kill(test_sprite) mock_component.on_kill.assert_not_called()
def test_should_spawn_sprite(self): # given simple_scene_manager = SimpleSceneManager() test_sprite = XPGESprite(simple_scene_manager) test_sprite.name = "test sprite" mock_component = Mock(spec=SpriteBehaviour) test_sprite.components.append(mock_component) # when simple_scene_manager.spawn(test_sprite) # then self.assertIn(test_sprite, simple_scene_manager._sprites) mock_component.on_spawn.assert_called_once()
def on_update(self): self.counter += 1 if self.counter % 100 == 0: apple_image = pygame.Surface((25, 25)) apple_image.fill((125, 125, 0)) apple = XPGESprite(self.sprite.scene_manager) apple.image = apple_image apple.components.append(AppleController(apple)) x = random.randint(0, 775) y = 0 apple.position = (x, y) self.sprite.scene_manager._sprites.append(apple) self.sprite.scene_manager.static["apples_number"] += 1 print("Number of apples: {}".format( self.sprite.scene_manager.static["apples_number"]))