using Pyro for network games

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael Rybak

    #1

    using Pyro for network games

    Hi, everyone.
    In topic "2-player game, client and server at localhost", I've asked
    about subj, and Peter Hansen suggested to switch to Twisted, Pyro or
    the like.

    I've tried using Pyro.

    I've written a very very simple test-game, in which you have 2 balls
    controlled by 2 players. Each player moves his mouse somewhere at
    his window, and his ball starts moving towards the pointer. No
    objectives, just to test how it works. The code is very small, so I
    can put it all here, skipping obvious stuff.

    I've tried playing this test-game via local-host - all is ok.
    Then I've tested via Internet connection with my friend. I have a
    33.6 Kbps modem, he has a 2 MBps dedicated line (if this is the term),
    and we ran a server at his pc and both connected to it. His ball ran as
    a child, smoothly and quickly, while I had about 5 fps :(, and for him
    it looked like my ball is simply very slow. I realise that client at
    my pc *has* to work slower than the client at server's pc, but hey,
    I've played Quake2 and WarCraft 2 via 33.6 modem, and those should have
    much more stuff to transfer per second :(

    Please help me in any way you can think of. I'd welcome links to
    Python games written with Pyro, tips on what I am doing wrong, on not
    Pythonically enough - anything.

    ############### #############se rver.py######## #########
    #
    [..imports..]
    class game__(game_, Pyro.core.ObjBa se):
    def __init__(self):
    #storage for balls' coordinates
    game_.__init__( self)
    Pyro.core.ObjBa se.__init__(sel f)

    [..server initialization. .]

    daemon.requestL oop()
    END############ #############se rver.py######## #########

    ############### #############cl ient.py######## #########
    [..imports..]

    [..preparations to create proxy..]
    proxy=Pyro.core .getAttrProxyFo rURI(URI)

    [..imports..]

    def process_user_in put(game, id):#id is client's id - 0 or 1
    nx, ny = pygame.mouse.ge t_pos()
    x, y = game.ball[id].get_pos()
    dx, dy = nx - x, ny - y
    leng = sqrt(dx*dx + dy*dy)
    k = 20 / leng
    dx *= k
    dy *= k
    game.move(id, dx, dy) #remote call: move ball


    id = proxy.get_n_cli ents() #which ball to control
    if id < 2:
    proxy.new_clien t()

    pygame.init()
    scr = pygame.display. set_mode((640, 480))

    g = game(proxy.get_ status(), scr)
    #get_status provides 2 pairs of balls's current coordinates
    #g, "game" instance, is a local storage, able to render itself

    while 1:
    g.set_status(pr oxy.get_status( ))
    g.render()
    process_user_in put(proxy, id)

    time.sleep(0.03 )

    [..quit = (ESCAPE is pressed)..]
    if quit: break

    END############ #############cl ient.py######## #########

    --
    Best Regards,
    Michael Rybak mailto:accepted @ukr.net

  • garabik-news-2005-05@kassiopeia.juls.savba.sk

    #2
    Re: using Pyro for network games

    Michael Rybak <accepted@ukr.n et> wrote:[color=blue]
    > Hi, everyone.
    > In topic "2-player game, client and server at localhost", I've asked
    > about subj, and Peter Hansen suggested to switch to Twisted, Pyro or
    > the like.
    >
    > I've tried using Pyro.
    >
    > I've written a very very simple test-game, in which you have 2 balls
    > controlled by 2 players. Each player moves his mouse somewhere at
    > his window, and his ball starts moving towards the pointer. No
    > objectives, just to test how it works. The code is very small, so I
    > can put it all here, skipping obvious stuff.
    >
    > I've tried playing this test-game via local-host - all is ok.
    > Then I've tested via Internet connection with my friend. I have a
    > 33.6 Kbps modem, he has a 2 MBps dedicated line (if this is the term),
    > and we ran a server at his pc and both connected to it. His ball ran as
    > a child, smoothly and quickly, while I had about 5 fps :(, and for him
    > it looked like my ball is simply very slow. I realise that client at
    > my pc *has* to work slower than the client at server's pc, but hey,
    > I've played Quake2 and WarCraft 2 via 33.6 modem, and those should have
    > much more stuff to transfer per second :(
    >
    > Please help me in any way you can think of. I'd welcome links to
    > Python games written with Pyro, tips on what I am doing wrong, on not
    > Pythonically enough - anything.[/color]


    Do not use pyro, use simple UDP protocol.
    I've written networked tetris in python, communicating via
    UDP protocol, and used it successfully on very congested lines.
    If all you need is to transfer pointer coordinates, UDP is perfect since
    you do not need feedback.

    use something like this for server:

    import socket
    s = socket.socket(s ocket.AF_INET, socket.SOCK_DGR AM)
    s.bind(('', port))
    while 1:
    data, addr = s.recvfrom(1024 )
    print `data`


    and for client:

    import socket

    outsock = socket.socket(s ocket.AF_INET, socket.SOCK_DGR AM)
    outsock.bind((' ', 0))
    outsock.sendto( 'message', ('server-hostname', server_port))



    --
    -----------------------------------------------------------
    | Radovan Garabík http://kassiopeia.juls.savba.sk/~garabik/ |
    | __..--^^^--..__ garabik @ kassiopeia.juls .savba.sk |
    -----------------------------------------------------------
    Antivirus alert: file .signature infected by signature virus.
    Hi! I'm a signature virus! Copy me into your signature file to help me spread!

    Comment

    • Michael Rybak

      #3
      Re[2]: using Pyro for network games

      gn20kjss> Do not use pyro, use simple UDP protocol.
      gn20kjss> I've written networked tetris in python, communicating via
      gn20kjss> UDP protocol, and used it successfully on very congested lines.

      Would you please be so kind to share that with me? That would be
      greatly helpful, because 1) I'd run it together with my friend to see
      what speed I can get from UDP 2) I'd grasp the networking part of your
      code and reuse it.

      gn20kjss> If all you need is to transfer pointer coordinates, UDP is perfect since
      gn20kjss> you do not need feedback.

      gn20kjss> use something like this for server:

      gn20kjss> import socket
      gn20kjss> s = socket.socket(s ocket.AF_INET, socket.SOCK_DGR AM)
      gn20kjss> s.bind(('', port))
      gn20kjss> while 1:
      gn20kjss> data, addr = s.recvfrom(1024 )
      gn20kjss> print `data`


      gn20kjss> and for client:

      gn20kjss> import socket

      gn20kjss> outsock = socket.socket(s ocket.AF_INET, socket.SOCK_DGR AM)
      gn20kjss> outsock.bind((' ', 0))
      gn20kjss> outsock.sendto( 'message', ('server-hostname', server_port))
      Would you recommend some reading on this? I have some immediate
      questions to your code, but don't want to flood here. OK, I will flood
      here a bit: what's the print `` syntax?

      P.S. I loved your virus alert ;)


      gn20kjss> --
      gn20kjss> -----------------------------------------------------------
      gn20kjss> | Radovan Garabik http://kassiopeia.juls.savba.sk/~garabik/ |
      gn20kjss> | __..--^^^--..__ garabik @ kassiopeia.juls .savba.sk |
      gn20kjss> -----------------------------------------------------------
      gn20kjss> Antivirus alert: file .signature infected by signature virus.
      gn20kjss> Hi! I'm a signature virus! Copy me into your signature file to help me spread!



      --
      Best Regards,
      Michael Rybak mailto:accepted @ukr.net


      Antivirus alert: file .signature infected by signature virus.
      Hi! I'm a signature virus! Copy me into your signature file to help me spread!

      Comment

      • garabik-news-2005-05@kassiopeia.juls.savba.sk

        #4
        Re: using Pyro for network games

        Michael Rybak <accepted@ukr.n et> wrote:[color=blue]
        > gn20kjss> Do not use pyro, use simple UDP protocol.
        > gn20kjss> I've written networked tetris in python, communicating via
        > gn20kjss> UDP protocol, and used it successfully on very congested lines.
        >
        > Would you please be so kind to share that with me? That would be[/color]



        contrary to what the page says, you do not need pyncurses, just plain
        curses as included with modern pythons
        [color=blue]
        > greatly helpful, because 1) I'd run it together with my friend to see
        > what speed I can get from UDP 2) I'd grasp the networking part of your
        > code and reuse it.[/color]

        see the Net() class. I recommend you to use the same number for myport
        and otherport (you can with UDP, and it makes traversing firewalls
        easier)
        [color=blue]
        >
        > gn20kjss> If all you need is to transfer pointer coordinates, UDP is perfect since
        > gn20kjss> you do not need feedback.
        >
        > gn20kjss> use something like this for server:
        >
        > gn20kjss> import socket
        > gn20kjss> s = socket.socket(s ocket.AF_INET, socket.SOCK_DGR AM)
        > gn20kjss> s.bind(('', port))
        > gn20kjss> while 1:
        > gn20kjss> data, addr = s.recvfrom(1024 )
        > gn20kjss> print `data`
        >
        >
        > gn20kjss> and for client:
        >
        > gn20kjss> import socket
        >
        > gn20kjss> outsock = socket.socket(s ocket.AF_INET, socket.SOCK_DGR AM)
        > gn20kjss> outsock.bind((' ', 0))
        > gn20kjss> outsock.sendto( 'message', ('server-hostname', server_port))
        > Would you recommend some reading on this? I have some immediate[/color]

        just the socket.socket documentation, and generally for UDP protocol,
        see e.g. http://www-net.cs.umass.edu/kurose/transport/UDP.html

        Using it is very simple, on one computer, you send a (short) string,
        and on the other computer you receive the string (or it might be lost on
        the way). For your situation, I'd recommend to implement some sort of
        time constrain - e.g. if user moves cursor very fast, ensure that the
        coordinates are not transmitted with higher frequency that 50 Hz (or
        something). And combine the coordinates into one packet - it always
        helps to reduce the number of packets.
        [color=blue]
        > questions to your code, but don't want to flood here. OK, I will flood
        > here a bit: what's the print `` syntax?[/color]

        the same as repr, i.e. textual representation of a variable - good for
        debugging
        [color=blue]
        >
        > P.S. I loved your virus alert ;)
        >[/color]

        and you got infected I see :-)

        --
        -----------------------------------------------------------
        | Radovan Garabík http://kassiopeia.juls.savba.sk/~garabik/ |
        | __..--^^^--..__ garabik @ kassiopeia.juls .savba.sk |
        -----------------------------------------------------------
        Antivirus alert: file .signature infected by signature virus.
        Hi! I'm a signature virus! Copy me into your signature file to help me spread!

        Comment

        Working...