Python module providing simple game networking

Overview

nethelper

Python module providing simple game networking

This module was originally created to facilitate a class on creating multiplayer games in Pygame Zero. It designed to be simple for beginner programmers to use, and will not be as full featured or efficient as other networking libraries.

Features

  • Work over the internet or LAN
  • Designed specifically for game networking (...send and receive never blocks, messages are batched)
  • Very simple to use
  • No dependencies or installation. Just download and import one file.

How it Works

nethelper relies on a server to help relay messages between nodes (...players). This enables it to work across the internet without requiring each user to perform firewall / router configuration or share IP address. You can also run the server within your own LAN, but users outside of your LAN will likely not be able to connect to it.

Each player is a node, and nodes are organized into groups. This means that two students can play a pong game within a "pong" group, while another three students play a maze game within a "maze" group, all using the same server. There are no hardcoded limits to the number of groups, and each group can have up to 253 concurrent players.

Messages are encoded with JSON (...with a custom binary header), and transmitted via TCP to the message relay server. The message relay server will then relay the message to the intended recipient(s).

Each running game can send data to either a specific node within the same group, or to all nodes in the group. The former is useful for clients to send control commands to a game host, while the latter is useful for a game host to send the game state to all clients. See the example folder for examples of games using this pattern.

Starting the Server

For this module to work, you will require a message relay server. To start the server, simply run the module as a script.

python3 nethelper.py -g net_demo

This will start the server and configure it to recognize the "net_demo" group. Your game can use any string as a group name, as long as it is recognized by the server.

IMPORTANT : Your server must be accessible on its listening port (default to 65042). Read the section on "Server Access" for some tips on how to set this up.

You can also configure the server to recognize multiple groups.

python3 nethelper.py -g demo1 demo2 demo3

This will configure the server to recognize 3 groups; "demo1", "demo2", and "demo3". Games can only talk to others in the same group, so a game that's on "demo1" will not be able to talk to a game on "demo2".

You can also write your group names into a file and provide the filename on the command line.

python3 nethelper.py -f file_containing_groups.txt

Each group name must be on it's own line, and lines starting with # are ignored.

To see more options, run...

python3 nethelper.py -h

Game Setup

First, import the NetNode class.

from nethelper import NetNode

Next, create a NetNode object and connect to the message relay server (...make sure to start it first).

net = NetNode()
net.connect('localhost', 'foo', 'net_demo')

Now your game should be connected to the server as a node. Every node in the same group can talk to each other via the message relay server.

localhost is what I'm using here as the server address, but that will only work if the game is running on the same computer as the server. If the game is not running on the same computer as the server, you'll want to replace localhost with the IP address or domain name of the server.

foo is the name that this node is identifying itself as. You can use any string as a name, as long as every node in the same group has a unique name.

net_demo is the group that I am are connecting to. The server must be configured to recognize this group, and nodes can only communicate with other computers in the same group.

In your update() function, you should start with a...

net.process_recv()

This will receive all the incoming messages server, making them available to read.

To send a message to other nodes, use...

net.send_msg('bar', 'pp', players_pos)

This will send a message with the title pp to the node named bar. The content of the message will be the value of the variable players_pos.

To send to all nodes in the group, use ALL as the destination name.

net.send_msg('ALL', 'pp', players_pos)

To read incoming messages, use...

msg = net.get_msg('bar', 'controls')

This will get the content of the message titled controls from bar, and put it in the msg variable. If no messages are available, get_msg will return None.

Finally, you should end your update() function with a...

net.process_send()

This will push all the sent messages to the server. If you do not run process_send(), your messages will never get sent out.

One last useful function is net.get_peers(). This function will return a list containing the names of all nodes in your group.

More Documentations

See Wiki page on Github for detailed documentations.

The Wiki docs are generated from the docstrings in the nethelper.py source, so you can also read that.

Examples

As nethelper is designed for use with games, both of these examples uses Pygame Zero. However, nothing in nethelper requires Pygame Zero and it can also be used on its own without any other modules. A message relay server must be running and configured for group "net_demo" for the example to work.

Message sender:

import pgzrun
from nethelper import NetNode

