Skip to main content

Sneks engine

Project description

Sneks Engine

A Python game engine for creating snake-like games with customizable rules, graphics, and AI submissions.

Architecture

src/sneks/
├── engine/           # Core engine components
│   ├── core/         # Primitives: Cell, Bearing, Direction, Action
│   ├── entity/       # Game entities: Snek, Food, Static/Dynamic
│   ├── config/       # Configuration dataclasses
│   ├── manager/      # Cell and bearing calculations
│   ├── validator/    # Submission validation tests
│   ├── world.py      # Base World class (game loop)
│   ├── graphics.py   # Pygame rendering
│   ├── recorder.py   # Frame capture and video encoding
│   ├── runner.py     # Game execution and scoring
│   └── scoring.py    # Score normalization
└── games/            # Game variants
    ├── classic/      # Traditional snek
    ├── space/        # Asteroids + snek
    ├── toroidal/     # Infinite growth mode
    └── example/      # Example submission

Key Components

  • World: Manages the game loop, entities, and board state. Subclass this to create new game modes.
  • Entity: Base class for all game objects. DynamicEntity moves and grows; StaticEntity maintains shape.
  • Snek: The AI interface that submissions implement. Receives game state and returns an Action.
  • Config: Singleton configuration system. Each game mode extends Config with custom settings.

Creating a New Game Mode

  1. Create a config class extending Config:
from dataclasses import dataclass
from sneks.engine.config.base import Config


@dataclass(frozen=True)
class MyGameConfig(Config):
    title: str = "My Game"
    turn_limit: int = 5000
  1. Create a world class extending World:
from sneks.engine.world import World
from sneks.engine.scoring import Criteria


class MyGame(World):
    criteria = Criteria.AGE | Criteria.LENGTH  # Scoring criteria

    def __init__(self, config: MyGameConfig) -> None:
        super().__init__(config=config)

    # Override hooks as needed:
    # - load_entities(): Add custom entities
    # - before_step(): Pre-step logic
    # - after_step(): Post-step logic (collisions, growth)

Core Concepts

Bearing vs Direction vs Action

The engine uses a velocity-based movement model:

  • Bearing: Current velocity as Bearing(x, y) — cells moved per tick in each axis. A snek at Bearing(2, -1) moves 2 cells right and 1 cell down each tick.
  • Direction: Cardinal directions (UP, DOWN, LEFT, RIGHT) used for querying neighbors and obstacles.
  • Action: Acceleration input returned by get_next_action(). Actions modify the bearing — Action.UP increments bearing.y by 1.

Sneks start stationary at Bearing(0, 0). Returning Action.RIGHT twice results in Bearing(2, 0), moving 2 cells right per tick.

Toroidal Board

The game board wraps — moving off the right edge appears on the left. This affects:

  • Cell neighbors (e.g., Cell(0, 0).get_left() wraps to the rightmost column)
  • Distance calculations (shortest path may cross edges)
  • Collision detection

Relative Coordinates

Submissions receive positions relative to their head, which is always Cell(0, 0):

  • self.occupied contains obstacles offset from the head
  • self.food contains food positions offset from the head
  • self.body[0] is always Cell(0, 0) (the head)

This simplifies AI logic — you don't need to know absolute board position.

Vision Range

Sneks can only see obstacles within Config().snek.vision_range cells. Food is always visible regardless of distance.

Recording

Install the recording dependency:

pip install -e ".[record]"

Enable recording in your config:

from sneks.engine.config.graphics import GraphicsConfig

MyGameConfig(
    graphics=GraphicsConfig(
        record=True,
        record_prefix="./output",  # Output directory
        record_fps=12,             # Video framerate
    )
)

Frames are saved to output/pics/ during gameplay. Call recorder.animate_game() after the run to encode to MP4 in output/movies/. The encoder uses ffmpeg via imageio_ffmpeg.

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

sneks_engine-1.0.2.tar.gz (27.8 kB view details)

Uploaded Source

Built Distribution

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

sneks_engine-1.0.2-py3-none-any.whl (40.7 kB view details)

Uploaded Python 3

File details

Details for the file sneks_engine-1.0.2.tar.gz.

File metadata

  • Download URL: sneks_engine-1.0.2.tar.gz
  • Upload date:
  • Size: 27.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.28.1

File hashes

Hashes for sneks_engine-1.0.2.tar.gz
Algorithm Hash digest
SHA256 bc82dd73966e69bdeb0f741ffc4244953a9e85d6832b4d17ec63c90b19d5eb9f
MD5 bc4fcf30f0f8637f5b7bf5278e205dbd
BLAKE2b-256 91fb7830ee5157e1649baaad0a4314372a030ef803425ebcb14029d4aaa88c29

See more details on using hashes here.

File details

Details for the file sneks_engine-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: sneks_engine-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 40.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.28.1

File hashes

Hashes for sneks_engine-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 51cff8c00528e291a2b9e7cbcc7caf57c4c16095c76818be8fcae7f898c6ecb3
MD5 00cf410cd009c8009ee96a14cf15b4bb
BLAKE2b-256 4d9989e773a897315bf7540370de4effa65f5a2c5de27a7149fb7b1ea9026825

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