Home / Game JS

GAME JS

GAME.js is a javascript library that provide helper functions and game play.

Below are the available functions to call on GAME


GAME

GAME.createSnake([name: string, [colour: string]]): Snake

GAME.createSnake has two optional parameters, name and color, which are both strings. GAME.createSnake returns a Snake Object.


GAME.createApple(): Apple

GAME.createApple returns a Apple Object.


GAME.setDirectionForSnake(): void

GAME.setDirectionForSnake sets the direction for where the snake will move next. It does not return anything.


GAME.detectCollisionBetween(object1, object2): boolean

GAME.detectCollisionBetween detects if two objects collide. Returns true or false.


GAME.endGame([message]): void

GAME.endGameends the game. Has an optional message parameter to display.


GAME.growSnake(): void

GAME.growSnake will make snake grow by one square. It does not return anything


GAME.setAppleInRandomLocation(): Apple

GAME.setAppleInRandomLocation generates a new Apple in a new location. This function returns an Apple object.


GAME.draw([snake, [apple]]): void

GAME.draw has two optional parameters, snake and apple. Pass in the Snake and Apple created by GAME.createSnake and GAME.createApple. GAME.draw does not return anything.


GAME.loop([gameSpeed: number, [gameRules: function]]): void

GAME.loop has two optional parameters, gameRules and gameSpeed.

gameSpeed is a number. The number is how many times the game will render per second.

gameRules can be any function.

GAME.loop does not return anything.


GAME.onArrowKey(callback: function): void

GAME.onArrowKey has one parameters, callback, which takes a function as an argument. This function will be passed a direction of "up", "down", "left" and "right".

GAME.onArrowKey does not return anything.


GAME.increaseGameSpeed(speed: number): void

GAME.increaseGameSpeed increases the speed of the game. The function does not return anything.


SNAKE

When you create a snake with GAME.createSnake, the snake object returned has two functions.

snake.head(): Location

snake.head returns the location of the snakes head.


snake.body(): Location

snake.body returns the location of the snakes body.