net = NetNode()
net.connect('localhost', 'Tom', 'net_demo')

def update():
    if keyboard.up:
        net.send_msg('Jerry', 'controls', 'up')
    elif keyboard.down:
        net.send_msg('Jerry', 'controls', 'down')

    net.process_send()

pgzrun.go()

Message receiver:

import pgzrun
from nethelper import NetNode

net = NetNode()
net.connect('localhost', 'Jerry', 'net_demo')

msg = None
def update():
    global msg
    
    net.process_recv()
    msg = net.get_msg('Tom', 'controls')    

def draw():
    if msg:
        screen.clear()
        screen.draw.text(msg, (100,100), color="white")

pgzrun.go()

See the examples folder for more complete examples of games using nethelper.

Server Access

You can run the message relay server on either a computer within your own LAN, or on an internet server (...typically through a cloud service).

Computer within LAN

Running it on a computer within your own LAN can be faster and uses no internet bandwidth, but this may be blocked by your firewall or router. To open up access through the firewall, you need to allow incoming connections on TCP port 65042.

On Linux (Including Raspberry Pi)

On the command line, run...

sudo ufw allow 65042

On Windows

Visit this link for instructions https://www.tomshardware.com/news/how-to-open-firewall-ports-in-windows-10,36451.html

On Mac

Visit this link for instructions https://www.macworld.co.uk/how-to/how-open-specific-ports-in-os-x-1010-firewall-3616405/

Router Blocking Access

If the connection is being blocked by your router, you'll likely need to disable an option typically named "Wireless Isolation", "Client Isolation", or "Wireless client separation". There are many ways to configure a router to block client-to-client communications, so if that doesn't work, you'll need to consult your router manual or your network administrator for help. Alternatively, use an internet server instead.

Internet Server

Running the message relay server on the internet may be slower (ie. laggy), but will allow players on different networks to play together (eg. for an online class where every student is at their own home). It can also have less issues with network configurations. You will need to pay for a cloud hosting service, but these are not expensive (...typically 1 cent per hour).

Most cloud services including Amazon AWS, DigitalOcean, and Linode, will do just fine. The following instructions are based on DigitalOcean.

  1. Register for an account on DigitalOcean
  2. Create a new Droplet:
    • Choose Ubuntu as your distribution.
    • The cheapest plan should be sufficient for dozens of students.
    • Choose a datacenter region that is close to you.
    • For Authentication, a password will be sufficient (...no sensitive data will be stored on the server)
  3. Wait for your droplet to be ready (...this may take a few minutes), then login using an SSH client and your password.
  4. Download nethelper.py using the command:
wget https://raw.githubusercontent.com/QuirkyCort/nethelper/main/nethelper.py
  1. Open the port on the firewall using:
sudo ufw allow 65042
  1. Run nethelper.py (...see Starting the Server for details)
You might also like...
Inflitator is a classic doom and wolfenstein3D like game made in Python, using the famous PYGAME module.
Inflitator is a classic doom and wolfenstein3D like game made in Python, using the famous PYGAME module.

INFLITATOR Raycaster INFLITATOR is a raycaster made in Python3 with Pygame. It is a game built on top of a simple engine of the same name. An example

Parkour game made in Python with Ursina Game Engine along with ano0002, Greeny127 and Someone-github
Average Clicker Game (AVG) is a Python made game using tkinter

Average-Clicker-Game Average Clicker Game (AVG) is a Python clicker game not made with pygame but with tkinter, it has worker, worker upgrades, times

Ice-Walker-Game - This repository is about the Ice Walker game made in Python.

Ice-Walker-Game Ce dépot contient le jeu Ice Walker programmé en Python. Les différentes grilles du jeu sont contenues dans le sous-dossier datas. Vou

Adventure-Game - Adventure Game which is created using Python

Adventure Game 🌇 This is a Adventure Game which is created using Python. Featur

I automated the lumberjack game on telegram, by recognising pixels and using pyautogui module

