Hi,
That worked like a charm!!! I am finally able to get the two components
working in the same thread!
Thank you so much, you have no idea how long I've been trying to get
this to work for.
I would like to acknowledge you in the code and the acknowledgements
file. What name would you like me to use? I only currently have your
email address, which I'm not sure if you're happy for me to use or not.
Cheers,
On 08/12/2016 23:29, [email protected] wrote:
Hm, the example works fine but seems to hang on exit. Which is a bit
wierd considering I have basically the same implementation in a larger
program that seems to handle it fine.
|
importpyglet
frompyglet.window importmouse
frompyglet.window importkey
fromtwisted.internet.endpoints importTCP4ServerEndpoint
fromtwisted.internet.protocol importProtocol
fromtwisted.internet.protocol importFactory
fromtwisted.internet.protocol importClientFactory
fromtwisted.internet importreactor
fromtwisted.internet importtask
importpickle
importzlib
pyglet.options['debug_gl']=False
#pyglet window
classexample(pyglet.window.Window):
def__init__(self):
super(example,self).__init__(640,480,resizable=False,fullscreen=False,caption="Test")
self.clear()
#connected user variable, when someone connects the Echo Class will
add itself into it.
#then just call self.user.sendData to transmit to the connected user
or self.user.dataRecieved to get data.
self.user =[]
self.server =None
#since the Reactor is in control, to properly run the main game loop
you have to manually call the clock.
#this variable is to keep track of the last time the clock was ticked.
self.old_dt =pyglet.clock.tick()
self.fps_display =pyglet.clock.ClockDisplay()
#main game loop
defupdate(self):
whilenotself.has_exit:
#Make sure events are timed properly while coiterating with network
layer. That and tick clock events like the fps counter and game pacing.
dt =pyglet.clock.tick()
self.old_dt =self.old_dt +dt
#draw to screen
self.draw()
#manually dispatch window events (since twisted reactor is in control)
self.dispatch_events()
#return control to twisted Reactor
yield1
#draw and manually flip frame buffers
defdraw(self):
self.clear()
self.fps_display.draw()
self.flip()
#mouse click
defon_mouse_press(self,x,y,button,modifiers):
ifbutton ==pyglet.window.mouse.LEFT:
iflen(self.user)!=0:
data =['left_press',x,y]
fora inself.user:
a.sendData(data)
ifbutton ==pyglet.window.mouse.RIGHT:
iflen(self.user)!=0:
data =['right_press',x,y]
fora inself.user:
a.sendData(data)
defon_mouse_release(self,x,y,button,modifiers):
ifbutton ==pyglet.window.mouse.LEFT:
#if someone has logged in, transmit if you click the mouse
iflen(self.user)!=0:
data =['left_release',x,y]
fora inself.user:
a.sendData(data)
ifbutton ==pyglet.window.mouse.RIGHT:
#if someone has logged in, transmit if you click the mouse
iflen(self.user)!=0:
data =['right_release',x,y]
fora inself.user:
a.sendData(data)
defon_key_release(self,symbol,modifiers):
ifsymbol ==key.S:
self.server =TCP4ServerEndpoint(reactor,8007)
self.server.listen(QOTDFactory())
print"server initializing"
ifsymbol ==key.C:
reactor.connectTCP('localhost',8007,EchoClientFactory())
print"connecting"
ifsymbol ==key.ESCAPE:
self.close()
#shutdown program gracefully
defshutdown(self,result):
reactor.stop()
#system error shutdown
defbailout(self,reason):
reason.printTraceback()
reactor.stop()
#twisted network classes
classEcho(Protocol):
#data reciever class
defconnectionMade(self):
data ="An apple a day keeps the doctor away\r\n"
data =pickle.dumps(data)
compressed =zlib.compress(data)
self.transport.write(compressed)
window.user.append(self)
defdataReceived(self,data):
#have the Echo Protocol class representing the connection to the
currently connected user add itself
#to the window.user variable so we can comminicate with the connected
user in the game loop.
data =zlib.decompress(data)
data =pickle.loads(data)
ifdata[0]=='left_press':
print'1'
ifdata[0]=='left_release':
print'2'
ifdata[0]=='right_press':
print'3'
ifdata[0]=='right_release':
print'4'
#data transmitter class
defsendData(self,data):
data =pickle.dumps(data)
data =zlib.compress(data)
self.transport.write(data)
#this monitors the port and automatically calls Echo() when someone
connects, passing their address to it.
classQOTDFactory(Factory):
defbuildProtocol(self,addr):
printaddr,"connected."
returnEcho()
#This class establishes a connection to a server.
classEchoClientFactory(ClientFactory):
defstartedConnecting(self,connector):
print'Started to connect.'
defbuildProtocol(self,addr):
print'Connected.'
returnEcho()
defclientConnectionLost(self,connector,reason):
print'Lost connection. Reason:',reason
defclientConnectionFailed(self,connector,reason):
print'Connection failed. Reason:',reason
if__name__ =='__main__':
#initialize main window
window =example()
#coiterate between the reactor and the window
task.coiterate(window.update()).addCallback(window.shutdown).addErrback(window.bailout)
#start main reactor loop
reactor.run()
|
--
You received this message because you are subscribed to the Google
Groups "pyglet-users" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to [email protected]
<mailto:[email protected]>.
To post to this group, send email to [email protected]
<mailto:[email protected]>.
Visit this group at https://groups.google.com/group/pyglet-users.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups
"pyglet-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/pyglet-users.
For more options, visit https://groups.google.com/d/optout.