def test_World_get_entities(self):
     w = World()
     e1 = PositionEntity(w, 1, 1)
     e2 = PositionEntity(w, 1, 2)
     self.assertEqual(len(w.get_entities(e1.position)), 1)
     e2.position.y = 1
     self.assertEqual(len(w.get_entities(e1.position)), 2)
Beispiel #2
0
 def test_World_get_entities(self):
     w = World()
     e1 = PositionEntity(w, 1, 1)
     e2 = PositionEntity(w, 1, 2)
     assert len(w.get_entities(e1.position)) == 1
     e2.position.y = 1
     assert len(w.get_entities(e1.position)) == 2
 def test_Entity_world(self):
     world = World()
     world2 = World()
     ent1 = Entity(world)
     ent2 = Entity(world2)
     self.assertEqual(ent1.world, world)
     self.assertNotEqual(ent1.world, world2)
     self.assertEqual(ent2.world, world2)
     self.assertNotEqual(ent2.world, world)
     self.assertNotEqual(ent1.world, ent2.world)
Beispiel #4
0
 def test_Entity_world(self):
     world = World()
     world2 = World()
     ent1 = Entity(world)
     ent2 = Entity(world2)
     assert ent1.world == world
     assert ent1.world != world2
     assert ent2.world == world2
     assert ent2.world != world
     assert ent1.world != ent2.world
    def test_Entity(self):
        world = World()
        world.add_system(PositionSystem())

        e = Entity(world)
        e2 = Entity(world)
        self.assertIsInstance(e, Entity)
        self.assertIsInstance(e2, Entity)
        self.assertNotEqual(e, e2)

        p = PositionEntity(world)
        self.assertIsInstance(p, PositionEntity)
        self.assertIsInstance(p, Entity)
Beispiel #6
0
    def test_Entity(self):
        world = World()
        world.add_system(PositionSystem())

        e = Entity(world)
        e2 = Entity(world)
        assert isinstance(e, Entity)
        assert isinstance(e2, Entity)
        assert e != e2

        p = PositionEntity(world)
        assert isinstance(p, PositionEntity)
        assert isinstance(p, Entity)
    def test_Applicator(self):
        world = World()

        class ErrornousApplicator(Applicator):
            def __init__(self):
                super(ErrornousApplicator, self).__init__()

        eapplicator = ErrornousApplicator()
        # No component types defined.
        self.assertRaises(ValueError, world.add_system, eapplicator)
        self.assertEqual(len(world.systems), 0)

        mapplicator = MovementApplicator()
        world.add_system(mapplicator)
        self.assertTrue(mapplicator in world.systems)
Beispiel #8
0
    def test_World_entities(self):
        w = World()
        assert len(w.entities) == 0

        for x in range(100):
            Entity(w)
        assert len(w.entities) == 100
    def test_World_entities(self):
        w = World()
        self.assertEqual(len(w.entities), 0)

        for x in range(100):
            Entity(w)
        self.assertEqual(len(w.entities), 100)
    def test_System(self):
        world = World()
        self.assertRaises(ValueError, world.add_system, None)
        self.assertRaises(ValueError, world.add_system, 1234)
        self.assertRaises(ValueError, world.add_system, "Test")

        class ErrornousSystem(System):
            def __init__(self):
                super(ErrornousSystem, self).__init__()

        esystem = ErrornousSystem()
        # No component types defined.
        self.assertRaises(ValueError, world.add_system, esystem)
        self.assertEqual(len(world.systems), 0)

        psystem = PositionSystem()
        world.add_system(psystem)
        self.assertTrue(psystem in world.systems)
Beispiel #11
0
    def test_Entity__inheritance(self):
        world = World()

        pos1 = PositionEntity(world)
        pos2 = PositionEntity(world, 10, 10)
        for p in (pos1, pos2):
            assert isinstance(p, PositionEntity)
            assert isinstance(p, Entity)
            assert isinstance(p.position, Position)
    def test_Entity__access(self):
        world = World()
        pos1 = PositionEntity(world)
        pos2 = PosEntity(world)

        pos1.position.x = 10

        # components are _always_ identified by a lower-case class name.
        def sx(p, v):
            p.pos.x = v
        self.assertRaises(AttributeError, sx, pos2, 10)
Beispiel #13
0
    def test_World_delete(self):
        w = World()
        e1 = Entity(w)
        e2 = Entity(w)

        assert len(w.entities) == 2
        w.delete(e1)
        assert len(w.entities) == 1
        w.delete(e2)
        assert len(w.entities) == 0

        # The next two should have no effect
        w.delete(e1)
        w.delete(e2)
    def test_World_delete(self):
        w = World()
        e1 = Entity(w)
        e2 = Entity(w)

        self.assertEqual(len(w.entities), 2)
        w.delete(e1)
        self.assertEqual(len(w.entities), 1)
        w.delete(e2)
        self.assertEqual(len(w.entities), 0)

        # The next two should have no effect
        w.delete(e1)
        w.delete(e2)