Lumberjack Automated: @gamebot According to the official documentation, @gamebot is a demo bot for the Telegram Gaming Platform.` It provides some sam

Made with pygame. Multiplayer game using socket module and threading.
Made with pygame. Multiplayer game using socket module and threading.

Rock Paper Scissor made with python-pygame. Poorly made, as a beginner in programming. Multiplayer with server code and client code provided.

Snake game mixed with Conway's Game of Life
Snake game mixed with Conway's Game of Life

SnakeOfLife Snake game mixed with Conway's Game of Life The rules are the same than a normal snake game but you have to avoid cells created by Conway'

Lint game data metafiles against GTA5.xsd for Rockstar's game engine (RAGE)
Lint game data metafiles against GTA5.xsd for Rockstar's game engine (RAGE)

rage-lint Lint RAGE (only GTA5 at the moment) meta/XML files for validity based off of the GTA5.xsd generated from game code. This script accepts a se

Comments
  • NetNode.disconnect raises an TypeError.

    NetNode.disconnect raises an TypeError.

    Code: nethelper.py:448

    def disconnect(self):
            '''
            Disconnect from the server.
    
            Returns:
                None
            '''
            self._send_req(self.REQ_TYPE_DISCONNECT)
            self.socket.close()
    

    nethelper.py:493

    def _send_req(self, req_type, data):
            data = {
                'type': req_type,
                'data': data
             }
            self._send_frame(self.SERVER_ADDR, self.FRAME_TYPE_REQ, data)
    

    Error: TypeError: _send_req() missing 1 required positional argument: 'data'

    When the req_type is self.REQ_TYPE_DISCONNECT, data is never used. However, current code requires positional argument. I worked around by assigning 0 to data, but I think there is better solution.

    opened by km19809 2
Owner
Cort
Cort
A networking library for multiplayer games.

Aerics A networking library for multiplayer games. Getting Started Install Python Open cmd/terminal and type: pip install Aerics Examples Creating a

Yusuf Rençber 3 Jan 4, 2023
Wordle-Python - A simple low-key clone of the popular game WORDLE made with python and a 2D Graphics module Pygame

Wordle-Python A simple low-key clone of the popular game WORDLE made with python

Showmick Kar 7 Feb 10, 2022
Game-of-life - A simple python program to simulate and visualise the Conway's Game of life

Conway's game of life A simple python program to simulate and visualise the Conw

Dhravya Shah 3 Feb 20, 2022
This is a simple telegram bot for the game Pyal, a word guessing game inspired by Wordle

Pyal Telegram Bot This is a simple telegram bot for the game Pyal, a word guessing game inspired by Wordle. How does it work? Differently from the ori

Rafael Omiya 4 Oct 6, 2022
An all-inclusive Python framework for the Riot Games League of Legends API. We focus on making the data easy and fun to work with, while providing all the tools necessary to create a website or do data analysis.

Cassiopeia A Python adaptation of the Riot Games League of Legends API (https://developer.riotgames.com/). Cassiopeia is the sister library to Orianna

Meraki Analytics 473 Jan 7, 2023
AI that plays Flappy Bird Game using the python module NEAT.

Flappy Bird AI [NEAT] AI that plays Flappy Bird Game using the python module NEAT. Instructions Install Python Modules: pip3 install -r requirements.t

Abhisht 5 Jan 26, 2022
Turtle Road Crossing Game in Turtle(python module)

Turtle Road Crossing Game in Turtle(python module) In this project we have built a road crossin game in python with Object-Oriebted Programming. This

Jhenil Parihar 3 Jun 15, 2022
A Snake Game built by Python Turtle Module 🐍

Snake-Game A Snake Game built with Python Turtle Module ?? Icons made by Freepik from www.flaticon.com Intro Control the direction of snake by simply

Megan 1 Oct 24, 2021
The Turtle Race Game built in Python with Turtle module.

Turtle Race Game The Turtle Race Game built in Python with Turtle module. Installation If you don't have Turtle module on your computer. You can downl

Aytaç Kaşoğlu 1 Nov 9, 2021
A Game Engine Made in Python with the Pygame Module

MandawEngine A Game Engine Made in Python with the Pygame Module Discord: https://discord.gg/MPPqj9PNt3 Installation To Get The Latest Version of Mand

Mandaw 14 Jun 24, 2022