当前位置: 首页>>代码示例>>Python>>正文


Python Display.draw_board方法代码示例

本文整理汇总了Python中display.Display.draw_board方法的典型用法代码示例。如果您正苦于以下问题:Python Display.draw_board方法的具体用法?Python Display.draw_board怎么用?Python Display.draw_board使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在display.Display的用法示例。


在下文中一共展示了Display.draw_board方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: from display import Display [as 别名]
# 或者: from display.Display import draw_board [as 别名]
class GameServer:
    def __init__(self, port, mapfile, rounds_per_second, w, h, observers):
        self._port = port
        self.players = []
        self.observers = []
        self.numObservers = observers
        self.numPlayers = 0  # Will be overwritten by loadmap. (but included here to make PyCharm happy)
        self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.socket.bind(('', port))
        self.socket.listen(3)
        self.loadmap(mapfile)
        self.display = Display(w, h, self.board.width, self.board.height)
        self.rounds_per_second = rounds_per_second

    def start(self):
        print("Server started on port {}.".format(self._port))
        self.wait_for_observsers()
        self.wait_for_players()
        print("All players are ready, game is now starting.")
        self.send_gamestate_to_observers(unfiltered=True)

        self.display.init()
        first_round = True
        while True:
            self.display.clear()

            self.start_next_turn()
            self.resolve_food_harvest()
            self.move_and_spawn_units()
            self.resolve_fights()
            self.destroy_spawners()

            if random.randrange(0, 100) < 17:
                self.board.spawn_food()

            self.send_gamestate_to_players()
            self.send_gamestate_to_observers()

            self.display.draw_board(self.board, self.players)
            if first_round:
                self.wait_for_next_round(1)
                first_round = False
            if self.wait_for_next_round(self.rounds_per_second):
                return True


    def wait_for_next_round(self, rounds_per_seconds):
        num_updates = int(20 / rounds_per_seconds)
        for _ in range(num_updates):
            self.display.update(20)
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    print("Game terminated by host.")
                    return True
        return False

    def wait_for_observsers(self):
        print("Waiting for {} observer(s) to connect...".format(self.numObservers))
        for _ in range(self.numObservers):
            observer = self.get_player_from_socket()
            observer.name = "Observer"
            self.observers.append(observer)

    def wait_for_players(self):
        print("Waiting for {} player(s) to connect...".format(self.numPlayers))
        for _ in range(self.numPlayers):
            player = self.get_player_from_socket()
            player.start()
            self.players.append(player)

        while not self.check_players_ready():
            time.sleep(0.5)

    def start_next_turn(self):
        for player in self.players:
            player.start_next_turn()

    def get_player_from_socket(self):
        conn, addr = self.socket.accept()
        print("Recieved new connection from {}:{}".format(*addr))
        return Player(conn)

    def check_players_ready(self):
        for player in self.players:
            if not player.ready:
                return False
        return True

    def move_and_spawn_units(self):
        for playerNum, player in enumerate(self.players):
            # Set new spawning mode for the player
            mode = player.command.get("mode", "standard")
            if mode == "standard":
                player.mode = Unit
            elif mode == "harvester":
                player.mode = Harvester
            elif mode == "soldier":
                player.mode = Soldier

            # move all the units he sent a command for
#.........这里部分代码省略.........
开发者ID:etse,项目名称:AI-compo-strategic-war,代码行数:103,代码来源:server.py

示例2: readline_from_socket

# 需要导入模块: from display import Display [as 别名]
# 或者: from display.Display import draw_board [as 别名]
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.connect(("127.0.0.1", 5050))

display = None
board = None
players = None

for line in readline_from_socket(s):
    try:
        data = json.loads(line)
        if not display:
            board = GameBoardClient(*data["map_size"])
            players = [Player(None, name=player["name"]) for player in data["players"]]
            display = Display(600, 600, board.width, board.height)
            display.init()

        board.update(data["map"])
        display.clear()
        display.draw_board(board, players)
        display.update(fps=0)

    except Exception, e:
        print("Error parsing:", e.message)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            print("Game terminated by host.")
            sys.exit(0)
开发者ID:etse,项目名称:AI-compo-strategic-war,代码行数:31,代码来源:testObserver.py

示例3: __init__

# 需要导入模块: from display import Display [as 别名]
# 或者: from display.Display import draw_board [as 别名]
class GameAI:
    def __init__(self, name):
        self.name = name
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.display = None
        self.board = None
        self.players = None
        self.my_id = None

    def sendline(self, line):
        self.sock.send((line+"\n").encode('UTF-8'))

    def send_command(self, command):
        self.sendline(json.dumps(command))

    def start(self, ip, port):
        self.sock.connect((ip, port))
        self.sendline("name {}".format(self.name))

        for line in readline_from_socket(self.sock):
            data = json.loads(line)
            if "status" in data:
                self.handle_status_message(data)
                continue

            if not self.display:
                self.board = GameBoardClient(*data["map_size"])
                self.players = [Player(None, name="Player"+str(i)) for i in range(data["num_players"])]
                self.my_id = data["player_id"]
                self.players[self.my_id].name = self.name
                self.display = Display(600, 600, self.board.width, self.board.height)
                self.display.init()

            self.board.update(data["map"])
            self.display.clear()
            self.display.draw_board(self.board, self.players)
            self.display.update(fps=0)
            self.resolve_round()

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    print("Game terminated by host.")
                    sys.exit(0)

    def handle_status_message(self, command):
        '''
        Implement how to handle status-messages here. The current implementation
        will ignore any "Status OK" and print everything else.

        :param command: The data recieved from server (must be a status message)
        '''
        if command.get("status", "ok").lower() != "ok":
            print("Status: {} - Message: {}".format(command.get("status", "ok"), command.get("msg", "")))

    def resolve_round(self):
        '''
        This function will be called once each turn. Any login with the AI should be implemented here.
        Make sure that this command sends a new command to the server before it returns.

        The map-state is stored in self.board.
        '''

        # Simple implementation
        command = {"mode": random.choice(["standard", "harvester", "soldier"]), "moves": []}
        for unit in filter(lambda u: u.owner == self.my_id, self.board.units):
            x, y = unit.position
            legal_directions = ["north", "south", "west", "east"]
            direction = random.choice(legal_directions)
            command["moves"].append([x, y, direction])
            self.board.move_unit(x, y, direction)
        self.send_command(command)
开发者ID:etse,项目名称:AI-compo-strategic-war,代码行数:74,代码来源:testClient.py


注:本文中的display.Display.draw_board方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。