Skip to content

A typescritp flexible, modern, and DOM-independent game engine

Notifications You must be signed in to change notification settings

lucabro81/snake-game-engine

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Snake Game Engine

A flexible and customizable Snake game engine written in TypeScript. This engine provides core game mechanics while allowing you to implement your own rendering logic.

Installation

pnpm add snake-game-engine

Features

  • Flexible rendering system - bring your own renderer
  • Customizable score system
  • TypeScript support out of the box

Basic Usage

import { Snake } from "snake-game-engine";

// Define game configuration
const gameConfig = {
  width: 20, // Grid width
  height: 20, // Grid height
  tickRate: 10, // Updates per second
  continuousSpace: false, // If true, snake wraps around edges
  scoreConfig: {
    foodMultiplier: 10,
    movementMultiplier: 1,
    useSnakeLength: true,
    onScoreUpdate: (score) => {
      console.log("Score updated:", score);
    },
  },
};

// Define how to render snake and food
const renderConfig = {
  cellSize: 20,
  snakeRenderer: (position) => {
    // Return your rendered snake segment
    // Example: return a DOM element, canvas context operation, etc.
  },
  foodRenderer: (position) => {
    // Return your rendered food
  },
  clearRenderer: (element) => {
    // Clean up rendered element
  },
};

// Create game instance
const game = new Snake(gameConfig, renderConfig, () =>
  console.log("Game Over!")
);

// Start the game
game.start();

// Listen for keyboard input
document.addEventListener("keydown", (event) => {
  switch (event.key) {
    case "ArrowUp":
      game.setDirection({ x: 0, y: -1 });
      break;
    case "ArrowDown":
      game.setDirection({ x: 0, y: 1 });
      break;
    case "ArrowLeft":
      game.setDirection({ x: -1, y: 0 });
      break;
    case "ArrowRight":
      game.setDirection({ x: 1, y: 0 });
      break;
  }
});

API Reference

Snake Class

Constructor

constructor(
  config: GameConfig,
  renderConfig: RenderConfig<T>,
  onGameOver: () => void
)

Methods

  • start(): Starts the game loop
  • stop(): Stops the game loop
  • setDirection(direction: Vector2D): Sets the snake's direction

Types

interface GameConfig {
  width: number;
  height: number;
  tickRate: number;
  continuousSpace: boolean;
}

interface RenderConfig<T> {
  cellSize: number;
  snakeRenderer: (position: Vector2D) => T;
  foodRenderer: (position: Vector2D) => T;
  clearRenderer: (element?: T) => void;
}

interface Vector2D {
  x: number;
  y: number;
}

Example Implementations

License

ISC

About

A typescritp flexible, modern, and DOM-independent game engine

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published