-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIndex.html
More file actions
154 lines (139 loc) · 4.21 KB
/
Copy pathIndex.html
File metadata and controls
154 lines (139 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Leon's Snake Game</title>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
flex-direction: column;
align-items: center;
height: 100vh;
margin: 0;
}
#gameContainer {
border: 1px solid black;
background-color: #f0f0f0;
}
.row {
display: flex;
}
.cell {
width: 20px;
height: 20px;
border: 1px solid #ccc;
box-sizing: border-box;
}
.snake {
background-color: green;
}
.food {
background-color: red;
}
</style>
</head>
<body>
<h1>Leon's Snake Game</h1>
<div id="gameContainer"></div>
<div id="score"></div>
<script>
const container = document.getElementById('gameContainer');
const scoreDisplay = document.getElementById('score');
const grid = [];
const rows = 20;
const cols = 20;
let snake = [{x: 9, y: 10}]; // Initial position of the snake
let food = {x: Math.floor(Math.random() * cols), y: Math.floor(Math.random() * rows)}; // Initial position of the food
let score = 0;
let highestScore = localStorage.getItem('highestScore') || 0;
let direction;
scoreDisplay.textContent = `Score: ${score} | Highest Score: ${highestScore}`;
// Create grid
for (let i = 0; i < rows; i++) {
const row = document.createElement('div');
row.classList.add('row');
grid.push([]);
for (let j = 0; j < cols; j++) {
const cell = document.createElement('div');
cell.classList.add('cell');
grid[i].push(cell);
row.appendChild(cell);
}
container.appendChild(row);
}
// Event listener for arrow key presses
document.addEventListener('keydown', directionHandler);
function directionHandler(event) {
if (event.keyCode === 37 && direction !== 'RIGHT') {
direction = 'LEFT';
} else if (event.keyCode === 38 && direction !== 'DOWN') {
direction = 'UP';
} else if (event.keyCode === 39 && direction !== 'LEFT') {
direction = 'RIGHT';
} else if (event.keyCode === 40 && direction !== 'UP') {
direction = 'DOWN';
}
}
// Function to draw on grid
function draw() {
// Clear grid
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
grid[i][j].classList.remove('snake', 'food');
}
}
// Draw snake
snake.forEach((segment) => {
grid[segment.y][segment.x].classList.add('snake');
});
// Draw food
grid[food.y][food.x].classList.add('food');
// Move snake
let snakeX = snake[0].x;
let snakeY = snake[0].y;
if (direction === 'LEFT') snakeX -= 1;
if (direction === 'UP') snakeY -= 1;
if (direction === 'RIGHT') snakeX += 1;
if (direction === 'DOWN') snakeY += 1;
// If snake eats the food
if (snakeX === food.x && snakeY === food.y) {
score++;
if (score > highestScore) {
highestScore = score;
localStorage.setItem('highestScore', highestScore);
}
food = {x: Math.floor(Math.random() * cols), y: Math.floor(Math.random() * rows)};
} else {
snake.pop();
}
// Create new head of the snake
const newHead = {
x: snakeX,
y: snakeY
};
// Game over conditions
if (
snakeX < 0 || snakeX >= cols ||
snakeY < 0 || snakeY >= rows ||
collision(newHead, snake)
) {
clearInterval(game);
alert('Game Over! Your score: ' + score);
location.reload();
}
snake.unshift(newHead);
scoreDisplay.textContent = `Score: ${score} | Highest Score: ${highestScore}`;
}
// Function to check collision
function collision(head, array) {
return array.some((segment) => {
return (head.x === segment.x && head.y === segment.y);
});
}
// Game loop
let game = setInterval(draw, 100);
</script>
</body>
</html>