Skip to content

b12f/Learn-Snake

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 

Repository files navigation

snake.js

Snake game with solver API for JS beginners to learn programming. Start node solvehere.js to see a random function performing.

Variables

bool game.devMode

default: false

You can set this to either true or false to toggle the display of the grid in the console. Devmode makes sure the grid doesn't get displayed so you can output your console log by hand.

int game.updateTime

default: 20

Time between gamesteps in ms.

Methods

###game.setInputFunction(function inputFunction)

Should be called with an inputFunction that returns either "r", "l", "u" or "d" for right, left, up, down respectively. inputFunction will receive a game object as a parameter, with the following properties:

int width
int height
array grid
    Array of Arrays, with each parent array representing a row and containing another array of column fields.
Fields will be either 0, 1 or 2, representing nothing, a piece of your snake and a piece of fruit respectively.
array snake
    Array of objects: {x: xPosition, y: yPosition}. The earliest entry is the tail, the last the head.

Changes made to this object will not result in changes to the game.

game.on(string event, function callback)

Should be called when you want to do something when one of the following events happens:

  • beforeStep
  • afterStep
  • afterInit
  • failed

Will receive the same game object as your inputFunction.

game.off(string event)

Clears all event listeners for the given event.

game.init(int width, int height)

Initializes the game board with height and width (both >=20).

game.start()

Starts the game.

game.pause()

Pauses the game.

game.log(string message)

Will log all messages you provide it with to display under Log data, in case you want to display log data while showing the grid. Messages will be collected per gamestep and displayed as follows:

message1 | message2 | message3[| nextmessage ]

game.showGrid()

Clears the console, draws the gameboard.

About

Snake game with solver API for JS beginners to learn programming.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published