问题描述
python的 select.select
的文档说:
The documentation for python's select.select
says:
我的小组正在使用pygame和套接字开发一种简化的多人游戏. (我们不是使用Twisted或zeromq或任何类似的库;这是唯一的约束.)
My group is developing a simplistic multiplayer game using pygame and sockets. (We are not using Twisted or zeromq or any similar libraries; this being the only constraint).
现在,用于游戏设计;我们希望当pygame屏幕中发生按键事件时,玩家将数据发送到服务器.否则,客户端/播放器端的套接字将被挂接到服务器上,并监听其他播放器端发生的更改.对于此任务,我需要pygame和socket并行工作.建议我在#python上使用多个用户的select
模块.
Now, for the game design; we want the player to send data to the server when a key event occurs in the pygame screen. The client/player side's socket will otherwise be hooked to server and listen for changes occurring on other players' side. For this task, I'd need the pygame and socket to work parallely. I was recommended to use select
module from several users on #python.
我可以做类似的事情吗?
Can I do something like:
inp = [self.sock, pygame.event.get]
out = [self.server]
i, o, x = select.select( inp, out, [] )
如果没有,应该怎么走?
If not, what should be the way to go?
推荐答案
您可以将线程用于此任务.是否有必要连续(而不是同时)处理服务器消息和pygame事件?如果是这样,您可以这样做:
You could use threads for this task. Is it necessary to process server messages and pygame events in series (not concurrently)? If so, you could do this:
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()"""
如果没有,则可以在PygameHandler
和SocketListener
类的run方法中处理事件.
If not, you could process the events inside the run methods of the PygameHandler
and SocketListener
classes.
这篇关于可以在select.select输入列表中处理pygame事件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!