Network Support for Pygame - python

I am making a simple multiplayer economic game in pygame. It consists of turns of a certain length, at the end of which, data is sent to the central server. A few quick calculations are done on the data and the results are sent back to the players. My question is how I should implement the network support. I was looking at Twisted and at Pyro and any suggestions or advice would be appreciated.

Twisted would certainly be a good idea. Here is example code that integrates twisted and pygame.

I've nothing against Twisted and PyRo, but the sort of simple messages you're going to be sending don't require anything like that and might be overcomplicated by using some sort of framework. Pickling an object and sending it over a socket is actually a very easy operation and well worth trying, even if you do eventually go with a more heavyweight framework. Don't fear the network!

There are a number of plug-and-play libraries tailored specifically to work nicely with PyGame on the pygame.org website.
These include PodSixNet, PygLibs.net, and my own Mastermind (which is, at the risk of self-aggrandizement, lightweight, easy to use, and comes with a simple tutorial).

Related

"Registration disabled." error (Pygame): Why can't I make a Pygame account? [duplicate]

I'm currently having trouble finding resources for network game programming(in Python specifically; I don't know any other languages). I have found a lot of things about general networking in Python but I'm not sure that's what I need(as I believe game networking has some other factors involved). I'm trying to create a 2 player LAN game that is played on separate computers. I have seen the Twisted python library suggested by some and scorned by others so I figure I should just leave it alone. I'm very confused about where or how to start the creation of this game and any help would be appreciated. (I have read a similar post to this but it didn't help me very much, I did read the creation of Client/Server model and Client side prediction though so I have a VERY BASIC idea of how it should work).
Thank you for reading!
P.S. I don't know if this helps any but the game is supposed to be a 2D-Arena Fighter and using PyGame library.
This is a late answer, but it should give people looking for python networking libraries and guides for games a good overview.
pygase
This is a library I wrote myself for various indie projects, because no other libraries met all my requirements. It's well documented and quite opinionated. It takes into account most learnings in terms of game networking that have been made in the past decades and the API is rather high-level. It's also actively developed and maintained by me.
podsixnet
A really well-designed low-level game networking API that should work for any kind of game. You still have some decisions to make architecture-wise that require a deeper understanding of game networking.
simple-game-server
Not really a library, more a ready-made server that organizes "rooms" of players, that can exchange messages directly. It's a simple concept that only really works for small games that are not too twitchy.
If you want some deep and practical knowledge about game networking, look here. Together with the docs for the socket and SocketServer packages this should enable anyone to write great networking code for games in python.
Cheers, Silas
I have actually done exactly that in Python. I used Pygames and made an arena fighter: https://github.com/mthsrobot30/Ship-Fighters.
There weren't any specific technologies that I used except for the standard sockets library.
The model that I used was that I had a thread running off the side that accepted keypresses from the other player from sockets and updated the screen based on it. Then, the local player would not only update the screen, but send his keypresses through the same socket.
I would recommend reading the sockets tutorial. I found it very hard to coordinate directly, so setting up a basic server that does nothing but relays the information would be a good idea for matching the players up with each other.

Python too slow with network [duplicate]

I need to do some basic networking for a Pygame project.
Basically, it's a 2D single player or cooperative game. The networking only needs to support two players, with one as a host.
The only information that needs to be sent is the positions of players, creeps and bullets.
I've been reading around and Twisted keeps coming up, but I haven't done networking before, and I'm not sure if that might be an overkill.
So, is it possible for a relative newbie to implement networking in Pygame?
This was asked recently on Reddit, so I'll more or less just copy my answer over from there. I apologize for not being able to provide more links, I have <10 rep so I can only post two at a time.
Twisted might work, but I don't have a whole lot of experience with it. I'd recommend going with sockets, as that's what Twisted uses in the background anyway. Beej's guide (google it) is pretty much the Holy Bible of sockets if you want to learn how they work (in C++, but the concepts extend everywhere). Python does abstract some of the complexity away, but it's still a good idea to know what's going on in the background.
For Python specific sockets, you can go ahead and just use the howto (user745294 posted a link above). Here's a nice article titled "What every programmer needs to know about Game Networking". It goes into the different types of major networking styles (client-server, p2p, udp v. tcp, etc.) and the history behind what some major games used for their networking.
Below is a link to a demo I did on making a networked "game" in Python 2.6/Pygame. It's not actually a game, but each client you create connects to the server and controls a character. You can move your character with the arrow keys and the character will move on all connected clients. I tried commenting the source code with some indication of what I'm sending back and forth, but you may need a little knowledge about sockets to understand it.
The source code is provided in the codepad links in the comment below this post. You will need to provide two images in the same directory as the scripts:
bg.png is the background sprite. It should be an image 400px wide and 300px tall (this can be changed in the GameClient class if needed)
sprite.png is the player character. It should be smaller than the background so that you can see it moving around.
You can use Twisted for networking with Pygame. The "game" project on Launchpad has some examples of how one might integrate the main loops together; basically, use twisted.internet.task.LoopingCall to draw Pygame frames and handle input, while letting the Twisted reactor of your choice run normally.
Since you are already using Pygame, I think this light networking library made for Pygame will do what you need and teach you, but not overwhelm you.
"Mastermind Networking Lib" via pygame.org
There is Pyro (Python remote objects) as another solution for networking in Python.
http://irmen.home.xs4all.nl/pyro/
Using raw sockets is low-level and full of danger. As said before, Twisted is complex and takes to time get up and running. To save yourself some headaches I'd try something like zerorpc.
You need the following solutions:
discovering other player(s) on the (local) network, you don't want player to enter some IP address
handle network errors
serialize messages containing your data (positions, player name, etc.)
handle threading as networking is asynchronous I/O
Above should still be called 'basic', you should really use some fancy networking library with idiomatic API.
Essentially you need to expose the network service (in its own thread) that will push messages to Python's Queue, and then access this same queue from your Pygame code, and if there is a message then you update whatever structures you use to store player's position and draw it on screen.
You shouldn't send stuff like bullet positions over the network as they can be easily (and faster) calculated locally. You just send an event like bullet_shot over the network with a source position and velocity vector.

Python Twisted or Concurrence Programming for Learning Sockets/Networking

Well this is my last ditch effort of finding help/guidance. We've only just started learning Python at university and i really want to get a jump on everyone else and i've really been focusing on networking with Python. I've had a look at the twisted framework and it's massive ! even the krondo.com site is.
I've seen Concurrence for python on LinuxQuestions a couple of times, which seems easier (however it looks like Concurrence has been dis-continued?), and was wondering if im better off knuckling down and really getting into twisted or maybe trying something else.... Its not that im afraid of hard work, but i dont want to go down a path that when im finished, i find out that know one use's it out in the real world, or knows of it.
Im sorry its not a technical question, but this is really the only place where i know i can get proper guidance. If you think putting my head down and learning twisted will be worth my while, then ill do that. Thanks in advance for your time.
Honestly, I would take a step back on frameworks and work with the Python standard library on a more basic level. The python docs are a very good and authoritative source of information on Python. If you're looking into networking, here's a path I'd recommend looking over:
Sockets
Learn how to work with basic socket connections, making clients, and making servers.
SSL
A lot of web services use this to communicate data securely for APIs. It may be difficult to wrap your head around, but it's a highly valuable piece of knowledge to have.
Select
One way of handling multiple clients connecting to a service. This gets into the concurrency realm which is a fairly sought after skill.
Threads
Another way of handling multiple connections, networking tasks.
Protocols
Now that you've wrapped your head around basic networking, it's time to get into protocols. These are sending specifically formatted data over the network to a server that can handle such data. Yes, I'd recommend going through all the different protocols.
Data Parsing
Data that is sent and received during different protocol communication can be encoded in various ways to make it easier to deal with data.
Pack and Unpack
Sometimes network communication isn't done with a standard format. These functions are helpful in dealing with binary protocols if there's no library available.
Pickle
This is some advanced stuff and python specific. You can use it to send python objects over the network. Just be careful to do this on trusted networks only for security purposes.
With that in mind these are general recommendations. Check the docs out, write some code, and ask questions if you get stuck.

In asyncore how can i send data to all or some of the clients?

I'm building a little MMORPG and im trying to use asyncore rather then threading.
1) How would i send data to certain clients, because in threading a saved each clients socket and current in a dictionary a with its unique id as a key. So how could i subjective send data to all the clients.
Thankyou, please say if i didnt give enough information
I find it great that you want to move to asynchronous programming instead of threading, since it is so much reliable and easier to debug.
However, asyncore is a bad library to do so. I don't advise you use it at all, since it requires a significant rewrite to do simple things like read standard io.
I suggest you move to twisted - it is a great asynchronous framework, well tested and developed, with good api documentation and good community support.
Regardless of your decision on which library to use, I find this series of blog posts by Dave Peticolas to be a great source of beginner information on asynchronous programming. Please read it.
There are some games under development using twisted. One example is Minions of Mirth - I never played it but it seems cool.
There's also divmod's imaginary - it's a simulationist's take on the realm of role playing, interactive fiction, and multiplayer dungeons. It incorporates gameplay features from each area while attempting to provide a richer environment than is generally available from existing systems.
Hope I helped.

