Thanks to visit codestin.com
Credit goes to github.com

Skip to content

sambhav-007/JavaScript-Snake

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

updated readme.md

Original game is located here:

http://patorjk.com/games/snake

How to use

The index.html file should give an idea of how to use this code. However, below you can see how to initialize it into any div within a webpage.

const mySnakeBoard = new SNAKE.Board( {
                                        boardContainer: "game-area",
                                        fullScreen: false,
                                        width: 580,
                                        height:400
                                    });

The comments within the source code are formatted a little strange because at the time I was playing around with YUI Doc which generates documentation from code. Kind of sucks that there's so much churn in the JavaScript world. However, I'm glad the rest of the code doesn't use any external libraries, as this game still works the same after over a decade.

Running

Clone project, then at command line:

npx parcel src/index.html

Runs on http://localhost:1234

AI Snake

If you want to control the snake via an AI algorithm see the ai-init.js and ai-example files.

Essentially all you have to do is run params.startAIGame(); when initializing and pass in a moveSnakeWithAI method which is run before the snake does each move.

  moveSnakeWithAI: ({
                      grid,
                      snakeHead,
                      currentDirection,
                      isFirstGameMove,
                      setDirection,
                    }) => {

    /*
    Direction:
                0
              3   1
                2
     */

    // This is NOT a real hamiltonian cycle. It misses some values, I'm just including this here as an example of
    // a look-up type table that you could do.
    const hamiltonianCycleGrid = [
      [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
      [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 0],
      [0, 0, 2, 3, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 0],
      [0, 0, 2, 0, 0, 3, 0, 2, 0, 2, 0, 2, 0, 2, 0],
      [0, 0, 2, 0, 2, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0],
      [0, 0, 3, 0, 3, 3, 3, 3, 0, 3, 0, 3, 0, 3, 0],
      [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    ];

    const newDirection = hamiltonianCycleGrid[snakeHead.row][snakeHead.col];
    setDirection(newDirection);
  },
  onInit: (params) => {
      params.startAIGame(); // This start an AI game
    },

Contributors

Thanks goes to these people: (emoji key)

patorjk
patorjk

💻 📖 🎨 🐛 💡
ultra17
ultra17

💻 📖 🎨 🐛
Rb64
Rb64

💻 🐛
Wyatt Nulton
Wyatt Nulton

💻 🐛
Ashish Bhoir
Ashish Bhoir

📖
Dan G
Dan G

💻 🐛
Megas4ever
Megas4ever

💻 🎨
Bugs Bunny
Bugs Bunny

💻 🐛
James Cote
James Cote

💻 🐛 📖
Arun Yokesh
Arun Yokesh

💻 🎨
Greg French
Greg French

💻
KT360
KT360

💻 🎨
Thusal Ranawaka
Thusal Ranawaka

💻 🎨
C. S.
C. S.

💻 🎨
Akhil Manohar
Akhil Manohar

💻

About

The game of Snake written in JavaScript.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 57.7%
  • CSS 30.7%
  • HTML 11.6%