Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add backround music, score sound, gameover sound #4

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added assets/sound/background.wav
Binary file not shown.
Binary file added assets/sound/gameover.wav
Binary file not shown.
Binary file added assets/sound/score.wav
Binary file not shown.
9 changes: 8 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Snake Game</title>
<link rel="stylesheet" href="style.css">
</head>

<body>
<audio id="eatSound" src="assets/sound/score.wav" preload="auto"></audio>
<audio id="gameOverSound" src="assets/sound/gameover.wav" preload="auto"></audio>
<audio id="backgroundMusic" src="assets/sound/background.wav" preload="auto" autoplay loop></audio>

<div class="game-container">
<canvas id="gameCanvas"></canvas>
<div class="score" id="score">Score: 0</div>
Expand All @@ -15,4 +21,5 @@

<script src="script.js"></script>
</body>
</html>

</html>
169 changes: 91 additions & 78 deletions script.js
Original file line number Diff line number Diff line change
@@ -1,117 +1,130 @@
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const canvas = document.getElementById("gameCanvas");
const ctx = canvas.getContext("2d");
canvas.width = canvas.height = Math.min(window.innerWidth * 0.9, 600);

const box = canvas.width / 30;
let score = 0;
let speed = 150;
let snake = [{ x: box * 5, y: box * 5 }];
let direction = 'RIGHT';
let direction = "RIGHT";
let food = generateFood();
let gameLoop;

const eatSound = document.getElementById("eatSound");
const gameOverSound = document.getElementById("gameOverSound");
const backgroundMusic = document.getElementById("backgroundMusic");

function generateFood() {
let foodX, foodY;
do {
foodX = Math.floor((Math.random() * canvas.width) / box) * box;
foodY = Math.floor((Math.random() * canvas.height) / box) * box;
} while (snake.some(segment => segment.x === foodX && segment.y === foodY));
return { x: foodX, y: foodY };
let foodX, foodY;
do {
foodX = Math.floor((Math.random() * canvas.width) / box) * box;
foodY = Math.floor((Math.random() * canvas.height) / box) * box;
} while (snake.some((segment) => segment.x === foodX && segment.y === foodY));
return { x: foodX, y: foodY };
}

document.addEventListener('keydown', changeDirection);
document.addEventListener("keydown", changeDirection);

function changeDirection(event) {
const key = event.key;
const oppositeDirections = {
UP: 'DOWN',
DOWN: 'UP',
LEFT: 'RIGHT',
RIGHT: 'LEFT'
};
const newDirection = {
ArrowUp: 'UP',
ArrowDown: 'DOWN',
ArrowLeft: 'LEFT',
ArrowRight: 'RIGHT'
}[key];
if (newDirection && newDirection !== oppositeDirections[direction]) {
direction = newDirection;
}
const key = event.key;
const oppositeDirections = {
UP: "DOWN",
DOWN: "UP",
LEFT: "RIGHT",
RIGHT: "LEFT",
};
const newDirection = {
ArrowUp: "UP",
ArrowDown: "DOWN",
ArrowLeft: "LEFT",
ArrowRight: "RIGHT",
}[key];
if (newDirection && newDirection !== oppositeDirections[direction]) {
direction = newDirection;
}
}

function drawSnake() {
snake.forEach((segment, index) => {
ctx.fillStyle = index === 0 ? '#00ff88' : '#00cc66';
ctx.fillRect(segment.x, segment.y, box, box);
ctx.strokeStyle = '#161b22';
ctx.strokeRect(segment.x, segment.y, box, box);
});
snake.forEach((segment, index) => {
ctx.fillStyle = index === 0 ? "#00ff88" : "#00cc66";
ctx.fillRect(segment.x, segment.y, box, box);
ctx.strokeStyle = "#161b22";
ctx.strokeRect(segment.x, segment.y, box, box);
});
}

function drawFood() {
ctx.fillStyle = '#ff0033';
ctx.beginPath();
ctx.arc(food.x + box / 2, food.y + box / 2, box / 2.5, 0, Math.PI * 2);
ctx.fill();
ctx.fillStyle = "#ff0033";
ctx.beginPath();
ctx.arc(food.x + box / 2, food.y + box / 2, box / 2.5, 0, Math.PI * 2);
ctx.fill();
}

function moveSnake() {
const head = { ...snake[0] };
if (direction === 'UP') head.y -= box;
else if (direction === 'DOWN') head.y += box;
else if (direction === 'LEFT') head.x -= box;
else if (direction === 'RIGHT') head.x += box;
const head = { ...snake[0] };
if (direction === "UP") head.y -= box;
else if (direction === "DOWN") head.y += box;
else if (direction === "LEFT") head.x -= box;
else if (direction === "RIGHT") head.x += box;

snake.unshift(head);
snake.unshift(head);

if (head.x === food.x && head.y === food.y) {
score += 10;
document.getElementById('score').innerText = `Score: ${score}`;
food = generateFood();
if (speed > 50) speed -= 5;
clearInterval(gameLoop);
gameLoop = setInterval(update, speed);
} else {
snake.pop();
}
if (head.x === food.x && head.y === food.y) {
eatSound.currentTime = 0;
eatSound.play();
score += 10;
document.getElementById("score").innerText = `Score: ${score}`;
food = generateFood();
if (speed > 50) speed -= 5;
clearInterval(gameLoop);
gameLoop = setInterval(update, speed);
} else {
snake.pop();
}
}

function checkCollision() {
const head = snake[0];
if (
head.x < 0 ||
head.x >= canvas.width ||
head.y < 0 ||
head.y >= canvas.height ||
snake.slice(1).some(segment => segment.x === head.x && segment.y === head.y)
) {
document.getElementById('gameOver').style.display = 'block';
clearInterval(gameLoop);
}
const head = snake[0];
if (
head.x < 0 ||
head.x >= canvas.width ||
head.y < 0 ||
head.y >= canvas.height ||
snake
.slice(1)
.some((segment) => segment.x === head.x && segment.y === head.y)
) {
gameOverSound.play();
backgroundMusic.pause();
backgroundMusic.currentTime = 0;
document.getElementById("gameOver").style.display = "block";
clearInterval(gameLoop);
}
}

function restartGame() {
snake = [{ x: box * 5, y: box * 5 }];
direction = 'RIGHT';
score = 0;
speed = 150;
document.getElementById('score').innerText = `Score: 0`;
document.getElementById('gameOver').style.display = 'none';
food = generateFood();
gameLoop = setInterval(update, speed);
backgroundMusic.play();
snake = [{ x: box * 5, y: box * 5 }];
direction = "RIGHT";
score = 0;
speed = 150;
document.getElementById("score").innerText = `Score: 0`;
document.getElementById("gameOver").style.display = "none";
food = generateFood();
gameLoop = setInterval(update, speed);
}

function update() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawFood();
moveSnake();
drawSnake();
checkCollision();
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawFood();
moveSnake();
drawSnake();
checkCollision();
}

backgroundMusic.play();
gameLoop = setInterval(update, speed);

window.addEventListener('resize', () => {
canvas.width = canvas.height = Math.min(window.innerWidth * 0.9, 600);
window.addEventListener("resize", () => {
canvas.width = canvas.height = Math.min(window.innerWidth * 0.9, 600);
});