Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
121 lines (105 loc) · 3.29 KB
/
Copy pathscript.js
File metadata and controls
121 lines (105 loc) · 3.29 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
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const scoreEl = document.getElementById('score');
const highScoreEl = document.getElementById('highScore');
const gridSize = 20;
const tileCount = canvas.width / gridSize;
let snake = [{x: 10, y: 10}];
let food = {x: 15, y: 15};
let dx = 0;
let dy = 0;
let score = 0;
let highScore = localStorage.getItem('highScore') || 0;
let gameInterval;
highScoreEl.textContent = highScore;
function startGame() {
clearInterval(gameInterval);
gameInterval = setInterval(gameLoop, 120); // speed
}
function resetGame() {
clearInterval(gameInterval);
snake = [{x: 10, y: 10}];
dx = 0; dy = 0;
score = 0;
scoreEl.textContent = score;
placeFood();
draw();
}
function gameLoop() {
moveSnake();
if (checkCollision()) {
clearInterval(gameInterval);
alert('Game Over! Score: ' + score);
return;
}
draw();
}
function moveSnake() {
const head = {x: snake[0].x + dx, y: snake[0].y + dy};
snake.unshift(head);
if (head.x === food.x && head.y === food.y) {
score += 10;
scoreEl.textContent = score;
if (score > highScore) {
highScore = score;
localStorage.setItem('highScore', highScore);
highScoreEl.textContent = highScore;
}
placeFood();
} else {
snake.pop();
}
}
function draw() {
// Clear canvas
ctx.fillStyle = '#1a1a2e';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Draw snake
ctx.fillStyle = '#a8ff78';
snake.forEach(part => {
ctx.fillRect(part.x * gridSize, part.y * gridSize, gridSize - 2, gridSize - 2);
});
// Draw food
ctx.fillStyle = '#ff6b6b';
ctx.fillRect(food.x * gridSize, food.y * gridSize, gridSize - 2, gridSize - 2);
}
function placeFood() {
food.x = Math.floor(Math.random() * tileCount);
food.y = Math.floor(Math.random() * tileCount);
}
function checkCollision() {
const head = snake[0];
// Wall collision
if (head.x < 0 || head.x >= tileCount || head.y < 0 || head.y >= tileCount) return true;
// 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;
}
// Keyboard controls
document.addEventListener('keydown', e => {
if (e.key === 'ArrowUp' && dy === 0) { dx = 0; dy = -1; }
if (e.key === 'ArrowDown' && dy === 0) { dx = 0; dy = 1; }
if (e.key === 'ArrowLeft' && dx === 0) { dx = -1; dy = 0; }
if (e.key === 'ArrowRight' && dx === 0) { dx = 1; dy = 0; }
});
// Mobile swipe controls
let touchStartX = 0, touchStartY = 0;
canvas.addEventListener('touchstart', e => {
touchStartX = e.touches[0].clientX;
touchStartY = e.touches[0].clientY;
});
canvas.addEventListener('touchend', e => {
let dxTouch = e.changedTouches[0].clientX - touchStartX;
let dyTouch = e.changedTouches[0].clientY - touchStartY;
if (Math.abs(dxTouch) > Math.abs(dyTouch)) {
if (dxTouch > 0 && dx === 0) { dx = 1; dy = 0; } // Right
else if (dxTouch < 0 && dx === 0) { dx = -1; dy = 0; } // Left
} else {
if (dyTouch > 0 && dy === 0) { dx = 0; dy = 1; } // Down
else if (dyTouch < 0 && dy === 0) { dx = 0; dy = -1; } // Up
}
});
placeFood();
draw();