Games become more engaging when you are able to play against other people. Usually this means playing over the Internet using some sort of client-server architecture. In the Python world, Twisted is commonly used for this kind of architecture.
Twisted can be installed in several ways depending on your operating system. For more information see https://twistedmatrix.com/trac/wiki/Downloads.
Unfortunately, we cannot create a massive multiplayer game in this tutorial, but we can create a simple client-server setup, which will lay the foundations for a puzzle we will create in a later recipe.
from twisted.internet import reactor, protocol class Server(protocol.Protocol): def __init__(self): self.count = 0 def dataReceived(self, msg): self. count += 1 self.transport.write("%d %s" % (self.count, msg)) def main(): factory = protocol.ServerFactory() factory.protocol = Server reactor.listenTCP(8888,factory) reactor.run() if __name__ == '__main__': main()
As you can see the server runs on port 8888 over TCP (see http://en.wikipedia.org/wiki/Transmission_Control_Protocol).
from twisted.internet import reactor, protocol from pygame.locals import * import pygame class Client(protocol.Protocol): def __init__(self): self.msg = 'Hello' self.end_msg = False def sendMessage(self, msg): self.transport.write(msg) self.update(msg) def dataReceived(self, msg): self.msg = msg if msg.startswith("19"): self.end_msg = True def update(self, msg): screen = pygame.display.get_surface() screen.fill((255, 255, 255)) font = pygame.font.Font(None, 36) text = font.render(self.msg, 1, (200, 200, 200)) textpos = text.get_rect(centerx=screen.get_width()/2, centery=screen.get_height()/2) screen.blit(text, textpos) pygame.display.flip() if self.end_msg: reactor.stop() def send(p): p.sendMessage("Hello!") for i in xrange(1, 20): reactor.callLater(i * .1, p.sendMessage, "IMPORTANT MESSAGE!") def main(): pygame.init() screen = pygame.display.set_mode((400, 400)) pygame.display.set_caption('Network Demo') c = protocol.ClientCreator(reactor, Client) c.connectTCP("localhost", 8888).addCallback(send) reactor.run() while True: for event in pygame.event.get(): if event.type == QUIT: return if __name__ == '__main__': main()
We need to start the server, before we can start the client. In the game GUI, you should see Hello being displayed followed by 1 IMPORTANT MESSAGE! to 19 IMPORTANT MESSAGE! as shown in the following screenshot:
We saw in this example how to create a simple server and client with a Pygame GUI. In principle, we can now extend this setup to create a multiplayer game. The details of the Twisted client and server setup are given as follows:
Function |
Description |
---|---|
|
This writes a message. In this case we are prepending a sequence number to the message. |
|
This creates a Twisted server factory, which itself creates Twisted servers. |
|
This listens to port 8888 using the given factory. |
|
This starts the server or client. |
|
This stops the client or server. |
|
This registers a callback function with a parameter to be executed after a specified time in seconds. |
|
This creates a Twisted client. |
|
This connects the client via TCP on port 8888 and registers a callback function. |