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.
import pyglet
from pyglet.window import mouse
from pyglet.window import key
from twisted.internet.endpoints import TCP4ServerEndpoint
from twisted.internet.protocol import Protocol
from twisted.internet.protocol import Factory
from twisted.internet.protocol import ClientFactory
from twisted.internet import reactor
from twisted.internet import task
import pickle
import zlib
pyglet.options['debug_gl'] = False
#pyglet window
class example(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
def update(self):
while not self.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
yield 1
#draw and manually flip frame buffers
def draw(self):
self.clear()
self.fps_display.draw()
self.flip()
#mouse click
def on_mouse_press(self,x,y,button,modifiers):
if button == pyglet.window.mouse.LEFT:
if len(self.user) != 0:
data = ['left_press',x,y]
for a in self.user:
a.sendData(data)
if button == pyglet.window.mouse.RIGHT:
if len(self.user) != 0:
data = ['right_press',x,y]
for a in self.user:
a.sendData(data)
def on_mouse_release(self,x,y,button,modifiers):
if button == pyglet.window.mouse.LEFT:
#if someone has logged in, transmit if you click the mouse
if len(self.user) != 0:
data = ['left_release',x,y]
for a in self.user:
a.sendData(data)
if button == pyglet.window.mouse.RIGHT:
#if someone has logged in, transmit if you click the mouse
if len(self.user) != 0:
data = ['right_release',x,y]
for a in self.user:
a.sendData(data)
def on_key_release(self,symbol, modifiers):
if symbol == key.S:
self.server = TCP4ServerEndpoint(reactor, 8007)
self.server.listen(QOTDFactory())
print "server initializing"
if symbol == key.C:
reactor.connectTCP('localhost', 8007, EchoClientFactory())
print "connecting"
if symbol == key.ESCAPE:
self.close()
#shutdown program gracefully
def shutdown(self, result):
reactor.stop()
#system error shutdown
def bailout(self, reason):
reason.printTraceback()
reactor.stop()
#twisted network classes
class Echo(Protocol):
#data reciever class
def connectionMade(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)
def dataReceived(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)
if data[0] == 'left_press':
print '1'
if data[0] == 'left_release':
print '2'
if data[0] == 'right_press':
print '3'
if data[0] == 'right_release':
print '4'
#data transmitter class
def sendData(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.
class QOTDFactory(Factory):
def buildProtocol(self, addr):
print addr, "connected."
return Echo()
#This class establishes a connection to a server.
class EchoClientFactory(ClientFactory):
def startedConnecting(self, connector):
print 'Started to connect.'
def buildProtocol(self, addr):
print 'Connected.'
return Echo()
def clientConnectionLost(self, connector, reason):
print 'Lost connection. Reason:', reason
def clientConnectionFailed(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].
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.