Beispiel #15
0
    def test_World_add_remove_system(self):
        world = World()
        assert isinstance(world, World)

        class SimpleSystem(object):
            def __init__(self):
                self.componenttypes = (Position, )

            def process(self, world, components):
                pass

        for method in (world.add_system, world.remove_system):
            for val in (None, "Test", Position, Entity(world)):
                with pytest.raises(ValueError):
                    method(val)

        psystem = SimpleSystem()
        world.add_system(psystem)
        assert len(world.systems) != 0
        assert psystem in world.systems
        world.remove_system(psystem)
        assert len(world.systems) == 0
        assert psystem not in world.systems

        psystem = PositionSystem()
        world.add_system(psystem)
        assert len(world.systems) != 0
        assert psystem in world.systems

        entity = PositionEntity(world)
        assert isinstance(entity.position, Position)

        world.remove_system(psystem)
        assert len(world.systems) == 0
        assert psystem not in world.systems

        # The data must stay intact in the world, even if the processing
        # system has been removed.
        assert isinstance(entity.position, Position)
    def test_Entity_delete(self):
        w = World()
        e1 = Entity(w)
        e2 = Entity(w)

        self.assertEqual(len(w.entities), 2)
        e1.delete()
        self.assertEqual(len(w.entities), 1)
        e2.delete()
        self.assertEqual(len(w.entities), 0)

        # The next two should have no effect
        e1.delete()
        e2.delete()
Beispiel #17
0
    def test_Entity_delete(self):
        w = World()
        e1 = Entity(w)
        e2 = Entity(w)

        assert len(w.entities) == 2
        e1.delete()
        assert len(w.entities) == 1
        e2.delete()
        assert len(w.entities) == 0

        # The next two should have no effect
        e1.delete()
        e2.delete()
    def test_World_delete_entities(self):
        w = World()
        e1 = Entity(w)
        e2 = Entity(w)

        self.assertEqual(len(w.entities), 2)
        w.delete_entities((e1, e2))
        self.assertEqual(len(w.entities), 0)
        # The next should have no effect
        w.delete_entities((e1, e2))
Beispiel #19
0
    def test_World_delete_entities(self):
        w = World()
        e1 = Entity(w)
        e2 = Entity(w)

        assert len(w.entities) == 2
        w.delete_entities((e1, e2))
        assert len(w.entities) == 0
        # The next should have no effect
        w.delete_entities((e1, e2))
Beispiel #20
0
    def test_Applicator(self):
        world = World()

        class ErrornousApplicator(Applicator):
            def __init__(self):
                super(ErrornousApplicator, self).__init__()

        eapplicator = ErrornousApplicator()
        # No component types defined.
        with pytest.raises(ValueError):
            world.add_system(eapplicator)
        assert len(world.systems) == 0

        mapplicator = MovementApplicator()
        world.add_system(mapplicator)
        assert mapplicator in world.systems
Beispiel #21
0
def run():
    sdl2ext.init()
    window = sdl2ext.Window("The Pong Game", size=(800, 600))
    window.show()

    if "-hardware" in sys.argv:
        print("Using hardware acceleration")
        renderer = sdl2ext.RenderContext(window)
        factory = sdl2ext.SpriteFactory(sdl2ext.TEXTURE, renderer=renderer)
    else:
        print("Using software rendering")
        factory = sdl2ext.SpriteFactory(sdl2ext.SOFTWARE)

    # Create the paddles - we want white ones. To keep it easy enough for us,
    # we create a set of surfaces that can be used for Texture- and
    # Software-based sprites.
    sp_paddle1 = factory.from_color(WHITE, size=(20, 100))
    sp_paddle2 = factory.from_color(WHITE, size=(20, 100))
    sp_ball = factory.from_color(WHITE, size=(20, 20))

    world = World()

    movement = MovementSystem(0, 0, 800, 600)
    collision = CollisionSystem(0, 0, 800, 600)
    aicontroller = TrackingAIController(0, 600)
    if factory.sprite_type == sdl2ext.SOFTWARE:
        spriterenderer = SoftwareRenderer(window)
    else:
        spriterenderer = TextureRenderer(renderer)

    world.add_system(aicontroller)
    world.add_system(movement)
    world.add_system(collision)
    world.add_system(spriterenderer)

    player1 = Player(world, sp_paddle1, 0, 250)
    player2 = Player(world, sp_paddle2, 780, 250, True)
    ball = Ball(world, sp_ball, 390, 290)
    ball.velocity.vx = -BALL_SPEED
    collision.ball = ball
    aicontroller.ball = ball

    running = True
    while running:
        for event in sdl2ext.get_events():
            if event.type == sdlevents.SDL_QUIT:
                running = False
                break
            if event.type == sdlevents.SDL_KEYDOWN:
                if event.key.keysym.sym == sdlkc.SDLK_UP:
                    player1.velocity.vy = -PADDLE_SPEED
                elif event.key.keysym.sym == sdlkc.SDLK_DOWN:
                    player1.velocity.vy = PADDLE_SPEED
            elif event.type == sdlevents.SDL_KEYUP:
                if event.key.keysym.sym in (sdlkc.SDLK_UP, sdlkc.SDLK_DOWN):
                    player1.velocity.vy = 0
        sdltimer.SDL_Delay(10)
        world.process()
 def test_World(self):
     w = World()
     self.assertIsInstance(w, World)
