基于Tkinter的Python游戏PodSixNet多人网络库

时间:2016-03-05 15:53:08

标签: python-2.7 tkinter network-programming multiplayer

我试图弄清楚如何使用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()

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)

1 个答案:

答案 0 :(得分:1)

您可以使用在窗口对象上调用update()和update_idletasks()的循环替换cfg.canvas.mainloop()中的循环。在你的情况下,while循环变为:

IHtmlHelper

因为在main.py中你现在有:

while 1:
  connection.Pump()
  self.Pump()
  cfg.win.update()
  cfg.win.update_idletasks()

在初始化GUI之后,在Gui.main():

的末尾放置while循环
if __name__ == "__main__":
  gui_instance = Gui.Gui()
  gui_instance.main()
  #cfg.canvas.mainloop()

你应该全力以赴。