Пример #1
0
 def terminate(self):
     app = engine.app()
     if app.program.AIState is not app.program.AIState.TRAIN:
         return
     if self.iteration < engine.app().program.iterations:
         engine.app().program.fitness = None
     else:
         engine.app().program.fitness = self.score / self.iteration
Пример #2
0
 def event(self, event: engine.event.KeyPress):
     if event.dispatch(engine.event.KeyPress) and event.key is engine.input.Key.COLON:
         try:
             if self.count >= 5:
                 self._aglo.fitness(self._value)
                 self.network = next(self._aglo._iter)
             else:
                 self.count += 1
                 self._value += self.score.value
         except StopIteration:
             self.network = self._aglo.merge()
             engine.app().event(engine.event.KeyPress(engine.input.Key.ESCAPE))
Пример #3
0
    def initialize(self):
        super().initialize()
        self.obstacle = self.Get(ObstacleManager)

        self._text_score = engine.render.Text(0)
        engine.instantiate(engine.component.Render(self._text_score, True),
                           parent=self.entity,
                           transform=engine.component.Transform(
                               engine.Vector(GameApplication.width - 20, 32)))
        self._text_high = engine.render.Text(0)
        engine.instantiate(engine.component.Render(self._text_high, True),
                           parent=self.entity,
                           transform=engine.component.Transform(
                               engine.Vector(GameApplication.width - 20, 20)))

        if engine.app().program.AIState >= GameApplication.AIState.ACTIVE:
            self._text_net_out = engine.render.Text([0, 0, 0])
            engine.instantiate(engine.component.Render(self._text_net_out,
                                                       True),
                               parent=self.entity,
                               transform=engine.component.Transform(
                                   engine.Vector(GameApplication.width - 300,
                                                 50)))

            self._text_net = engine.render.Text("NET")
            engine.instantiate(engine.component.Render(self._text_net, True),
                               parent=self.entity,
                               transform=engine.component.Transform(
                                   engine.Vector(GameApplication.width - 300,
                                                 70)))

        self.state = self.GameState.RUN
Пример #4
0
 def __init__(self, manager: StateManager):
     self._jump = False
     self._fall = False
     self.floor = False
     self.manager = manager
     self.layers: engine.layer.Type = engine.app().setting.collision(
     ).layers
