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.
DynamicEntitymoves and grows;StaticEntitymaintains shape. - Snek: The AI interface that submissions implement. Receives game state and returns an
Action. - Config: Singleton configuration system. Each game mode extends
Configwith custom settings.
Creating a New Game Mode
- 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
- 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 atBearing(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.UPincrementsbearing.yby 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.occupiedcontains obstacles offset from the headself.foodcontains food positions offset from the headself.body[0]is alwaysCell(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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sneks_engine-1.0.5.tar.gz.
File metadata
- Download URL: sneks_engine-1.0.5.tar.gz
- Upload date:
- Size: 28.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.28.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f6e8e6dd48005897a4ff654a955a39813c85d9b1e9ae451020e67ed1b8499fc3
|
|
| MD5 |
5d031fbc84b6efe0aaf43e217cc7e76f
|
|
| BLAKE2b-256 |
840344da73d0970aac15a89ed82a2ec66fca6a919ccd732a728803b3c4c6297d
|
File details
Details for the file sneks_engine-1.0.5-py3-none-any.whl.
File metadata
- Download URL: sneks_engine-1.0.5-py3-none-any.whl
- Upload date:
- Size: 40.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.28.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
467c9877e1644ca8de8d99e566112a2a10d76c0dc04469b735b8511090d86b6b
|
|
| MD5 |
4f3b126c85d40154c022990b6f98c662
|
|
| BLAKE2b-256 |
902bb1012d6787ba0b15279d78378f80395ce5388ff0358065f3f120e8a5c1b4
|