An open API service indexing awesome lists of open source software.

https://github.com/douxxu/qr-snake

The tinyest snake game in shell. Fits in a qr code
https://github.com/douxxu/qr-snake

Last synced: 8 months ago
JSON representation

The tinyest snake game in shell. Fits in a qr code

Awesome Lists containing this project

README

          

# qr-snake
Hey, wanna play a simple snake game in your shell ? But where is the code ? It's easy. It's just here:

![snake](/snake.png)

Yeah it's a pretty big qr code, there are __1888 bytes__ (1.8kb) stored in ! It's the size of the optimized code.
You can use [zbarimg](https://github.com/mchehab/zbar) to extact the code and play it!

## Run snake
1. Download the qr code
2. If it's your first time, use
```
zbarimg -q snake.png | awk 'NR==1{print substr($0, 9)} NR>1{print}' > snake && chmod +x snake && ./snake
```
If you already did that, simply do
```
./snake
```

Human readable code

```bash
#!/bin/bash

# Initialize variables
score=0 # Score
width=30 # Width of the field
height=15 # Height of the field
snake_x=(10 9 8) # Snake's x-coordinates
snake_y=(7 7 7) # Snake's y-coordinates
direction=R # Current direction
fruit_x=$((RANDOM % width)) # Fruit's x-position
fruit_y=$((RANDOM % height)) # Fruit's y-position

# Function to update the snake's position
move_snake() {
local new_x=${snake_x[0]} # New x-position
local new_y=${snake_y[0]} # New y-position

# Update position based on direction
case $direction in
R) ((new_x++)) ;;
L) ((new_x--)) ;;
U) ((new_y--)) ;;
D) ((new_y++)) ;;
esac

# Handle border crossing
if ((new_x >= width)); then new_x=0; fi
if ((new_x < 0)); then new_x=$((width - 1)); fi
if ((new_y >= height)); then new_y=0; fi
if ((new_y < 0)); then new_y=$((height - 1)); fi

# Check for collision with the snake's body
for ((i=1; i<${#snake_x[@]}; i++)); do
if [[ ${snake_x[i]} -eq $new_x && ${snake_y[i]} -eq $new_y ]]; then
echo "Game Over! Score: $score"
exit
fi
done

# Update the snake's coordinates
snake_x=($new_x "${snake_x[@]}")
snake_y=($new_y "${snake_y[@]}")
snake_x=("${snake_x[@]:0:${#snake_x[@]}-1}")
snake_y=("${snake_y[@]:0:${#snake_y[@]}-1}")
}

# Function to display the field
display_field() {
clear
for ((j=0; j