Why is there a need for Twisted?

I have been playing around with the twisted framework for about a week now(more because of curiosity rather than having to use it) and its been a lot of fun doing event driven asynchronous network programming.
However, there is something that I fail to understand. The twisted documentation starts off with
Twisted is a framework designed to be very flexible and let you write powerful servers.
My doubt is :- Why do we need such an event-driven library to write powerful servers when there are already very efficient implementations of various servers out there?
Surely, there must have been more than a couple of concrete implementations which the twisted developers had in mind while writing this event-driven I\O library. What are those? Why exactly was twisted made?
In a comment on another answer, you say "Every library is supposed to have ...". "Supposed" by whom? Having use-cases is certainly a nice way to nail down your requirements, but it's not the only way. It also doesn't make sense to talk about the use-cases for all of Twisted at once. There is no use case that justifies every single API in Twisted. There are hundreds or thousands of different use cases, each which justifies a lesser or greater subdivision of Twisted. These came and went over the years of Twisted's development, and no attempt has been made to keep a list of them. I can say that I worked on part of Twisted Names so that I would have a topic for a paper I was presenting at the time. I implemented the vt102 parser in Twisted Conch because I am obsessed with terminals and wanted a fun project involving them. And I implemented the IMAP4 support in Twisted Mail because I worked at a company developing a mail server which required tighter control over the mail store than any other IMAP4 server at the time offered.
So, as you can see, different parts of Twisted were written for widely differing reasons (and I've only given examples of my own reasons, not the reasons of any other developers).
The initial reason for a program being written often doesn't matter much in the long run though. Now the code is written: Twisted Names now runs the DNS for many domain names on the internet, the vt102 parser helped me get a job, and the company that drove the IMAP4 development is out of business. What really matters is what useful things you can do with the code now. As MattH points out, the resulting plethora of functionality has resulted in a library that (perhaps uniquely) addresses a wide array of interesting problems.
Why do we need such an event-driven library to write powerful servers when there are already very efficient implementations of various servers out there?
So paraphrasing: you can't imagine why anyone would need a toolkit when dyecast products already exist?
I'm guessing you've never needed to knock up a protocol gateway, e.g.
- write a daemon to md5 local files on demand over a unix socket
- interrogate a piece of software using udp and expose statistics over http.
I wrote a little proof-of-concept for the second example for a question here on SO in a handful of minutes. I couldn't do that without twisted.
Have you looked at: ProjectsUsingTwisted?
More on 'why': (disclaimer: I'm not a developer of Twisted proper), it's necessary to consider Twisted's high age (relative to Python's). When Twisted was written there was no sufficiently powerful non-blocking network/event driven library written around the reactor pattern (almost everyone was using threads back then). Twisted's initial use case was a large multiplayer game, although the specifics of this game seems to be somewhat lost in time.
Since the origins, as #MattH's link suggest, a very large amount of various network servers written in Python is based on Twisted.
This PyCon talk by the creator of Twisted should give you answers.
It has changed my opinion of Twisted. Before I viewed it as a massive piece of software with interfaces and weird names, two things that many developers dislike but that are actually just superficial things, and now that I’ve seen the history behind and the amazing number of use cases I respect it a lot. Life is short, you need Twisted :)

Categories

Resources