Пример #1
0
 def __init__(self):
     self.app = QApplication(sys.argv)
     self.init_scene()
     self.debug_stub = DebugStub(8000)
     self.debug_stub.run()
     self.init_game()
     self.arena.start()
     self.spawn_timer = QTimer()
     self.spawn_timer.timeout.connect(self.check_and_spawn_tick)
     self.spawn_timer.start(2000)
Пример #2
0
 def __init__(self):
     self.app = QApplication(sys.argv)
     self.init_scene()
     self.debug_stub = DebugStub(8000)
     self.debug_stub.run()
     self.init_game()
     self.arena.start()
     self.spawn_timer = QTimer()
     self.spawn_timer.timeout.connect(self.check_and_spawn_tick)
     self.spawn_timer.start(2000)
Пример #3
0
class App(object):
    def __init__(self):
        self.app = QApplication(sys.argv)
        self.init_scene()
        self.debug_stub = DebugStub(8000)
        self.debug_stub.run()
        self.init_game()
        self.arena.start()
        self.spawn_timer = QTimer()
        self.spawn_timer.timeout.connect(self.check_and_spawn_tick)
        self.spawn_timer.start(2000)

    def init_scene(self):
        self.scene = QGraphicsScene()
        scene = self.scene
        scene.setSceneRect(-10 * UNIT, -10 * UNIT, 20 * UNIT, 20 * UNIT)
        scene.setItemIndexMethod(QGraphicsScene.NoIndex)

        view = self.view = QGraphicsView(scene)
        view.setGeometry(100, 100, 15 * UNIT, 15 * UNIT)
        view.setRenderHint(QPainter.Antialiasing)
        #view.setBackgroundBrush(QBrush(QColor(0, 255, 0), Qt.CrossPattern))
        view.setCacheMode(QGraphicsView.CacheBackground)
        view.setViewportUpdateMode(QGraphicsView.BoundingRectViewportUpdate)
        view.setDragMode(QGraphicsView.ScrollHandDrag)
        view.show()

    def init_game(self):
        self.init_arena()
        self.init_players()
        self.arena.start()

    def init_arena(self):
        self.arena = Arena()
        self.arena.set_scene(self.scene)

    def init_players(self):
        tank1 = AI2AITank(Qt.blue, 2)
        tank1.position = QPointF(UNIT * 0, UNIT * 0)
        self.arena.add(tank1)
        tank1.start()
        tank1.agent.debug_id = "tank1"
        self.debug_stub.add_agent(tank1.agent)

        self.spawn_tank_in_range((0, 0), 4)

    def spawn_tank_in_range(self, pos, radius):
        tank = RounderAITank(Qt.red, 1)
        if isinstance(pos, tuple):
            tank.position = QPointF(pos[0] * UNIT, pos[1] * UNIT)
        else:
            tank.position = pos
        ra = random.uniform(0, math.pi * 2)
        d = random.uniform(radius * 0.5 * UNIT, radius * UNIT)
        tank.position += QPointF(math.cos(ra), math.sin(ra)) * d
        tank.rotation = ra
        tank.cannon_rotation = 0
        self.arena.add(tank)

    def run(self):
        self.app.exec_()

    @property
    def n_player(self):
        acc = 0
        for i in self.arena.children:
            if i.alive and isinstance(i, Tank):
                acc += 1
        return acc

    def check_and_spawn_tick(self):
        if self.n_player < 3:
            self.spawn_tank_in_range((0, 0), 8)

    def update(self):
        self.arena.update()

    def stop(self):
        self.debug_stub.stop()
        print("shutdown over")
Пример #4
0
class App(object):
    def __init__(self):
        self.app = QApplication(sys.argv)
        self.init_scene()
        self.debug_stub = DebugStub(8000)
        self.debug_stub.run()
        self.init_game()
        self.arena.start()
        self.spawn_timer = QTimer()
        self.spawn_timer.timeout.connect(self.check_and_spawn_tick)
        self.spawn_timer.start(2000)

    def init_scene(self):
        self.scene = QGraphicsScene()
        scene = self.scene
        scene.setSceneRect(-10 * UNIT, -10 * UNIT, 20 * UNIT, 20 * UNIT)
        scene.setItemIndexMethod(QGraphicsScene.NoIndex)

        view = self.view = QGraphicsView(scene)
        view.setGeometry(100, 100, 15 * UNIT, 15 * UNIT)
        view.setRenderHint(QPainter.Antialiasing)
        # view.setBackgroundBrush(QBrush(QColor(0, 255, 0), Qt.CrossPattern))
        view.setCacheMode(QGraphicsView.CacheBackground)
        view.setViewportUpdateMode(QGraphicsView.BoundingRectViewportUpdate)
        view.setDragMode(QGraphicsView.ScrollHandDrag)
        view.show()

    def init_game(self):
        self.init_arena()
        self.init_players()
        self.arena.start()

    def init_arena(self):
        self.arena = Arena()
        self.arena.set_scene(self.scene)

    def init_players(self):
        tank1 = AI2AITank(Qt.blue, 2)
        tank1.position = QPointF(UNIT * 0, UNIT * 0)
        self.arena.add(tank1)
        tank1.start()
        tank1.agent.debug_id = "tank1"
        self.debug_stub.add_agent(tank1.agent)

        self.spawn_tank_in_range((0, 0), 4)

    def spawn_tank_in_range(self, pos, radius):
        tank = RounderAITank(Qt.red, 1)
        if isinstance(pos, tuple):
            tank.position = QPointF(pos[0] * UNIT, pos[1] * UNIT)
        else:
            tank.position = pos
        ra = random.uniform(0, math.pi * 2)
        d = random.uniform(radius * 0.5 * UNIT, radius * UNIT)
        tank.position += QPointF(math.cos(ra), math.sin(ra)) * d
        tank.rotation = ra
        tank.cannon_rotation = 0
        self.arena.add(tank)

    def run(self):
        self.app.exec_()

    @property
    def n_player(self):
        acc = 0
        for i in self.arena.children:
            if i.alive and isinstance(i, Tank):
                acc += 1
        return acc

    def check_and_spawn_tick(self):
        if self.n_player < 3:
            self.spawn_tank_in_range((0, 0), 8)

    def update(self):
        self.arena.update()

    def stop(self):
        self.debug_stub.stop()
        print("shutdown over")