A TypeScript implementation of the classic Snake game using HTML5 Canvas.
- Node.js and npm installed
- Modern web browser
npm installnpm run buildThis compiles snake-game.ts to dist/snake-game.js.
npm run startThis builds the TypeScript code and opens the game in your default browser.
Alternatively, you can:
- Run
npm run buildto compile the TypeScript - Open
index.htmlin your browser manually
├── snake-game.ts # Main game logic (Snake, Food, Game classes)
├── index.html # HTML page with canvas and UI
├── dist/
│ └── snake-game.js # Compiled JavaScript
├── tsconfig.json # TypeScript configuration
├── package.json # Project configuration and scripts
└── README.md # This file
- Arrow Keys or WASD: Move the snake
- ↑/W: Move up
- ↓/S: Move down
- ←/A: Move left
- →/D: Move right
- Space: Restart game after game over
- The snake starts moving automatically
- Guide the snake to eat the red food squares
- Each food eaten increases your score by 10 points
- The snake grows longer with each food eaten
- Avoid hitting the walls or the snake's own body
- Game ends when you collide with walls or yourself
- Starting score: 0
- Points per food: 10
- Score is displayed at the top of the game
- Smooth snake movement with collision detection
- Food respawning in random positions
- Score tracking
- Game over detection with restart option
- Responsive controls with direction validation
- Clean TypeScript code with proper typing
The game uses a 20x20 grid system with each cell being 20x20 pixels on a 400x400 canvas. The game runs at 10 FPS (100ms intervals) for classic snake game timing.
Snake: Handles snake movement, growth, and collision detectionFood: Manages food positioning and respawningSnakeGame: Main game controller with update loop and rendering
- Edit
snake-game.tsfor game logic changes - Run
npm run buildto compile - Refresh the browser to see changes
This game was completely written using OpenCode and Big Pickle as the model.
Prompt 1
Write me the code for a snake game in typescript.
Followed the instruction and the game did work. Ran another prompt.
Prompt 2
The game is frozen, only showing a box and score.
The game works. I ran one more prompt.
Prompt 3
Create a readme file to show how to update, compile, and run the application. Also, include how to play the game.