-
Notifications
You must be signed in to change notification settings - Fork 640
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from Akash20x/master
Snake Game Javascript Project
- Loading branch information
Showing
11 changed files
with
278 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
|
||
<head> | ||
<meta charset="UTF-8"> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> | ||
<link rel="shortcut icon" href="assets/snake-game.png" type="image/x-icon"> | ||
<link rel="stylesheet" href="style.css"> | ||
<title>Snake Game</title> | ||
</head> | ||
|
||
<body> | ||
<h1>Snake Game</h1> | ||
<div class="main"> | ||
<div class="content"> | ||
<div id="board"></div> | ||
</div> | ||
<div id="scoreBox">Score: 0</div> | ||
<div id="hiscoreBox">Hi Score: 0</div> | ||
</div> | ||
|
||
<footer> | ||
<p>< / > with ❤️ by | ||
<a href="https://swapnilsparsh.github.io/">Swapnil Srivastava</a> | ||
<br> | ||
<a href="https://github.com/swapnilsparsh/30DaysOfJavaScript" target="_blank">#30DaysOfJavaScript | ||
</a> | ||
</p> | ||
</footer> | ||
<script src="script.js"></script> | ||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
let inputDir = { x: 0, y: 0 }; | ||
const moveSound = new Audio('audio/move.mp3'); | ||
const foodSound = new Audio('audio/food.mp3'); | ||
const gameOverSound = new Audio('audio/gameover.mp3'); | ||
let speed = 5; | ||
let lastPaintTime = 0; | ||
let snakeArr = [{ x: 13, y: 15 }] | ||
food = { x: 6, y: 7 }; | ||
let score = 0; | ||
// Game Functions | ||
function main(ctime) { | ||
window.requestAnimationFrame(main); | ||
if ((ctime - lastPaintTime) / 1000 < 1 / speed) { | ||
return; | ||
} | ||
lastPaintTime = ctime; | ||
gameEngine(); | ||
} | ||
|
||
function isCollide(snake) { | ||
// If you bump into yourself | ||
for (let i = 1; i < snakeArr.length; 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() { | ||
// Updating the snake array & Food | ||
if (isCollide(snakeArr)) { | ||
gameOverSound.play(); | ||
inputDir = { x: 0, y: 0 }; | ||
alert("Game Over. Press Any Key To Continue"); | ||
snakeArr = [{ x: 13, y: 15 }]; | ||
score = 0; | ||
scoreBox.innerHTML = 'Score: ' + score; | ||
} | ||
if (snakeArr[0].x === food.x && snakeArr[0].y === food.y) { | ||
foodSound.play(); | ||
score += 1; | ||
if (score > hiscoreval) { | ||
hiscoreval = score; | ||
localStorage.setItem("hiscore", JSON.stringify(hiscoreval)); | ||
hiscoreBox.innerHTML = "Hi Score: " + hiscoreval; | ||
} | ||
scoreBox.innerHTML = 'Score: ' + score; | ||
snakeArr.unshift({ x: snakeArr[0].x + inputDir.x, y: snakeArr[0].y + inputDir.y }); | ||
let a = 2; | ||
let b = 16; | ||
food = { x: Math.round(a + (b - 1) * Math.random()), y: Math.round(a + (b - 1) * Math.random()) }; | ||
} | ||
// Moving the snake | ||
for (let i = snakeArr.length - 2; i >= 0; i--) { | ||
snakeArr[i + 1] = {...snakeArr[i] }; | ||
} | ||
snakeArr[0].x += inputDir.x; | ||
snakeArr[0].y += inputDir.y; | ||
|
||
// Display the snake and Food | ||
// Display the snake | ||
board.innerHTML = ""; | ||
snakeArr.forEach((e, index) => { | ||
snakeElement = document.createElement('div'); | ||
snakeElement.style.gridRowStart = e.y; | ||
snakeElement.style.gridColumnStart = e.x; | ||
if (index === 0) { | ||
snakeElement.classList.add('head'); | ||
} else { | ||
snakeElement.classList.add('snake'); | ||
} | ||
board.appendChild(snakeElement); | ||
}); | ||
// Display the food | ||
foodElement = document.createElement('div'); | ||
foodElement.style.gridRowStart = food.y; | ||
foodElement.style.gridColumnStart = food.x; | ||
foodElement.classList.add('food'); | ||
board.appendChild(foodElement); | ||
} | ||
|
||
// Main logic starts here | ||
let hiscore = localStorage.getItem("hiscore"); | ||
if (hiscore === null) { | ||
hiscoreval = 0; | ||
localStorage.setItem("hiscore", JSON.stringify(hiscoreval)); | ||
} else { | ||
hiscoreval = JSON.parse(hiscore); | ||
hiscoreBox.innerHTML = "Hi Score: " + hiscore; | ||
} | ||
|
||
window.requestAnimationFrame(main); | ||
window.addEventListener('keydown', e => { | ||
inputDir = { x: 0, y: 1 }; | ||
moveSound.play(); | ||
switch (e.key) { | ||
case "ArrowUp": | ||
console.log(e.key); | ||
inputDir.x = 0; | ||
inputDir.y = -1; | ||
break; | ||
case "ArrowDown": | ||
console.log(e.key); | ||
inputDir.x = 0; | ||
inputDir.y = 1; | ||
break; | ||
case "ArrowLeft": | ||
console.log(e.key); | ||
inputDir.x = -1; | ||
inputDir.y = 0; | ||
break; | ||
case "ArrowRight": | ||
console.log(e.key); | ||
inputDir.x = 1; | ||
inputDir.y = 0; | ||
break; | ||
default: | ||
break; | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
@import url('https://fonts.googleapis.com/css2?family=Press+Start+2P&display=swap'); | ||
* { | ||
padding: 0; | ||
margin: 0; | ||
} | ||
|
||
body { | ||
overflow: hidden; | ||
background: #19172e; | ||
font-size: 40px; | ||
} | ||
|
||
.main { | ||
background: #614385; | ||
background: -webkit-linear-gradient(to right, #516395, #614385); | ||
background: linear-gradient(to right, #516395, #614385); | ||
background-repeat: no-repeat; | ||
align-items: center; | ||
justify-content: center; | ||
position: absolute; | ||
top: 50%; | ||
left: 50%; | ||
transform: translate(-50%, -50%); | ||
display: block; | ||
} | ||
|
||
.content { | ||
width: 300px; | ||
height: 365px; | ||
padding-top: 8px; | ||
} | ||
h1 { | ||
color: #fff; | ||
text-align: center; | ||
position: absolute; | ||
left: 0; | ||
right: 0; | ||
top: 30px; | ||
padding-top: 10px; | ||
font-size: 2rem !important; | ||
} | ||
|
||
#board { | ||
background-color: #260e44; | ||
width: 260px; | ||
height: 285px; | ||
border: 4px solid white; | ||
border-radius: 4px; | ||
display: grid; | ||
grid-template-rows: repeat(18, 1fr); | ||
grid-template-columns: repeat(18, 1fr); | ||
box-shadow: rgba(0, 0, 0, 0.56) 0px 22px 70px 4px; | ||
margin-left: 18px; | ||
margin-top:5px; | ||
|
||
} | ||
|
||
#scoreBox { | ||
position: absolute; | ||
font-size: 14px; | ||
color: white; | ||
bottom: 11%; | ||
left: 30%; | ||
font-family: 'Press Start 2P', cursive; | ||
} | ||
|
||
#hiscoreBox { | ||
position: absolute; | ||
font-size: 14px; | ||
color: white; | ||
bottom: 5%; | ||
left: 16%; | ||
font-family: 'Press Start 2P', cursive; | ||
} | ||
|
||
|
||
.head { | ||
background: yellow; | ||
border: 2px solid rgb(26, 1, 26); | ||
transform: scale(1.02); | ||
border-radius: 9px; | ||
} | ||
|
||
.snake { | ||
background-color: rgb(0, 225, 255); | ||
} | ||
|
||
.food { | ||
background: red; | ||
border: .25vmin solid black; | ||
border-radius: 8px; | ||
} | ||
|
||
footer { | ||
text-align: center; | ||
color: white; | ||
font-size: 1rem !important; | ||
position: absolute; | ||
left: 0; | ||
right: 0; | ||
bottom: 0; | ||
margin-bottom: 0; | ||
padding: 5px; | ||
} | ||
|
||
footer a:visited { | ||
color: inherit; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters