-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
207 lines (186 loc) · 7.26 KB
/
index.html
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<html lang="en" class="hydrated"><head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Pixel Snake Game</title>
<style>
body {
background-color: #000;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
font-family: 'Courier New', monospace;
}
#gameContainer {
text-align: center;
}
#gameCanvas {
border: 4px solid #4CAF50;
background-color: #000;
}
#score, #highScore {
color: #4CAF50;
font-size: 24px;
margin-top: 20px;
}
#startButton{
background-color: #4CAF50;
border: none;
color: white;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin-top: 20px;
cursor: pointer;
transition: background-color 0.3s;
}
#startButton:hover {
background-color: #45a049;
}
</style>
<script src="chrome-extension://kapjaoifikajdcdehfdlmojlepfpkpoe/sqrx-email-notifier/sqrx-email-notifier.esm.js" type="module" data-resources-url="chrome-extension://kapjaoifikajdcdehfdlmojlepfpkpoe/sqrx-email-notifier/" data-stencil-namespace="sqrx-email-notifier" id="sqrx-email-notifier"></script><script src="chrome-extension://kapjaoifikajdcdehfdlmojlepfpkpoe/sqrx-dialog/sqrx-dialog.esm.js" type="module" data-resources-url="chrome-extension://kapjaoifikajdcdehfdlmojlepfpkpoe/sqrx-dialog/" data-stencil-namespace="sqrx-dialog" id="sqrx-dialog"></script><script src="chrome-extension://kapjaoifikajdcdehfdlmojlepfpkpoe/sqrx-notification-bar/sqrx-notification-bar.esm.js" type="module" data-resources-url="chrome-extension://kapjaoifikajdcdehfdlmojlepfpkpoe/sqrx-notification-bar/" data-stencil-namespace="sqrx-notification-bar" id="sqrx-notification-bar"></script></head>
<body data-new-gr-c-s-check-loaded="14.1195.0" data-gr-ext-installed="">
<div id="gameContainer">
<canvas id="gameCanvas" width="400" height="400"></canvas>
<div id="score">Score: 0</div>
<button id="startButton">Start Game</button>
<button id="downloadButton"></button>
<div id="highScore">Highest Score: 0</div>
</div>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const scoreElement = document.getElementById('score');
const highScoreElement = document.getElementById('highScore');
const startButton = document.getElementById('startButton');
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 = 0;
let gameInterval;
let gameRunning = false;
function drawGame() {
clearCanvas();
moveSnake();
drawFood();
drawSnake();
checkCollision();
updateScore();
}
function clearCanvas() {
ctx.fillStyle = '#000';
ctx.fillRect(0, 0, canvas.width, canvas.height);
}
function drawPixel(x, y, color) {
ctx.fillStyle = color;
ctx.fillRect(x * gridSize, y * gridSize, gridSize - 2, gridSize - 2);
}
function drawSnake() {
snake.forEach((segment, index) => {
const color = index === 0 ? '#4CAF50' : '#45a049';
drawPixel(segment.x, segment.y, color);
});
}
function drawFood() {
drawPixel(food.x, food.y, '#FF0000');
}
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++;
generateFood();
} else {
snake.pop();
}
}
function generateFood() {
food.x = Math.floor(Math.random() * tileCount);
food.y = Math.floor(Math.random() * tileCount);
}
function checkCollision() {
const head = snake[0];
if (head.x < 0 || head.x >= tileCount || head.y < 0 || head.y >= tileCount) {
endGame();
}
for (let i = 1; i < snake.length; i++) {
if (head.x === snake[i].x && head.y === snake[i].y) {
endGame();
}
}
}
function resetGame() {
snake = [{ x: 10, y: 10 }];
food = { x: 15, y: 15 };
dx = 0;
dy = 0;
score = 0;
updateScore();
}
function updateScore() {
scoreElement.textContent = `Score: ${score}`;
if (score > highScore) {
highScore = score;
highScoreElement.textContent = `Highest Score: ${highScore}`;
}
}
function startGame() {
if (!gameRunning) {
resetGame();
gameRunning = true;
startButton.textContent = 'Restart Game';
gameInterval = setInterval(drawGame, 100);
} else {
endGame();
startGame();
}
}
function endGame() {
clearInterval(gameInterval);
gameRunning = false;
startButton.textContent = 'Start Game';
updateScore();
}
document.addEventListener('keydown', (e) => {
if (gameRunning) {
switch (e.key) {
case 'ArrowUp':
if (dy === 0) { dx = 0; dy = -1; }
break;
case 'ArrowDown':
if (dy === 0) { dx = 0; dy = 1; }
break;
case 'ArrowLeft':
if (dx === 0) { dx = -1; dy = 0; }
break;
case 'ArrowRight':
if (dx === 0) { dx = 1; dy = 0; }
break;
}
}
});
startButton.addEventListener('click', startGame);
downloadButton.addEventListener('click', () => {
const sourceCode = document.documentElement.outerHTML;
const blob = new Blob([sourceCode], { type: 'text/html' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'pixel_snake_game.html';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
});
clearCanvas();
drawSnake();
drawFood();
</script>
<div id="sqrx-content-container"><div class="squarex_ext_modal"><div class="squarex_ext_dialog-container"></div></div></div></body><grammarly-desktop-integration data-grammarly-shadow-root="true"></grammarly-desktop-integration></html>