//Given a position of snake/food (Will be changed in the future to handle multiplayer/randomness)
constgameState={
player:{
//Give a temporary position for the snake head
headPosition:{
x:3,
y:10,
},
//Give a temporary speed
velocity:{
x:1,
y:0,
},
//Give a temporary position of the snake body
snake:[
{x:2,y:10},
{x:3,y:10},
],
},
//Give a temporary position for the food, we will change it later to a random position
food:{
x:7,
y:7,
},
//Initialize the gridesize
gridsize:20,
};
//Initialize the game elements such as canvas size, color, etc.
functioninit(){
canvas=document.createElement('canvas');
element=canvas.getContext('2d');
canvas.width=600;
canvas.height=600;
element.fillStyle=BACKGROUND_COLOR;
element.fillRect(0,0,canvas.width,canvas.height);
gameScreen.appendChild(canvas);
document.addEventListener('keydown',keydown);
}
//Here is where we will implement movement/direction logic. The server is not implemented yet so we just use console.log(event.keyCode) to represent keys pressed
functionkeydown(event){
console.log(event.keyCode);
}
init();
//Change background color, initilize game screen size, and place a food object at a set position