基于 Tkinter 的 Python 游戏的 PodSixNet 多人网络库
PodSixNet multiplayer networking library for Python game based on Tkinter
我想弄清楚如何使用 PodSixNet 使用 tkinter 多人游戏来制作我的游戏。
然后我从两个终端会话启动 server.py 和 main.py 并单击按钮,这会触发 Deck.confirm_move()。 Deck.confirm_move() 应该向服务器发送信息:
connection.Send({"action": "myaction", "myvar": 33})
问题是响应仅在我关闭 window 时在服务器上可见:
> python ./server.py
Starting server on localhost
('boxesServe=', '<__main__.BoxesServer listening 127.0.0.1:31425 at 0x7f7de91193b0>')
new connection: channel = <__main__.ClientChannel connected 127.0.0.1:56286 at 0x7f7de9119638>
#THIS APPEARS WHEN I CLOSE THE WINDOW
**{'action': 'myaction', 'myvar': 33}
('myaction:', {'action': 'myaction', 'myvar': 33})**
> python ./main.py
('connection=', '<PodSixNet.EndPoint.EndPoint 127.0.0.1:31425 at 0x7f447af96cf8>')
我有几个 files/classes,所以我用它们做了一个最小的例子。如果您对我如何组织脚本有任何意见,请告诉我。
main.py:
#launch the GUI and mainloop
from PodSixNet.Connection import ConnectionListener, connection
import Gui
import config as cfg
if __name__ == "__main__":
gui_instance = Gui.Gui()
gui_instance.main()
cfg.canvas.mainloop()
pass
while 1:
connection.Pump()
gui_instance.Pump()
config.py:
#Here I initialize objects/parameters that are shared between scripts.
#Is this good practice?
win = None
canvas = None
deck = False
Gui.py:
#Define the GUI with a canvas, buttons, etc. Call self.Connect()
import Tkinter as tk
from PodSixNet.Connection import ConnectionListener, connection
import config as cfg
from Deck import Deck
class Gui(ConnectionListener):
def __init__(self):
cfg.win=tk.Tk()
"""Create cfg.canvas"""
cfg.canvas=tk.Canvas(cfg.win,height=300,width=300,name="canvas")
"""Buttons""" self.btnConf=tk.Button(cfg.win,text="Confirm",width=6,name="btnConf",bg="white",anchor=tk.W)
self.btnConf.bind('<ButtonRelease-1>', self.buttonCallback)
self.btnConf_window = cfg.canvas.create_window(50,50,anchor=tk.NW, window=self.btnConf)
cfg.canvas.grid(row = 1, column = 0, rowspan = 5)
self.Connect() # <--- PodSixNet's Connect
def buttonCallback(self, event):
cfg.deck.confirm_move()
def main(self):
"""Deal deck"""
cfg.deck = Deck()
Deck.py:
#Here is where the actions of the game are run.
#Here I want to use Send to send information to the server
from PodSixNet.Connection import ConnectionListener, connection
class Deck(ConnectionListener):
def __init__(self):
pass
def confirm_move(self):
print("connection=",str(connection))
connection.Send({"action": "myaction", "myvar": 33})
server.py:
import PodSixNet.Channel
import PodSixNet.Server
from time import sleep
class ClientChannel(PodSixNet.Channel.Channel):
def Network(self, data):
print data
def Network_myaction(self, data):
print("myaction:", data)
class BoxesServer(PodSixNet.Server.Server):
channelClass = ClientChannel
def __init__(self, *args, **kwargs):
PodSixNet.Server.Server.__init__(self, *args, **kwargs)
def Connected(self, channel, addr):
print 'new connection: channel = ', channel
print "Starting server on localhost"
boxesServe = BoxesServer()
print("boxesServe=",str(boxesServe))
while True:
boxesServe.Pump()
sleep(0.01)
您可以将 cfg.canvas.mainloop() 中的循环替换为在 window 对象上调用 update() 和 update_idletasks() 的循环。在您的情况下,while 循环变为:
while 1:
connection.Pump()
self.Pump()
cfg.win.update()
cfg.win.update_idletasks()
因为在 main.py 你现在有:
if __name__ == "__main__":
gui_instance = Gui.Gui()
gui_instance.main()
#cfg.canvas.mainloop()
在初始化 GUI 之后放置 while 循环,在 Gui.main():
的末尾
def main(self):
"""Deal deck"""
cfg.deck = Deck()
while 1:
connection.Pump()
self.Pump()
cfg.win.update()
cfg.win.update_idletasks()
你应该一切就绪。
我想弄清楚如何使用 PodSixNet 使用 tkinter 多人游戏来制作我的游戏。
然后我从两个终端会话启动 server.py 和 main.py 并单击按钮,这会触发 Deck.confirm_move()。 Deck.confirm_move() 应该向服务器发送信息:
connection.Send({"action": "myaction", "myvar": 33})
问题是响应仅在我关闭 window 时在服务器上可见:
> python ./server.py
Starting server on localhost
('boxesServe=', '<__main__.BoxesServer listening 127.0.0.1:31425 at 0x7f7de91193b0>')
new connection: channel = <__main__.ClientChannel connected 127.0.0.1:56286 at 0x7f7de9119638>
#THIS APPEARS WHEN I CLOSE THE WINDOW
**{'action': 'myaction', 'myvar': 33}
('myaction:', {'action': 'myaction', 'myvar': 33})**
> python ./main.py
('connection=', '<PodSixNet.EndPoint.EndPoint 127.0.0.1:31425 at 0x7f447af96cf8>')
我有几个 files/classes,所以我用它们做了一个最小的例子。如果您对我如何组织脚本有任何意见,请告诉我。
main.py:
#launch the GUI and mainloop
from PodSixNet.Connection import ConnectionListener, connection
import Gui
import config as cfg
if __name__ == "__main__":
gui_instance = Gui.Gui()
gui_instance.main()
cfg.canvas.mainloop()
pass
while 1:
connection.Pump()
gui_instance.Pump()
config.py:
#Here I initialize objects/parameters that are shared between scripts.
#Is this good practice?
win = None
canvas = None
deck = False
Gui.py:
#Define the GUI with a canvas, buttons, etc. Call self.Connect()
import Tkinter as tk
from PodSixNet.Connection import ConnectionListener, connection
import config as cfg
from Deck import Deck
class Gui(ConnectionListener):
def __init__(self):
cfg.win=tk.Tk()
"""Create cfg.canvas"""
cfg.canvas=tk.Canvas(cfg.win,height=300,width=300,name="canvas")
"""Buttons""" self.btnConf=tk.Button(cfg.win,text="Confirm",width=6,name="btnConf",bg="white",anchor=tk.W)
self.btnConf.bind('<ButtonRelease-1>', self.buttonCallback)
self.btnConf_window = cfg.canvas.create_window(50,50,anchor=tk.NW, window=self.btnConf)
cfg.canvas.grid(row = 1, column = 0, rowspan = 5)
self.Connect() # <--- PodSixNet's Connect
def buttonCallback(self, event):
cfg.deck.confirm_move()
def main(self):
"""Deal deck"""
cfg.deck = Deck()
Deck.py:
#Here is where the actions of the game are run.
#Here I want to use Send to send information to the server
from PodSixNet.Connection import ConnectionListener, connection
class Deck(ConnectionListener):
def __init__(self):
pass
def confirm_move(self):
print("connection=",str(connection))
connection.Send({"action": "myaction", "myvar": 33})
server.py:
import PodSixNet.Channel
import PodSixNet.Server
from time import sleep
class ClientChannel(PodSixNet.Channel.Channel):
def Network(self, data):
print data
def Network_myaction(self, data):
print("myaction:", data)
class BoxesServer(PodSixNet.Server.Server):
channelClass = ClientChannel
def __init__(self, *args, **kwargs):
PodSixNet.Server.Server.__init__(self, *args, **kwargs)
def Connected(self, channel, addr):
print 'new connection: channel = ', channel
print "Starting server on localhost"
boxesServe = BoxesServer()
print("boxesServe=",str(boxesServe))
while True:
boxesServe.Pump()
sleep(0.01)
您可以将 cfg.canvas.mainloop() 中的循环替换为在 window 对象上调用 update() 和 update_idletasks() 的循环。在您的情况下,while 循环变为:
while 1:
connection.Pump()
self.Pump()
cfg.win.update()
cfg.win.update_idletasks()
因为在 main.py 你现在有:
if __name__ == "__main__":
gui_instance = Gui.Gui()
gui_instance.main()
#cfg.canvas.mainloop()
在初始化 GUI 之后放置 while 循环,在 Gui.main():
的末尾def main(self):
"""Deal deck"""
cfg.deck = Deck()
while 1:
connection.Pump()
self.Pump()
cfg.win.update()
cfg.win.update_idletasks()
你应该一切就绪。