Beispiel #23
0
 def test_Entity_id(self):
     world = World()
     ent1 = Entity(world)
     ent2 = Entity(world)
     assert ent1.id != ent2.id
Beispiel #24
0
    def test_Applicator_process(self):
        world = World()

        class ErrornousApplicator(Applicator):
            def __init__(self):
                super(ErrornousApplicator, self).__init__()
                self.componenttypes = (Position, Movement)

        eapplicator = ErrornousApplicator()
        world.add_system(eapplicator)
        for x in range(10):
            MovingEntity(world)
        assert eapplicator in world.systems
        with pytest.raises(NotImplementedError):
            world.process()

        world2 = World()
        mapplicator = MovementApplicator()
        world2.add_system(mapplicator)
        for x in range(10):
            MovingEntity(world2, vx=1, vy=1)
        assert mapplicator in world2.systems
        world2.process()
        for c in world2.components[Position].values():
            assert c.x == 1
            assert c.y == 1
        world2.process()
        for c in world2.components[Position].values():
            assert c.x == 2
            assert c.y == 2
Beispiel #25
0
    def test_System_process(self):
        world = World()

        class ErrornousSystem(System):
            def __init__(self):
                super(ErrornousSystem, self).__init__()
                self.componenttypes = (Position, )

        esystem = ErrornousSystem()
        world.add_system(esystem)
        for x in range(10):
            PositionEntity(world)
        assert esystem in world.systems
        with pytest.raises(NotImplementedError):
            world.process()

        world2 = World()
        psystem = PositionSystem()
        world2.add_system(psystem)
        for x in range(10):
            PositionEntity(world2)
        assert psystem in world2.systems
        world2.process()
        for c in world2.components[Position].values():
            assert c.x == 1
            assert c.y == 1
        world2.process()
        for c in world2.components[Position].values():
            assert c.x == 2
            assert c.y == 2
Beispiel #26
0
    def test_System(self):
        world = World()
        with pytest.raises(ValueError):
            world.add_system(None)
        with pytest.raises(ValueError):
            world.add_system(1234)
        with pytest.raises(ValueError):
            world.add_system("Test")

        class ErrornousSystem(System):
            def __init__(self):
                super(ErrornousSystem, self).__init__()

        esystem = ErrornousSystem()
        # No component types defined.
        with pytest.raises(ValueError):
            world.add_system(esystem)
        assert len(world.systems) == 0

        psystem = PositionSystem()
        world.add_system(psystem)
        assert psystem in world.systems
    def test_System_process(self):
        world = World()

        class ErrornousSystem(System):
            def __init__(self):
                super(ErrornousSystem, self).__init__()
                self.componenttypes = (Position,)

        esystem = ErrornousSystem()
        world.add_system(esystem)
        for x in range(10):
            PositionEntity(world)
        self.assertTrue(esystem in world.systems)
        self.assertRaises(NotImplementedError, world.process)

        world2 = World()
        psystem = PositionSystem()
        world2.add_system(psystem)
        for x in range(10):
            PositionEntity(world2)
        self.assertTrue(psystem in world2.systems)
        world2.process()
        for c in world2.components[Position].values():
            self.assertEqual(c.x, 1)
            self.assertEqual(c.y, 1)
        world2.process()
        for c in world2.components[Position].values():
            self.assertEqual(c.x, 2)
            self.assertEqual(c.y, 2)
    def test_Applicator_process(self):
        world = World()

        class ErrornousApplicator(Applicator):
            def __init__(self):
                super(ErrornousApplicator, self).__init__()
                self.componenttypes = (Position, Movement)

        eapplicator = ErrornousApplicator()
        world.add_system(eapplicator)
        for x in range(10):
            MovingEntity(world)
        self.assertTrue(eapplicator in world.systems)
        self.assertRaises(NotImplementedError, world.process)

        world2 = World()
        mapplicator = MovementApplicator()
        world2.add_system(mapplicator)
        for x in range(10):
            MovingEntity(world2, vx=1, vy=1)
        self.assertTrue(mapplicator in world2.systems)
        world2.process()
        for c in world2.components[Position].values():
            self.assertEqual(c.x, 1)
            self.assertEqual(c.y, 1)
        world2.process()
        for c in world2.components[Position].values():
            self.assertEqual(c.x, 2)
            self.assertEqual(c.y, 2)
 def test_Entity_id(self):
     world = World()
     ent1 = Entity(world)
     ent2 = Entity(world)
     self.assertNotEqual(ent1.id, ent2.id)
Beispiel #30
0
 def test_World(self):
     w = World()
     assert isinstance(w, World)