Skip to content

Snake Object

Michael Sagnes edited this page Jan 8, 2020 · 9 revisions

snakeObject.js

Constructor

Snake(map)

Creates a new Snake object.

Parameters:

map

The expected parameter is an instance of the Map Object

Properties

To be added.

Object Methods

getDirection(keyCode, keys)

Parameters:

keyCode

keys

This function takes in two parameters. The pressed key code from event handling and a key object, both reside in canvas.js. The function sets the snake direction property as mentioned below.

What the function returns:

Does not return any value. It sets the snake object property direction to one of the following four states: LEFT, RIGHT, UP or DOWN.


directionChange(map)

Parameters:

map

The expected parameter is an instance of the Map Object

What the function returns:

Moves the head of the snake one unit in the current direction of movement.


isOutOfBounds(map)

Parameters:

map

The expected parameter is an instance of the Map Object

What the function returns:

Checks if the position of the head has the same x-coordinate or y-coordinate as either of the four walls. Returns true in that case.


eatFood(food)

Parameters:

food

The expected parameter is an instance of the Food Object

What the function returns:

Checks if the position of the head matches with the x-coordinate and y-coordinate of the current food. Returns true if that's the case, returns false if not.


incrementTail()

Parameters:

This function takes no parameters.

What the function returns:

Defines variable headPosition with the current x- and y-coordinates of the head. The function proceeds to unshift the headPosition and check whether or not the length of the Snake object-array.


isTouchingItself()

Parameters:

This function takes no parameters.

What the function returns:

Loops through the x- and y- coordinates of every tail joint and checks if the head has the same position as any of them. Returns true if that's the case, returns false if not.


resetSnake(map)

Parameters:

map

The expected parameter is an instance of the Map Object

What the function returns:

Does not return anything. The function sets the snake object back to its initial state. I.e. when the game needs to be reset.


incrementMove()

Parameters:

This function takes no parameters.

What the function returns:

Does not return anything. Simply increments the property moves of the snake object, by one.

Clone this wiki locally