Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiplayer Mode #22

Open
monsdar opened this issue Feb 24, 2015 · 1 comment
Open

Multiplayer Mode #22

monsdar opened this issue Feb 24, 2015 · 1 comment
Milestone

Comments

@monsdar
Copy link
Owner

monsdar commented Feb 24, 2015

Integrate the ability to row online against others. What's the best way of doing that? Could this be a specific Boat? Could this be some kind of WorkoutLogic?

@monsdar monsdar added this to the Future milestone Mar 1, 2015
@gustavklopp
Copy link
Contributor

That would be great!
A multiplayer mode would be very interesting indeed.
I've seen that the package Twisted can be used for it.

Here is an example: It only displays the list of players currently connected to the server. But that's a start!
First run the server:

from twisted.internet.protocol import Factory
from twisted.protocols.basic import LineReceiver
from twisted.internet import reactor

class ChatProtocol(LineReceiver):
    def __init__(self, factory):
        self.factory = factory
        self.id_player = None

    def connectionMade(self):
        self.factory.count += 1
        self.id_player = 'player_' + str(self.factory.count)
        self.factory.users[self.id_player] = self
        player_list = self.factory.users.keys()
        self.broadcastMessage(','.join(player_list))

    def connectionLost(self, reason):
        if self.id_player in self.factory.users:
            del self.factory.users[self.id_player]
            player_list = self.factory.users.keys()
            self.broadcastMessage(','.join(player_list))

    def broadcastMessage(self, message):
        for id_player, protocol in self.factory.users.items():
            protocol.sendLine(message.encode())


class ChatFactory(Factory):
    def __init__(self):
        self.users = {} #maps instances to clients 
        self.count = 0

    def buildProtocol(self, addr):
        return ChatProtocol(self)


reactor.listenTCP(8000, ChatFactory())
reactor.run()

Then the client (try to run several clients from the command line, to close one of them, restart them etc...):

from twisted.internet import reactor
from twisted.internet.protocol import ClientFactory
from twisted.internet.task import LoopingCall
from twisted.protocols.basic import LineReceiver

import pygame

class ChatClientProtocol(LineReceiver):

    def __init__(self, recv):
        self.recv = recv

    def lineReceived(self,line):
        self.recv(line)

class ChatClient(ClientFactory):
    def __init__(self, recv):
        self.protocol = ChatClientProtocol
        self.recv = recv

    def buildProtocol(self, addr):
        return ChatClientProtocol(self.recv)

class PygameClient(object):

    def __init__(self):
        self.line = 'no message'
        pygame.init()
        self.screen = pygame.display.set_mode((300, 200))

    def new_line(self, line):
        self.line = line

    def tick(self):
        self.screen.fill((0,0,0))
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                reactor.stop() # just stop somehow
            elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
                reactor.stop() # just stop somehow
        self.screen.blit(pygame.font.SysFont('mono', 12, bold=True).render(self.line, True, (0, 255, 0)), (20,20))
        pygame.display.flip()

if __name__ == '__main__':
    FPS = 30

    c = PygameClient()

    lc = LoopingCall(c.tick)
    lc.start(1)
    reactor.connectTCP('localhost',8000, ChatClient(c.new_line))    
    reactor.run()

Basically we need to replace the loop controlled by Pygame by a loop controlled by Twisted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants