-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
123 lines (108 loc) · 3.5 KB
/
script.js
File metadata and controls
123 lines (108 loc) · 3.5 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
document.addEventListener('DOMContentLoaded', () => {
const board = document.getElementById('game-board');
const scoreElement = document.getElementById('score');
const gridSize = 20;
const cellSize = 20;
let snake = [{ x: 10, y: 10 }];
let food = generateFood();
let direction = 'right';
let score = 0;
function generateFood() {
const foodX = Math.floor(Math.random() * gridSize);
const foodY = Math.floor(Math.random() * gridSize);
return { x: foodX, y: foodY };
}
function draw() {
board.innerHTML = '';
// Draw snake
snake.forEach(segment => {
const snakeSegment = document.createElement('div');
snakeSegment.className = 'snake';
snakeSegment.style.gridRowStart = segment.y + 1;
snakeSegment.style.gridColumnStart = segment.x + 1;
board.appendChild(snakeSegment);
});
// Draw food
const foodElement = document.createElement('div');
foodElement.className = 'food';
foodElement.style.gridRowStart = food.y + 1;
foodElement.style.gridColumnStart = food.x + 1;
board.appendChild(foodElement);
}
function update() {
// Move snake
const head = { ...snake[0] };
switch (direction) {
case 'up':
head.y = (head.y - 1 + gridSize) % gridSize;
break;
case 'down':
head.y = (head.y + 1) % gridSize;
break;
case 'left':
head.x = (head.x - 1 + gridSize) % gridSize;
break;
case 'right':
head.x = (head.x + 1) % gridSize;
break;
}
// Check collision with food
if (head.x === food.x && head.y === food.y) {
score++;
scoreElement.textContent = `Score: ${score}`;
food = generateFood();
} else {
// Remove the tail
snake.pop();
}
// Check collision with self
if (snake.some(segment => segment.x === head.x && segment.y === head.y)) {
gameOver();
return;
}
// Add new head
snake.unshift(head);
draw();
}
function handleKeyPress(event) {
switch (event.key) {
case 'ArrowUp':
if (direction !== 'down') {
direction = 'up';
}
break;
case 'ArrowDown':
if (direction !== 'up') {
direction = 'down';
}
break;
case 'ArrowLeft':
if (direction !== 'right') {
direction = 'left';
}
break;
case 'ArrowRight':
if (direction !== 'left') {
direction = 'right';
}
break;
}
}
function gameOver() {
alert(`Game Over! Your score is ${score}.`);
resetGame();
}
function resetGame() {
snake = [{ x: 10, y: 10 }];
food = generateFood();
direction = 'right';
score = 0;
scoreElement.textContent = 'Score: 0';
draw();
}
// Initial setup
draw();
setInterval(update, 200);
// Event listeners
document.addEventListener('keydown', handleKeyPress);
});