rsnaker 0.1.0

A good old retro Snake in terminal UI
Documentation
[![crates.io](https://img.shields.io/crates/v/nom_du_crate.svg)](https://crates.io/crates/rsnaker)
[![GPLv3 License](https://img.shields.io/badge/License-GPL%20v3-yellow.svg)](https://opensource.org/licenses/)
[![Last Commit](https://img.shields.io/github/last-commit/fromtherags/rsnake)](https://github.com/FromTheRags/rsnake/commits)
[![Build](https://github.com/FromTheRags/rsnake/actions/workflows/rust.yml/badge.svg)](https://github.com/FromTheRags/rsnake/actions/workflows/rust.yml)
[![Test](https://github.com/FromTheRags/rsnake/actions/workflows/test.yml/badge.svg)](https://github.com/FromTheRags/rsnake/actions/workflows/test.yml)
[![docs](https://img.shields.io/badge/docs-online-blue)](https://fromtherags.github.io/rsnake/rsnaker/index.html)

# Snake Game using Ratatui

It is a terminal-based snake game using the Ratatui crate for rendering.
![Terminal Output Menu](demo_images/snake_setup.png)
![Terminal Output Running](demo_images/snake_running.png)

## Features

- **Terminal UI**: Uses Ratatui for rendering a grid-based game.
- **Game Logic**: Manages snake movement, collisions, and scoring.
- **Multithreading**: Uses multiple threads for input handling, rendering at 60 FPS, and game logic execution.
- **Emoji-based graphics**: Supports rendering the snake using emojis instead of ASCII.
- **Configurable parameters**: With `clap` for command-line arguments & toml file.

## TODO

- [ ] Add a save score (local db) with a pseudo got from cmdline
- [ ] Add some performance log with tracing,
  for [example]https://github.com/ratatui/ratatui/blob/main/examples/apps/tracing/src/main.rs
- [ ] Show game options in the menu, and visually change them
- [ ] Enhance fruits eaten detection and grid management with multiple emojis as body
- [ ] Manage mouse/touchscreen mouvement to control the snake
- [ ] Internal code: Provide a macro exemple for trait implementation widget and this error use.

## πŸ’– Support

- If you like this game and want to contribute to more amazing features, consider giving me a coffee
  on [kofi]https://ko-fi.com/retrosnake
  πŸ₯€to support development.

## Installation

### βœ… Prerequisites

- πŸ¦€ **Have Rust and compilation tools installed**  
  If you don't have Rust yet:
- On **Windows**, Install Rust using the official .exe installer https://www.rust-lang.org/tools/install (as it works
  Out-Of-The-Box on
  windows)
- On **Linux** See  
  πŸ‘‰ [Installation Rust and tools for Linux]#Installation-Rust-and-tools-for-Linux for instructions
- On **Android** use a linux emulator and follow same instruction as linux, tested with:
    - [Android using Ubuntu]https://github.com/CypherpunkArmory/UserLAnd,
    - [Termux]https://github.com/termux/termux-app
      For easier use, END key works as ENTER and HOME as a pause key.

- πŸ’» **Use a terminal that supports emoji**
    - On **Windows**, the default terminal supports emoji out of the box.
    - On **Linux** / Android, if need install the Noto Emoji font.
      See  
      πŸ‘‰ [Emoji font support]#Enable-Emoji-Font-Support for instructions.

### For this project

- Clone this repository
  `git clone https://github.com/FromTheRags/rsnake.git`
- Go to the directory `cd rsnake`

### Run the Game

- To run the game, either:`cargo run` or `cargo run --manifest-path rsnake/Cargo.toml` (if in another directory)
- To see run options, use: `rsnake --help`
- E.g., `rsnake -z 🐼 -b πŸ₯` or `cargo run -- -z 🐼 -b πŸ₯`
- To save a set of commands, create and alias or use `--save` to generate to a snake_config.toml file near the
  executable.
- Then load with `--load` option. [Shipped one]snake_config.toml
- To install the game as a command:  
  `cargo install --path .`  
  And then run the game with: `rsnake`

## Architecture

- Uses `Arc` & `RwLock` for synchronization.
- Spawns separate threads for input handling, rendering (60Hz), and game logic execution.

## Documentation generation

- `cargo doc --document-private-items --no-deps --open`

## Tests

- As usual run them with `cargo test` the project is set up with a lib containing all the code, and a main.rs just
  calling it
- As this is a widespread pattern providing full compliance with the Rust test ecosystem, allowing doc comment to be
  automatically tested, for example.
- To have a coverage report, install `llvm-cov`:
  `rustup component add llvm-tools-preview
  cargo install cargo-llvm-cov`
- And run `cargo llvm-cov --open`
- A great coverage is not a goal for this project (tests are only there to showcase tests in rust),
- For reference the current coverage
  is :[![codecov]https://codecov.io/gh/FromTheRags/rsnake/graph/badge.svg?token=XJXII0CMQF]https://codecov.io/gh/FromTheRags/rsnake
  [![codecov]https://codecov.io/gh/fromtherags/rsnake/branch/main/graph/badge.svg]https://codecov.io/gh/FromTheRags/rsnake

## Installation Rust and tools for Linux

Make sure your system has `curl`, `gcc` and `git` installed:

```bash
sudo apt update
sudo apt install curl git gcc -y
```

Use the official installer `rustup`, or any alternative method on https://www.rust-lang.org/tools/install (by your own):

```bash
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
```

- Follow the prompts in the terminal.
- After installation, run:

```bash
echo "source '$HOME/.cargo/env'" >> ~/.bashrc
source ~/.bashrc
```

- Verify the installation:

```bash
rustc --version
```

---

## Enable Emoji Font Support

To properly display emoji characters in your terminal and system fonts, install an emoji-compatible font.

### For Ubuntu/Debian-based distros:

```bash
sudo apt install fonts-noto-color-emoji
```

### For Arch Linux:

```bash
sudo pacman -S noto-fonts-emoji
```

### For Fedora:

```bash
sudo dnf install google-noto-emoji-color-fonts
```

---

## Optional: Configure Font Fallback (if emojis still do not render)

Create or edit the following file:

```bash
~/.config/fontconfig/fonts.conf
```

Add:

```xml
<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<fontconfig>
    <alias>
        <family>sans-serif</family>
        <prefer>
            <family>Noto Color Emoji</family>
        </prefer>
    </alias>
</fontconfig>
```

Then refresh the font cache:

```bash
fc-cache -f -v
```

---

## Test Your Setup

Run:

```bash
echo "Rust is awesome! πŸ¦€πŸ”₯πŸš€"
```

You should see emojis rendered correctly in your terminal or text editors.

- Then follow quick installation instructions

## References

- Clippy lints: <https://github.com/rust-lang/rust-clippy/>
- Ratatui tutorial: <https://ratatui.rs/tutorials/hello-world/>
- Example: <https://ratatui.rs/examples/widgets/canvas/>
- Git over-bloated: <https://rtyley.github.io/bfg-repo-cleaner/>

[![Contributors](https://img.shields.io/github/contributors/fromtherags/rsnake)](https://github.com/fromtherags/rsnake/graphs/contributors)