Skip to main content

A package for adding multiplayer functionality to python games.

Project description

pymultiplayer

A library for adding multiplayer functionality to python games.

Note

This is a work in progress. Don't expect all features to work. I don't know what version of python is needed for this package to work.

This is mainly just a passion project and a project to help future projects (in fact I'm using it right now in Elemental Defense) but it would be nice if it had some use.


Usage

A full example of this package's usage will be uploaded to iamdeedz.github.io eventually (if I get around to it)!


Installation

pip install pymultiplayer or python -m pip install pymultiplayer


Client Representation

The client is represented by a class with the following attributes:

class _Client:
    def __init__(self, ws, id):
        self.ws = ws
        self.id = id

Parameters

  • ws - The websocket connection. Used for sending messages with ws.send().
  • id - The client's id. Used for identifying clients.

The client class is not meant to be initialized by the user. It is created by the server and passed to the message handler function.


MultiplayerClient

class MultiplayerClient:
    def __init__(self, msg_handler, ip="127.0.0.1", port=1300, auth_handler=None):

TCPMultiplayerServer

class TCPMultiplayerServer:
    def __init__(self, msg_handler, ip="127.0.0.1", port=1300, auth_func=None):

Parameters

  • msg_handler - The function that gets ran for every message the server receives. Takes in the message as the first argument and the client as the second. REQUIRED
  • ip - The ip address the server will run on. Default is localhost or 127.0.0.1.
  • port - The port the server will run on. Default is 1300.
  • auth_func - If you want to use authentication, you can pass a function here. If an auth_func is given, it will be run every time a client connects. Takes in the websocket connection. Take a look at this example. The auth code is in server.py and authenticator.py.

Example

Full examples of these functions will be uploaded to iamdeedz.github.io in due course but for now, here's a basic example.

Let's start with the server.

First, create a file called server.py.

Now, create the required function(s).

# server.py

def msg_handler(msg, client):
    print(f"Received message from {client}: {msg}")

Then, create the server object.

# server.py

from pymultiplayer import TCPMultiplayerServer

def msg_handler(msg, client):
    print(f"Received message from {client}: {msg}")

server = TCPMultiplayerServer(msg_handler)

Next, run the server.

# server.py

from pymultiplayer import TCPMultiplayerServer

def msg_handler(msg, client):
    print(f"Received message from {client}: {msg}")

server = TCPMultiplayerServer(msg_handler)
server.run()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pymultiplayer-0.3.5.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pymultiplayer-0.3.5-py3-none-any.whl (9.7 kB view details)

Uploaded Python 3

File details

Details for the file pymultiplayer-0.3.5.tar.gz.

File metadata

  • Download URL: pymultiplayer-0.3.5.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.2

File hashes

Hashes for pymultiplayer-0.3.5.tar.gz
Algorithm Hash digest
SHA256 f343947b6de70f996ae25c20b4fa47a1c4ef317da11d6caac41903e237c80880
MD5 e6914b8c9a7b322d6851f2e44ee9e385
BLAKE2b-256 c2a04d4634323a7f5ec6b435564f54eb5671bb46e570be906bde268158b3ca01

See more details on using hashes here.

File details

Details for the file pymultiplayer-0.3.5-py3-none-any.whl.

File metadata

  • Download URL: pymultiplayer-0.3.5-py3-none-any.whl
  • Upload date:
  • Size: 9.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.2

File hashes

Hashes for pymultiplayer-0.3.5-py3-none-any.whl
Algorithm Hash digest
SHA256 7ea95efb9f3af0a60da18138c9e3854f37c5bfa59a0b7aca5b7403a1e908c675
MD5 5b6c2b786599b25c5941014950afb794
BLAKE2b-256 86b1ef337e6ee97289a909556fd6208502b91ef3f57ad5db7e9e7d0bc4a989ac

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page