Пример #5
0
    def initialize(self):
        app = engine.app()
        engine.instantiate(engine.component.Event(self.close_event, "WINDOW"),
                           id=False)

        app.window._master.title(f"Pyrio")
        app.world.system(engine.ecs.systems.Render, True)
        app.world.add_system(PhysBodySystem(), "PHYSICS")
        app.world.add_system(engine.ecs.systems.FPS(50), "POST")
        # engine.ecs.World.active()._systems.insert(2, Collision())
        # engine.ecs.World.active()._systems.insert(3, LandOnFloor())

        engine.instantiate(  # FPS Counter
            engine.component.FPS(),
            engine.component.Render(engine.render.Text("FPS"), True),
            transform=engine.component.Transform(
                engine.Vector(Game.width - 20, 10)))

        engine.instantiate(  # Player
            Player(),
            PhysBody(),
            engine.component.Collider(
                engine.physics.collider.Rectangle,
                engine.component.Transform(scl=engine.Vector(50, 50))),
            engine.component.Render(engine.render.Polygon.Quad(50, 50)),
            transform=engine.component.Transform(
                engine.Vector(Game.width // 8, 3 * Game.height // 4)))
Пример #6
0
    def update(self):
        data = [
            self.paddle.side,
            neural.maths.constrain(self.paddle.transform.position[1], *self._clamp),
            neural.maths.constrain(self.ball.transform.position[0], 0, Game.width),
            neural.maths.constrain(self.ball.transform.position[1], 0, Game.height),
            neural.maths.constrain(self.ball.velocity[0], 0, self.ball.SPEED),
            neural.maths.constrain(self.ball.velocity[1], 0, self.ball.SPEED),
        ]

        out = self.network.input(*data)
        if out[0] > out[1]:
            self.paddle.move_up()
        else:
            self.paddle.move_down()

        # TRAINING
        if self.ball.speed == 0:
            engine.app().event(engine.event.KeyPress(engine.input.Key.SPACE))
Пример #7
0
 def fail(self):
     if self.manager.state is self.manager.GameState.PAUSE:
         app = engine.app()
         if app.program.AIState is not app.program.AIState.TRAIN:
             return app.event(engine.event.KeyPress(engine.input.Key.R))
         self.iteration += 1
         self.score += self.manager.score
         app.window.title(f"Dinosaur - {self.iteration}")
         if self.iteration >= app.program.iterations:
             return app.event(engine.event.KeyPress(
                 engine.input.Key.ESCAPE))
         app.event(engine.event.KeyPress(engine.input.Key.R))
Пример #8
0
 def collide(self):
     if self.state is self.GameState.RUN and (
             self.score - engine.core.DeltaTime().dt() * 20) >= 1:
         self.state = self.GameState.HIT
         engine.app().program.database(self.score)
Пример #9
0
 def close_event(self, event: engine.event.KeyPress):
     if event.dispatch(engine.event.KeyPress
                       ) and event.key is engine.input.Key.ESCAPE:
         engine.app().event(engine.event.WindowClose())
Пример #10
0
 def initialize(self):
     super().initialize()
     self.controller = self.Get(PlayerController)
     self.network = engine.app().program.AI
     engine.app().window.title(f"Dinosaur - {self.iteration}")
Пример #11
0
 def remove(self):
     engine.app().world.destroy(self.children.popleft().entity)
Пример #12
0
    def initialize(self):
        app = engine.app()
        engine.instantiate(engine.component.Event(self.close, "WINDOW"))

        app.world.add_system(ScoreManager(), "RENDER")
        app.setting.collision().update({
            0: {1,},
        })

        scores = {
            "tally": Score(0),
            "left": Score(0),
            "right": Score(0)
        }

        engine.instantiate( # Ball
            (ball := Ball(scores)),
            engine.component.Event(ball.event_start), # Change collider to Circle once implemented
            engine.component.Collider(engine.physics.collider.Rectangle, engine.component.Transform(scl=engine.Vector(Ball.RADIUS, Ball.RADIUS)), 0),
            engine.component.Render(engine.render.Polygon.Circle(Ball.RADIUS, res=Ball.RESOLUTION)),
            transform=engine.component.Transform(engine.Vector(Game.width, Game.height) // 2)
        )

        score_parent = engine.instantiate(
            transform=engine.component.Transform(engine.Vector(Game.width // 2, Game.height // 10))
        )

        engine.instantiate(
            scores["tally"],
            engine.component.Render(engine.render.Text("T"), True),
            parent=score_parent,
            transform=engine.component.Transform(engine.Vector(0, 10))
        )

        engine.instantiate(
            scores["left"],
            engine.component.Render(engine.render.Text("L"), True),
            parent=score_parent,
            transform=engine.component.Transform(engine.Vector(-10, 0))
        )

        engine.instantiate(
            scores["right"],
            engine.component.Render(engine.render.Text("R"), True),
            parent=score_parent,
            transform=engine.component.Transform(engine.Vector(10, 0))
        )

        engine.instantiate( # Left Paddle
            Paddle(0),
            # (ai := PaddleAI(ball, scores["tally"])),
            # engine.component.Event(ai.event),
            PaddlePlayer(engine.input.Key.W, engine.input.Key.S),
            engine.component.Collider(engine.physics.collider.Rectangle, engine.component.Transform(scl=Paddle.SIZE), 1),
            engine.component.Render(Paddle.QUAD),
            transform=engine.component.Transform(engine.Vector(Paddle.SIZE[0], Game.height // 2))
        )

        engine.instantiate( # Right Paddle
            Paddle(1),
            # (ai := PaddleAI(ball, scores["tally"])),
            # engine.component.Event(ai.event),
            PaddlePlayer(engine.input.Key.UP, engine.input.Key.DOWN),
            engine.component.Collider(engine.physics.collider.Rectangle, engine.component.Transform(scl=Paddle.SIZE), 1),
            engine.component.Render(Paddle.QUAD),
            transform=engine.component.Transform(engine.Vector(Game.width - Paddle.SIZE[0], Game.height // 2))
        )
Пример #13
0
 def event_start(self, event):
     if event.dispatch(engine.event.KeyPress) and event.key is engine.input.Key.SPACE and self.speed == 0:
         engine.app().event(engine.event.KeyPress(engine.input.Key.COLON))
         self.start()