Skip to content

Conversation

@gnodet
Copy link
Member

@gnodet gnodet commented Aug 7, 2025

Overview

This PR implements a classic Snake game for JLine3 as requested in issue #1317. The game demonstrates JLine3's advanced terminal UI capabilities using the Display class for fullscreen rendering. It is available directly as a demo: ./build demo snake.

Features

Game Features

  • Classic Snake gameplay with growing snake and food collection
  • Score tracking and collision detection (walls and self-collision)
  • Pause/resume functionality
  • Dynamic food spawning
  • Game over screen with final score display

Controls

  • Arrow keys or WASD for snake movement
  • P/Space for pause/resume
  • M to open the options menu
  • Q/Ctrl+C to quit game

Visual Design and Effects

  • Beautiful TUI with Unicode borders
  • Rainbow/gradient snake tail coloring on capable terminals
  • Sparkle effect when eating food
  • Colored walls and status line
  • Responsive to terminal resize events

Options Menu (press 'm')

  • Color Mode: Classic or Rainbow
  • Walls Mode: None, Border, Random
  • Difficulty: Slow, Normal, Fast
  • Wrap Around: On/Off

Technical Implementation

Architecture

  • Single class design: org.jline.builtins.Snake
  • Command integration: Commands.snake(...)
  • Demo main: org.jline.demo.SnakeDemo wired to ./build demo snake
  • Display-based rendering using JLine3's Display
  • Advanced input handling with BindingReader and KeyMap

Files Added/Modified

  • builtins/src/main/java/org/jline/builtins/Snake.java — main game, visuals, menu
  • builtins/pom.xml — ensure terminal dependency
  • demo/src/main/java/org/jline/demo/SnakeDemo.java — demo entrypoint
  • build — add demo wiring for snake

Usage

  • Run the demo: ./build demo snake
  • Or within an app: instantiate new Snake(terminal).run()

Testing and Build

  • Full build and Spotless formatting pass locally with Maven 4.0.0-rc-4 and JDK 22

This game both entertains and showcases JLine3 for fullscreen TUI apps, real-time input, and efficient rendering.

Fixes #1317

This commit implements a classic Snake game as requested in issue #1317.
The implementation provides a fully interactive terminal-based game using
JLine3's Display class for fullscreen rendering.

Features:
- Classic Snake gameplay with growing snake and food collection
- Score tracking and collision detection (walls and self)
- Pause/resume functionality with P/Space keys
- Arrow keys and WASD controls for movement
- Responsive terminal UI with colored borders and status display
- Proper signal handling for window resize and interrupts

Technical implementation:
- Single class design in builtins subproject (Snake.java)
- Uses Display class for efficient fullscreen updates
- BindingReader and KeyMap for advanced keyboard input handling
- AttributedString for colored output and visual styling
- Integrated as 'snake' command in Commands.java
- Includes demo script (snake-demo.jline) for interactive introduction

The game serves as both entertainment and a demonstration of JLine3's
advanced terminal UI capabilities, showcasing fullscreen applications,
real-time input handling, and efficient display updates.

Fixes #1317
@gnodet gnodet marked this pull request as draft August 7, 2025 18:21
gnodet added 5 commits August 10, 2025 15:06
…tions

- Wire Snake demo to build script and add SnakeDemo main class
- Add options menu (m) with color mode, walls mode, difficulty, wrap-around
- Add rainbow tail coloring and sparkle effect on eating
- Add walls (border/random/none) and wrap-around toggle
- Keep defaults compatible; formatting via Spotless; full build passes
…animation with bell, and persistent high score.\n\n- Visuals: head glow halo and crash ripple animation\n- UX: help overlay toggle (h), reset (r) without exiting, footer/help updates\n- Audio: terminal bell on eat and crash\n- Persistence: store/load high score with Preferences and display it (highlight when new)\n- Commands help text updated
…eption.\n\n- Game over: prompt [Y/n] (R=Reset, Q=Quit) and loop when accepted\n- Timing: reduce delay ~33% when moving horizontally to compensate cell geometry\n- Keep previous improvements (help overlay, reset, bell, high score)
This commit significantly enhances the Snake game with exciting new features:

New Power-Up System:
- Speed Boost (⚡): Doubles snake movement speed for 5 seconds
- Double Points (💎): Doubles food scoring for 10 seconds
- Shrink Snake (🔸): Removes 2 segments from snake tail
- Invincible Shield (🛡): Prevents collision damage for 5 seconds

Power-Up Mechanics:
- Random spawning every 10 seconds with 5% probability
- Visual indicators on game board with unique symbols
- Active effects displayed in header status bar
- Collision detection and automatic activation
- Limited to 2 active power-ups on board simultaneously

Enhanced Difficulty:
- Added EXTREME difficulty mode (50ms delay)
- Speed boost power-up provides dynamic gameplay acceleration
- Improved speed calculation for horizontal vs vertical movement

Achievement System:
- Track total power-ups collected across games
- Monitor maximum snake length achieved
- Speed Demon achievement for reaching length 20 on EXTREME mode
- Foundation for future achievement expansions

Visual Improvements:
- Power-up symbols rendered on game board
- Real-time status indicators in header
- Enhanced collision detection with invincibility effects
- Improved game state management and reset functionality

Technical Enhancements:
- Robust power-up lifecycle management
- Thread-safe power-up state tracking
- Optimized rendering with power-up overlays
- Clean separation of power-up logic from core game mechanics

This update transforms the Snake game into a more engaging and dynamic
experience while maintaining the classic gameplay that players love.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Snake 🐍

2 participants