Git Product home page Git Product logo

bash-snake's Introduction

About

A simple snake game written in Bash.

How to Play

Direction control: vim-style keys h, j, k and l; Quit: q Accept both upper- and lower-case.

Some Implementation Details

  • Bash-snake has two processes, the foreground one responds to user's control commands, the background one draws the board. kill and trap are used to enable communication between the two processes.

    The foreground process getchar() ignores SIGINT and SIGQUIT, and replies to the signal of death SIG_HEAD by returning from the function getchar().

    The background process game_loop() traps direction control signals from the keyboard, and self-defined signal SIG_QUIT which indicates the press of Q button.

  • Use $! to grasp the PID of the latest created background process.

  • When setting up several traps, it's necessary to guarantee that the normal execution will not be interrupted by the signal handling if the handlers envolve modification of some variables it depends on.

  • The snake is represented by the coordinate $head_r and $head_c indicating the row and column on which the snake head is, and a string $body which stores the directions from the head to tail. e.g. a snake with this shape (@ is the head):

@
oooo
   o

will have a $body with value 21112, meaning 'down, right, right, right, down'.

With the above scheme and the coordinates of the snake head, we can figure out the position the the entire snake body, without storing every part of its body.

  • The board is represented by a "2D array", which is actually implemented by a bunch of bash arrays and the eval command. For example, assigning 123 to the array entry arr[5][6] is done with eval "arr$i[$j]=123".

  • The board is re-drawn in each iteration, which happens every 0.03 seconds.

  • Coloring is implemented with the escaped sequences of the terminal.

bash-snake's People

Contributors

pjhades avatar giovazz89 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.