-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is my first push of my html website file.
- Loading branch information
1 parent
7266439
commit 840fbd6
Showing
1 changed file
with
127 additions
and
0 deletions.
There are no files selected for viewing
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,127 @@ | ||
<!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> | ||
<style> | ||
body { | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
height: 100vh; | ||
margin: 0; | ||
background-color: #f0f0f0; | ||
font-family: Arial, sans-serif; | ||
} | ||
|
||
#gameCanvas { | ||
background-color: #a23; | ||
border: 2px solid #fff; | ||
} | ||
|
||
.game-container { | ||
text-align: center; | ||
} | ||
|
||
h1 { | ||
color: #333; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<div class="game-container"> | ||
<h1>Snake Game</h1> | ||
<canvas id="gameCanvas" width="400" height="400"></canvas> | ||
</div> | ||
|
||
<script> | ||
const canvas = document.getElementById('gameCanvas'); | ||
const ctx = canvas.getContext('2d'); | ||
|
||
const boxSize = 20; // Size of each block | ||
let snake = [{x: 200, y: 200}]; // Snake starts in the center | ||
let direction = {x: 0, y: 0}; // Initial direction | ||
let food = {x: Math.floor(Math.random() * 20) * boxSize, y: Math.floor(Math.random() * 20) * boxSize}; // Random food position | ||
let score = 0; | ||
|
||
function drawBox(x, y, color) { | ||
ctx.fillStyle = color; | ||
ctx.fillRect(x, y, boxSize, boxSize); | ||
} | ||
|
||
function drawSnake() { | ||
for (let i = 0; i < snake.length; i++) { | ||
drawBox(snake[i].x, snake[i].y, '#00FF00'); | ||
} | ||
} | ||
|
||
function moveSnake() { | ||
const newHead = { | ||
x: snake[0].x + direction.x * boxSize, | ||
y: snake[0].y + direction.y * boxSize | ||
}; | ||
|
||
snake.unshift(newHead); | ||
|
||
// If the snake eats the food | ||
if (newHead.x === food.x && newHead.y === food.y) { | ||
score++; | ||
food = {x: Math.floor(Math.random() * 20) * boxSize, y: Math.floor(Math.random() * 20) * boxSize}; // Place new food | ||
} else { | ||
snake.pop(); // Remove tail if no food is eaten | ||
} | ||
} | ||
|
||
function checkCollision() { | ||
const head = snake[0]; | ||
|
||
// Check wall collisions | ||
if (head.x < 0 || head.x >= canvas.width || head.y < 0 || head.y >= canvas.height) { | ||
return true; | ||
} | ||
|
||
// Check self-collision | ||
for (let i = 1; i < snake.length; i++) { | ||
if (head.x === snake[i].x && head.y === snake[i].y) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
function drawFood() { | ||
drawBox(food.x, food.y, '#FF0000'); | ||
} | ||
|
||
function updateGame() { | ||
if (checkCollision()) { | ||
alert('Game Over! Your score: ' + score); | ||
snake = [{x: 200, y: 200}]; // Reset the snake | ||
direction = {x: 0, y: 0}; // Reset direction | ||
score = 0; // Reset score | ||
} | ||
|
||
ctx.clearRect(0, 0, canvas.width, canvas.height); // Clear the canvas | ||
drawFood(); // Draw food | ||
moveSnake(); // Move snake | ||
drawSnake(); // Draw snake | ||
} | ||
|
||
document.addEventListener('keydown', function(event) { | ||
if (event.key === 'ArrowUp' && direction.y === 0) { | ||
direction = {x: 0, y: -1}; | ||
} else if (event.key === 'ArrowDown' && direction.y === 0) { | ||
direction = {x: 0, y: 1}; | ||
} else if (event.key === 'ArrowLeft' && direction.x === 0) { | ||
direction = {x: -1, y: 0}; | ||
} else if (event.key === 'ArrowRight' && direction.x === 0) { | ||
direction = {x: 1, y: 0}; | ||
} | ||
}); | ||
|
||
setInterval(updateGame, 100); // Update game every 100 milliseconds | ||
</script> | ||
</body> | ||
</html> |