【问题标题】:PodSixNet multiplayer networking library for Python game based on Tkinter基于 Tkinter 的 Python 游戏 PodSixNet 多人网络库
【发布时间】:2016-03-05 15:53:08
【问题描述】:

我试图弄清楚如何使用 PodSixNet 使用 tkinter 多人游戏制作我的游戏。

然后我从两个终端会话中启动 server.py 和 main.py 并单击按钮,这会触发 Deck.confirm_move()。 Deck.confirm_move() 应该向服务器发送信息:

connection.Send({"action": "myaction", "myvar": 33}) 

问题是只有当我关闭窗口时,响应才在服务器上可见:

> 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>')

我有几个文件/类,所以我用它们做了一个最小的例子。如果您对我如何组织脚本有意见,请告诉我。

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()

甲板.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)

【问题讨论】:

    标签: python-2.7 tkinter network-programming multiplayer


    【解决方案1】:

    您可以将 cfg.canvas.mainloop() 中的循环替换为对窗口对象调用 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()
    

    你应该准备好了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-10
      • 1970-01-01
      • 2012-01-19
      • 1970-01-01
      • 2012-12-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多