snake game implementation using 2d array in Go

Overview

Snake Game Implementation

Snake game implementation in Go using a 2-dimensional array.

Demo

snaky

Install

download the package

  • git clone https://github.com/twiny/snaky
  • cd snaky/

run the game directly using the Go command

  • go run cmd/launcher/main.go -x 30 -y 15 -s slow

or build it

  • go build -o snaky -v cmd/launcher/main.go
  • chmod +x ./snaky

Command usage

  • ./snaky -h print help message.
  • -x game grid width
  • -y game grid height
  • -s game speed (string default: medium) - slow|medium|fast

How to play

  • keyboard arrow key changes Snake direction.
  • S Key starts the game.
  • P Key pauses the game.
  • R restart the game.
  • ESC or Ctrl+C exit the game.

Implementation

The snake game logic:

  • The snake keeps on moving in a straight line unless the direction is changed.

  • If the snake ate the food it grows. then a new food will be placed on the grid. the new position must not be on the snake body nor the same as the previous one.

  • If the snake bites itself or hits the wall, the game ends.

The snaky game consist of two main structures:

1 - Game struct

Game struct that represents the current game. it contains Snake, Food & Board structs

  • Board holds the current game information:
  • Grid is a 2d array. where snake body and food will be marked/mapped for rendering
  • Cell is the coordinate of a point on the grid
  • Icon used to paint the Cell when rendering board.

2 - Renderer interface

A Renderer is an interface that renders the game Board, and it consists of two methods.

  • Listen(chan Event) error: this listens to user inputs and sends them to a channel of Event.
  • Render(b *Board) error: this renders a game Board and prints it to the user. This way, renderer can be easily replaced.

The renderer range the grid Cell by Cell and depends on its value (Icon) it either print Head, Body, or Food.

TODO

  • fix case food is generated on snake body.
  • snake auto move forward.
  • fix snake moving direction.
  • change food icon.
  • fix when snake hit itself or borders.
  • show a message to the user when the game ends/restarts.
  • initially the snake is stopped. disable auto-start.
  • add more test cases.
You might also like...
MazePlay - a game powered by Go+ spx game engine
MazePlay - a game powered by Go+ spx game engine

MazePlay - a game powered by Go+ spx game engine How to run Download Go+ and build it. See https://github.com/goplus/gop#how-to-build. Download this g

A simple game that I created with Ebiten game library as a way to teach myself Go. Enjoy!
A simple game that I created with Ebiten game library as a way to teach myself Go. Enjoy!

galactic-asteroid-belt Overview A simple game that I created with Ebiten game library as a way to teach myself Go. Enjoy! Run To run, you will need Go

RundQuiz-Game - This is a Go exercise that implements and builds a quiz game from a list of math questions in a CSV file.

Go RundQuiz Game Exercise details This exercise is broken into two parts to help simplify the process of explaining it as well as to make it easier to

Simple 2D game to teach myself various things about game development and ECS, etc

2d-grass-game I really don't know what to name this game. Its a big grass field, and its in 2d so....2D Grass game This is a simple 2D game to teach m

An implementation of the popular game Codenames created with Go and React.

OpenCodenames A real-time implementation of Codenames created with React/TypeScript and Golang. You can play the game here! Installation Stack: React

A go implementation of Conway's game of life

go-life A go implementation of Conway's game of life. The program takes input from stdin. It's recommended to use it as cat input.txt | go-life with i

Go implementation of 2048 game

Go implementation of 2048 game

a Go implementation of the game of Briscola

BrisGolang BrisGolang is a Go implementation of the game of briscola using the WebSocket protocol for client/server communication. Usage You can play

An implementation of Conway's Game of Life.
An implementation of Conway's Game of Life.

Conway's Game of Life From Wikipedia, the free encyclopedia: The Game of Life, also known simply as Life, is a cellular automaton devised by the Briti

Releases(v0.1.0)
Owner
Iss Meftah
Running fast on the edge!
Iss Meftah
Snake game made in Go! 🐍

Snake This is a Terminal based snake game made by tristangoossens. Please star this repository to help my first big project grow! Documentation can be

Tristan Goossens 318 Nov 16, 2022
Snake game implemented in golang

little_pineapple(Snake game implemented in golang) 贪吃蛇golang实现 Snake game implemented in golang 数据结构:链表&数组 Data structures used: linked list&array 使用方

null 7 Aug 18, 2020
A simple desktop snake game

A simple desktop snake game that's supposed to run on Linux and Mac OS (maybe eventually on Windows). Work on the game is currently in progress - so i

Meghashyam Chirravoori 2 Oct 12, 2021
Command line Snake Game built with Golang

Commandline Snake Game Commandline Snake Game built with Golang. Took about a whole day to complete. Had breaks inbetween progress made. Assumptions H

Jubril Aminu 1 Jan 22, 2022
A golang Snake game in webassembly

snake A simple Snake game in Go WebAssembly, loading each grid tile from a diffe

Brad Fitzpatrick 97 Nov 20, 2022
A terminal Snake game that supports multiple players to play together online.

GoSnake A Snake game that supports multiple players to play together online. The game is written in go language and does not use other third-party lib

StevE Zhang 2 Jun 25, 2022
Arkanoid game in Go using Ebiten game engine with ECS.

Arkanoid-go Arkanoid game in Go using Ebiten game engine with ECS. You must have Git LFS installed when cloning the repository to download assets. See

null 55 Oct 9, 2022
AircraftWar - a game powered by Go+ spx game engine

AircraftWar - a game powered by Go+ spx game engine How to run Download Go+ and build it. See https://github.com/goplus/gop#how-to-build. Download thi

GoPlus 1 Jan 5, 2022
FlappyCalf - a game powered by Go+ spx game engine

FlappyCalf - a game powered by Go+ spx game engine How to run Download Go+ and build it. See https://github.com/goplus/gop#how-to-build. Download this

GoPlus 3 Nov 6, 2022
FlappyCalf - a game powered by Go+ spx game engine

FlappyCalf - a game powered by Go+ spx game engine How to run Download Go+ and build it. See https://github.com/goplus/gop#how-to-build. Download this

GoPlus 3 Nov 6, 2022