Can pygame events be handled in select.select input list?
python
Note that on Windows, it only works for sockets; on other operating
systems, it also works for other file types (in particular, on Unix,
it works on pipes).
我的小组正在使用pygame和套接字开发一种简化的多人游戏。 (我们没有使用Twisted或zeromq或任何类似的库;这是唯一的约束)。
现在,用于游戏设计;我们希望当pygame屏幕上发生按键事件时,玩家将数据发送到服务器。否则,客户端/播放器端的套接字将被挂接到服务器上,并监听其他播放器端发生的更改。对于此任务,我需要pygame和socket并行工作。建议我在#python上使用多个用户的
我可以做以下事情吗:
1 2 3 | inp = [self.sock, pygame.event.get] out = [self.server] i, o, x = select.select( inp, out, [] ) |
如果没有,应该怎么走?
您可以将线程用于此任务。是否有必要连续(而不是同时)处理服务器消息和pygame事件?如果是这样,您可以执行以下操作:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | class SocketListener(threading.Thread): def __init__(self, sock, queue): threading.Thread.__init__(self) self.daemon = True self.socket = sock self.queue = queue def run(self): while True: msg = self.socket.recv() self.queue.put(msg) class PygameHandler(threading.Thread): def __init__(self, queue): threading.Thread.__init__(self) self.queue = queue self.daemon = True def run(self): while True: self.queue.put(pygame.event.wait()) queue = Queue.Queue() PygameHandler(queue).start() SocketListener(queue).start() while True: event = queue.get() """Process the event()""" |
如果没有,则可以在