Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Basic networking with Pygame

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 2 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? Can anyone point me in the right direction?

like image 367
Lightbreeze Avatar asked Apr 01 '12 03:04

Lightbreeze


People also ask

Can you make multiplayer games with pygame?

Developed a multiplayer multithreaded tic-tac-toe game in python3 using the pygame module along with the socket and threading library. This was done as the final project of the Operating Systems course.

Is pygame a beginner?

It simplifies the whole game designing process and makes it easy for beginners who want to develop games. The process of installing pygame in Python is straightforward. It can be done with the pip command, and the module can be imported into your program with the help of the import command.

What can pygame be used for?

Pygame is a cross-platform set of Python modules designed for writing video games. It includes computer graphics and sound libraries designed to be used with the Python programming language.


3 Answers

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:

  1. 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)
  2. sprite.png is the player character. It should be smaller than the background so that you can see it moving around.
like image 75
nemec Avatar answered Sep 28 '22 15:09

nemec


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.

like image 26
Glyph Avatar answered Sep 28 '22 17:09

Glyph


Since you are already using Pygame I think this light networking lib made for Pygame will do what you need and teach you but not overwhelm you. "Mastermind Networking Lib" via pygame.org

like image 33
infamousjeff Avatar answered Sep 28 '22 15:09

infamousjeff