0% found this document useful (0 votes)
33 views3 pages

Js

This document contains JavaScript code for a simple snake game. It includes game constants, variables, and functions for game mechanics such as movement, collision detection, scoring, and rendering the snake and food on the board. The game also incorporates sound effects and keeps track of high scores using local storage.

Uploaded by

Zaid Sayed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views3 pages

Js

This document contains JavaScript code for a simple snake game. It includes game constants, variables, and functions for game mechanics such as movement, collision detection, scoring, and rendering the snake and food on the board. The game also incorporates sound effects and keeps track of high scores using local storage.

Uploaded by

Zaid Sayed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

// Game Constants & Variables

let inputDir = {x: 0, y: 0};


const foodSound = new Audio('music/food.mp3');
const gameOverSound = new Audio('music/gameover.mp3');
const moveSound = new Audio('music/move.mp3');
const musicSound = new Audio('music/music.mp3');
let speed = 19;
let score = 0;
let lastPaintTime = 0;
let snakeArr = [
{x: 13, y: 15}
];

food = {x: 6, y: 7};

// Game Functions
function main(ctime) {
[Link](main);
// [Link](ctime)
if((ctime - lastPaintTime)/1000 < 1/speed){
return;
}
lastPaintTime = ctime;
gameEngine();
}

function isCollide(snake) {
// If you bump into yourself
for (let i = 1; i < [Link]; i++) {
if(snake[i].x === snake[0].x && snake[i].y === snake[0].y){
return true;
}
}
// If you bump into the wall
if(snake[0].x >= 18 || snake[0].x <=0 || snake[0].y >= 18 || snake[0].y <=0){
return true;
}

return false;
}

function gameEngine(){
// Part 1: Updating the snake array & Food
if(isCollide(snakeArr)){
[Link]();
[Link]();
inputDir = {x: 0, y: 0};
alert("Game Over. Press any key to play again!");
snakeArr = [{x: 13, y: 15}];
[Link]();
score = 0;
}

// If you have eaten the food, increment the score and regenerate the food
if(snakeArr[0].y === food.y && snakeArr[0].x ===food.x){
[Link]();
score += 1;
if(score>hiscoreval){
hiscoreval = score;
[Link]("hiscore", [Link](hiscoreval));
[Link] = "HiScore: " + hiscoreval;
}
[Link] = "Score: " + score;
[Link]({x: snakeArr[0].x + inputDir.x, y: snakeArr[0].y +
inputDir.y});
let a = 2;
let b = 16;
food = {x: [Link](a + (b-a)* [Link]()), y: [Link](a + (b-a)*
[Link]())}
}

// Moving the snake


for (let i = [Link] - 2; i>=0; i--) {
snakeArr[i+1] = {...snakeArr[i]};
}

snakeArr[0].x += inputDir.x;
snakeArr[0].y += inputDir.y;

// Part 2: Display the snake and Food


// Display the snake
[Link] = "";
[Link]((e, index)=>{
snakeElement = [Link]('div');
[Link] = e.y;
[Link] = e.x;

if(index === 0){


[Link]('head');
}
else{
[Link]('snake');
}
[Link](snakeElement);
});
// Display the food
foodElement = [Link]('div');
[Link] = food.y;
[Link] = food.x;
[Link]('food')
[Link](foodElement);

// Main logic starts here


[Link]();
let hiscore = [Link]("hiscore");
if(hiscore === null){
hiscoreval = 0;
[Link]("hiscore", [Link](hiscoreval))
}
else{
hiscoreval = [Link](hiscore);
[Link] = "HiScore: " + hiscore;
}
[Link](main);
[Link]('keydown', e =>{
inputDir = {x: 0, y: 1} // Start the game
[Link]();
switch ([Link]) {
case "ArrowUp":
[Link]("ArrowUp");
inputDir.x = 0;
inputDir.y = -1;
break;

case "ArrowDown":
[Link]("ArrowDown");
inputDir.x = 0;
inputDir.y = 1;
break;

case "ArrowLeft":
[Link]("ArrowLeft");
inputDir.x = -1;
inputDir.y = 0;
break;

case "ArrowRight":
[Link]("ArrowRight");
inputDir.x = 1;
inputDir.y = 0;
break;
default:
break;
}

});

You might also like