使用新的客户端socket-io连接在pygame中最多容纳6个玩家(玩家起始位置已固定)

时间:2019-05-10 22:13:22

标签: socket.io pygame sprite

我正在创建一个游戏,该游戏可以等待多达6位来自不同客户端的玩家(或者该游戏只是在连接至少2位玩家的情况下开始的)。我创建了一个Player精灵类,只是简单地在板上画出了精灵即可显示其工作原理。但是,我需要连接一个新的客户端连接,并将其分配给特定的精灵“令牌”之一(第一个客户端变为红色,第二个客户端变为蓝色,第三个客户端变为绿色,...)。我在板上使用了一张地图,该地图通过其颜色(R代表红色,B代表蓝色,G代表绿色,...)来标识各种标记,墙壁分别为0,特定房间的编号为H,走廊为H。

000000000
00000S000
001H2H300
0PH0H0HM0
004H5H600
0BH0H0H00
007H8H900
000G0W000
000000000

我是pygame和socket io的新手,我主要设计了该游戏的客户端,而另一名团队成员则设计了游戏的服务器端。我有一个代码块,试图弄清楚如何最好地分配一个新的调用服务器的客户端来玩游戏,以便他们拥有角色并从角色的初始位置开始。

当前,客户端代码只是这样简单地调用服务器:

# Set up the Server-Client Connection
sio = socketio.Client()
sio.connect('http://localhost:5000')

@sio.on('connect')
def on_connect():
    print('Successfully connected to the server.')

客户端代码中当前的相对功能:

 def __init__(self):
        pg.init()
        self.screen = WINDOW_SET
        pg.display.set_caption(TITLE)
        self.clock = pg.time.Clock()
        pg.key.set_repeat() # 1 move per key press
        self.load_data()
        logging.warning("Setting up basic configurations.")
        # Spawn a player
        pg.sprite.Sprite._init_(self)
        self.turn = True
        logging.warning("Turn equals True")

def load_data(self):
        game_folder = path.dirname(__file__)
        self.map_data = []
        with open(path.join(game_folder, 'map.txt'), 'rt') as f:
            for line in f:
                self.map_data.append(line)
        logging.warning("Loading the board map.")

    def new_board(self):
        # initialize all variables and do all the setup for a new game
        self.all_sprites = pg.sprite.Group()
        self.walls = pg.sprite.Group()
        self.rooms = pg.sprite.Group()
        self.halls = pg.sprite.Group()
        # INSTEAD: Have sprite create upon client connection up to 6 / start game
        for row, tiles in enumerate(self.map_data):
            for col, tile in enumerate(tiles):
                # Set Board Tiles
                board = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
                if tile in board:
                    Room(self, col, row)
                if tile == 'H':
                    Hall(self, col, row)
                if tile == '0':
                    Wall(self, col, row)

                # Set Player Tokens / Tiles   
                # Print client that sprite has been assigned to ********* 
                # ---Player1 = Scarlet
                #if tile == 'S': 
                #    self.playerScarlet = Player(self, col, row, RED)
                # --- Player2 = Mustard
                #if tile == 'M':
                #    self.player = Player(self, col, row, YELLOW)
                # --- Player2 = White
                #if tile == 'W':
                #    self.player = Player(self, col, row, WHITE)
                # --- Player2 = Green
                #if tile == 'G':
                #    self.player = Player(self, col, row, GREEN)
                # --- Player2 = Peacock
                #if tile == 'B':
                #    self.player = Player(self, col, row, BLUE)
                # --- Player2 = Plum
                #if tile == 'P':
                #    self.player = Player(self, col, row, PURPLE)

        logging.warning("Set up for a new game.")

字符被注释掉了,因为它们实际上并未分配,并且运行没有注释的游戏仅允许客户移动地图中最后添加的一个,即白色图块。

带有定义播放器的我的Sprites类:

class Player(pg.sprite.Sprite):
    def __init__(self, game, x, y, color):
        self.groups = game.all_sprites
        pg.sprite.Sprite.__init__(self, self.groups)
        self.game = game
        self.image = pg.Surface((TILE_SIZE, TILE_SIZE))
        self.image.fill(color)
        self.rect = self.image.get_rect()
        self.x = x
        self.y = y

    def move(self, dx=0, dy=0):
        if not self.collide_with_walls(dx, dy):
            self.x += dx
            self.y += dy

    def collide_with_walls(self, dx=0, dy=0):
        for wall in self.game.walls:
            if wall.x == self.x + dx and wall.y == self.y + dy:
                return True
        return False

    def update(self):
        self.rect.x = self.x * TILE_SIZE
        self.rect.y = self.y * TILE_SIZE

服务器端相关代码:

host = ''
port = 5000

sio = socketio.Server(logger=True)
app = socketio.WSGIApp(sio)

rooms = {} #key= guid, value= game
player_list = {} #key=sid, value=room_id

@sio.on('connect')
def connect(sid, environ):
    sio.save_session(sid, {"test" : "test string"})
    print('connect', sid)
    sio.emit('message', 'connected to server', sid)

我知道我需要一个函数,该函数接受客户端的sid并为其分配一个递增的索引,直到它达到6并拒绝任何进一步的连接,但是我很难在服务器上同时看到此集成(跟踪连接的客户端)和客户端。

任何帮助或建议将不胜感激!我正在为此而苦苦挣扎,并且正在努力更好地理解。

0 个答案:

没有答案