Skip to content

PavelFalta/udp_multiplayer_game

Repository files navigation

Networked Arcade Shooter - UDP Multiplayer with Protobuf & Pymunk

A fast-paced multiplayer arcade game built using Python's Arcade library, featuring real-time UDP network communication, physics-driven gameplay via Pymunk, and an intuitive ImGui-based interface. This project demonstrates a robust client-server architecture with several optimizations to ensure low latency and efficient data transfer. Key features include personalized game state updates, selective object transmission based on proximity, UDP networking, and the use of Protocol Buffers with zstandard compression.


demo.mp4

Table of Contents


Overview

This repository contains a networked arcade shooter that pits players against each other in a physics-driven multiplayer arena. The project is divided into two main components:

  • Client:
    Built with Arcade and ImGui, the client handles user input, renders players, bullets, and the game world, and manages interactive UI elements such as the chat window, minimap, and leaderboard.

  • Server:
    The server processes player inputs, simulates physics (movement, collisions, bullet dynamics) using Pymunk, and broadcasts personalized game state updates via UDP. It also maintains an in-game leaderboard and chat system, ensuring that each player receives only the information relevant to their immediate surroundings.


Installation & Setup

  1. Clone the Repository:

    git clone https://github.com/PavelFalta/udp_multiplayer_game.git
    cd udp_multiplayer_game
  2. Set Up a Virtual Environment 🤓:

    python -m venv venv
    source venv/bin/activate
  3. Install Dependencies:

    pip install -r requirements.txt

Usage

Running the Server

Start the game server to handle player connections, physics simulation, and game state updates:

python server.py

Running the Client

Launch the client to join the multiplayer arena:

python client.py

Project Structure

Client Components

  • classes.py
    Contains core classes for game objects and UI:

    • Entity System:
      The Entity class is the foundation for all game objects, extended by Player and Bullet classes. These classes implement smooth interpolation through buffered positions.
    • GUI & Chat:
      Manages the chat system (ChatWindow), message classes (PlayerMessage, Announcement), and UI elements such as the minimap and customization panels using ImGui.
    • Terrain:
      Handles the drawing of game boundaries and static obstacles. Unused as of now.
  • client.py
    The client entry point:

    • NetworkManager:
      Manages UDP communication, sending player inputs to the server and receiving zstandard-compressed game state updates.
    • Game Loop & Rendering:
      Sets up the Arcade window, camera movement, and integrates ImGui for a dynamic user interface.
    • Input Handling:
      Captures keyboard and mouse events to update the player's state and relay input data to the server.

Server Components

  • server.py
    Implements the game server:
    • Game Physics:
      Uses Pymunk to simulate realistic physics, managing player movement, bullet trajectories, and collision detection.
    • Player Management:
      Processes incoming player inputs, manages connections/disconnections, handles respawns, and applies health/damage logic.
    • Network Communication:
      Utilizes UDP and multithreading to concurrently handle player inputs and send personalized, compressed game state updates.
    • Leaderboard & Chat:
      Maintains a dynamic leaderboard and message buffer to broadcast game events and chat messages to players.

Protocol Buffers

  • game_pb2.py
    Auto-generated code from the Protocol Buffers definition file (.proto). It defines message structures such as:
    • Messages: PlayerInput, Player, Bullet, GameState, ServerMessage, and LeaderboardEntry.
    • Serialization:
      Provides fast serialization and deserialization for real-time communication between client and server.

Configuration & Dependencies

  • imgui.ini
    Configuration file for ImGui window positions and sizes, ensuring a consistent UI layout.
  • requirements.txt
    Lists all required packages, including:
    • arcade, arcade-imgui, pymunk, imgui, protobuf, zstandard, and more.

Optimizations & Performance Enhancements

Personalized Game State Updates

  • Tailored Updates:
    Each player receives a personalized game state update. In the server's serialize_game_state method, the current player's position is used to filter out distant objects. This ensures that only relevant objects (e.g., players, bullets) within a specific distance are included in the update, reducing unnecessary data transmission.

Selective Object Transmission

  • Distance Threshold Filtering:
    The server only sends objects that are within a defined distance (determined by SEND_DISTANCE_THRESHOLD) of each player. This optimization significantly reduces bandwidth usage and client processing load, especially in large maps where distant objects are irrelevant to the player's immediate gameplay.

Efficient Networking with UDP

  • Low-Latency Communication:
    By using UDP (via socket.SOCK_DGRAM), the game minimizes network overhead compared to TCP. Although UDP does not guarantee delivery, the game design compensates by sending frequent state updates (at 60 FPS), ensuring that lost packets are quickly replaced by new ones.
  • Reduced Connection Overhead:
    UDP's connectionless nature eliminates the latency introduced by TCP’s connection setup and maintenance, making it ideal for fast-paced real-time gaming.

Protocol Buffers & Compression

  • Compact, Schema-Based Messages:
    The use of Protocol Buffers allows for fast, efficient, and structured serialization of game data. This minimizes message size and parsing time.
  • Zstandard Compression:
    Before sending game state updates, messages are compressed with zstandard. This further reduces the data footprint on the network, ensuring rapid transmission even when game state messages are large.
  • Fast Deserialization:
    On the client side, decompression and deserialization occur swiftly, keeping the rendering loop smooth and responsive.

Threaded Networking & Locking

  • Concurrent Processing:
    Both client and server employ multithreading. For example, the client’s network manager runs a dedicated thread for receiving game state updates, ensuring that network I/O does not block rendering or input handling.
  • Thread Safety:
    Locks (e.g., in the server’s game state update and leaderboard management) ensure that shared data is safely accessed and modified across threads, maintaining data integrity without sacrificing performance.
  • Decoupled Game Logic:
    Network communication is separated from game logic and rendering. This decoupling guarantees that high-frequency physics and rendering updates (60 FPS) are not impeded by network latency or variability.

Cool Stuff(s)

Real-Time Network Communication

  • UDP & Compression:
    The game leverages UDP for low-latency communication. Game state messages are compressed using zstandard and serialized with Protocol Buffers to maximize efficiency.
  • Threaded Networking:
    Multithreading separates network I/O from core game logic, ensuring smooth performance even under heavy network load.

Physics & Collision Handling

  • Pymunk Integration:
    The server uses Pymunk to simulate realistic physics, managing movement, damping, and collision detection for players and bullets.
  • Bullet Collision Detection:
    Bullet collisions are detected using segment queries, ensuring that impacts are accurately registered and damage is applied in real time.

Dynamic UI & Chat System

  • ImGui-Based Interface:
    The client employs ImGui (via arcade-imgui) to render interactive UI panels, including:
    • A Chat Window for real-time messaging.
    • A Leaderboard displaying top players and scores.
    • A Minimap providing a top-down view of the arena.
    • A Customization Panel to adjust player name and color.
  • Interactive Chat:
    Double-clicking on chat messages allows players to quickly reuse text, enhancing in-game communication.

Customization

  • Personalization:
    Players can customize their in-game appearance by modifying their name and color through the built-in customization panel.

About

Arcade UDP multiplayer shooter showcasing networking/game optimalizations.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages