Basic networking with Pygame

后端 未结 5 1815
不知归路
不知归路 2021-01-31 19:32

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 on

5条回答
  •  鱼传尺愫
    2021-01-31 19:51

    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 https://github.com/dotcloud/zerorpc-python

    You need following solutions:

    • discovering other player(s) on (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 IO

    Above should still be called 'basic', you should really use some fancy networking library with idiomatic API.

    UPDATE:

    Essentially you need to expose network service (in it's own thread) that will push messages to Python's Queue, 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 sent 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 source position and velocity vector.

提交回复
热议问题