-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMGsnake.html
More file actions
331 lines (281 loc) · 10.5 KB
/
MGsnake.html
File metadata and controls
331 lines (281 loc) · 10.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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
<!DOCTYPE html>
<html lang="zh-TW">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>迷你遊戲</title>
<link id="body-main-stylesheet" rel="stylesheet" href="css/bodyMain.css">
<link id="welcome-featured-profile-highlight-stylesheet" rel="stylesheet" href="css/welcomeFeatured.css">
<link id="nav-stylesheet" rel="stylesheet" href="css/navStyles.css">
<link id="news-stylesheet" rel="stylesheet" href="css/newsStyles.css">
<link id="register-auth-modal-stylesheet" rel="stylesheet" href="css/registerModal.css">
<link id="card-display-stylesheet" rel="stylesheet" href="css/card.css">
<link id="footer-celebrate-btn-stylesheet" rel="stylesheet" href="css/footerStyles.css">
<style>
html,
body {
overflow: hidden;
/* 禁用整個頁面的滾動 */
/* 遊戲區域樣式 */
}
#game {
display: flex;
flex-direction: column;
align-items: center;
}
#game-canvas {
border: 2px solid #8f8b8b;
/* 蛇蛇遊戲畫布邊框 */
}
#score-board {
position: fixed;
/* 固定位置 */
right: 20px;
/* 距離左邊 20px */
top: 50%;
/* 垂直居中 */
transform: translateY(-50%);
/* 垂直居中對齊 */
padding: 10px;
background: linear-gradient(to bottom, #414453 0%, #231e3a 100%);
border: 1px solid #ccc;
/* 邊框樣式 */
border-radius: 5px;
/* 邊角圓潤 */
text-align: center;
/* 文字居中 */
z-index: 9999;
/* 確保區域在其他元素之上 */
}
#score-board p {
font-size: 18px;
}
#start-btn {
position: fixed;
/* 固定位置 */
right: 100px;
/* 距離左邊 20px */
top: 50%;
/* 垂直居中 */
transform: translateY(-50%);
/* 垂直居中對齊 */
padding: 10px 20px;
font-size: 20px;
color: #fff;
border: none;
border-radius: 5px;
/* 邊角圓潤 */
cursor: pointer;
z-index: 9999;
/* 確保按鈕在其他元素之上 */
background-color: #9673a3;
/* 按鈕背景色 */
}
#start-btn:hover {
background-color: #ccbf75;
/* 按鈕懸停顏色 */
}
</style>
<script>
document.addEventListener('DOMContentLoaded', () => {
const nav = document.querySelector('nav');
// 當滑鼠懸停時展開導航欄
nav.addEventListener('mouseover', () => {
nav.classList.add('expanded');
});
// 當滑鼠移開時縮回導航欄
nav.addEventListener('mouseout', () => {
nav.classList.remove('expanded');
});
// 圖片懸停效果
const portfolioItems = document.querySelectorAll('.portfolio-item');
portfolioItems.forEach(item => {
item.addEventListener('mouseover', () => {
// 縮放圖片
item.querySelector('img').style.transform = 'scale(1.1)';
});
item.addEventListener('mouseout', () => {
// 恢復圖片大小
item.querySelector('img').style.transform = 'scale(1)';
});
});
// 平滑滾動到部分
const navLinks = document.querySelectorAll('nav ul li a');
navLinks.forEach(link => {
link.addEventListener('click', (event) => {
const targetId = link.getAttribute('href').substring(1); // 獲取目標部分的 ID
const targetElement = document.getElementById(targetId);
if (targetElement) {
targetElement.scrollIntoView({ behavior: 'smooth' }); // 平滑滾動到目標部分
}
});
});
});
</script>
<!-- 主頁樣式表 -->
</head>
<body>
<nav>
<img src="img/BACKBTN-01-01.png" alt="backBTN" class="backBTN">
<h1>貪吃蛇蛇</h1>
<ul>
<li><a href="index.html">首頁</a></li>
<li><a href="portfolio.html">過去作品</a></li>
<li><a href="LearningDiary.html">學習日誌</a></li>
<li><a href="viewport.html">學習資源推薦</a></li>
<li><a href="mini-games.html">迷你遊戲</a></li>
<li><a href="contact.html">聯絡</a></li>
</ul>
</nav>
<main>
<section id="MGsnake" class="profile">
<div class="welcome-text">
<h2>貪吃蛇遊戲</h2>
<p>這是一個經典的貪吃蛇遊戲。用箭頭鍵控制蛇的方向,吃到食物會增加分數,但是蛇不會變長。</p>
</div>
<p>會變長的版本請看:</p>
<a href="https://tananis.itch.io/inake" target="_blank">itch.io</a>
</section>
<section id="game">
<canvas id="game-canvas" width="400" height="400"></canvas>
<div id="score-board">
<p>Score: <span id="score">0</span></p>
<button id="start-btn">Start</button>
</div>
</section>
</main>
<footer class="footerBG">
<p>© 2024 版權所有.</p>
</footer>
<script>
const canvas = document.getElementById('game-canvas');
const ctx = canvas.getContext('2d');
const width = canvas.width;
const height = canvas.height;
const cellSize = 20;
const gridSize = 20;
const speed = 100;
const snakeLength = 3;
const foodCount = 1;
const scoreBoard = document.getElementById('score-board');
const score = document.getElementById('score');
const startBtn = document.getElementById('start-btn');
let gameOver = false;
let scoreCount = 0;
let snake = [];
let food = [];
let direction = 'right';
let intervalId = null;
function init() {
gameOver = false;
scoreCount = 0;
snake = [];
food = [];
direction = 'right';
// 將蛇初始化為不重疊
for (let i = 0; i < snakeLength; i++) {
snake.push({ x: gridSize * (snakeLength - i - 1), y: 0 });
}
placeFood();
draw();
}
function placeFood() {
let x = Math.floor(Math.random() * (width / gridSize)) * gridSize;
let y = Math.floor(Math.random() * (height / gridSize)) * gridSize;
food = [{ x: x, y: y }];
}
function draw() {
ctx.clearRect(0, 0, width, height);
for (let x = 0; x < width; x += gridSize) {
for (let y = 0; y < height; y += gridSize) {
ctx.fillStyle = (x + y) % 2 === 0 ? '#fff' : '#ddd';
ctx.fillRect(x, y, gridSize, gridSize);
}
}
for (let i = 0; i < snake.length; i++) {
ctx.fillStyle = i === 0 ? '#000' : '#fff';
ctx.fillRect(snake[i].x, snake[i].y, cellSize, cellSize);
}
ctx.fillStyle = '#f00';
ctx.fillRect(food[0].x, food[0].y, cellSize, cellSize);
score.textContent = scoreCount;
}
function moveSnake() {
let head = { x: snake[0].x, y: snake[0].y };
switch (direction) {
case 'up':
head.y -= gridSize;
break;
case 'down':
head.y += gridSize;
break;
case 'left':
head.x -= gridSize;
break;
case 'right':
head.x += gridSize;
break;
}
if (head.x === food[0].x && head.y === food[0].y) {
// 吃到食物,增加分數
scoreCount++;
score.textContent = scoreCount;
// 增長蛇身體長度
addBody();
addBody(); // 再增加一節身體
placeFood(); // 重新放置食物
} else {
snake.pop(); // 移除蛇尾
}
snake.unshift(head); // 更新蛇頭位置
// 檢查遊戲結束
if (head.x < 0 || head.x + cellSize > width || head.y < 0 || head.y + cellSize > height || snake.slice(1).some(s => s.x === head.x && s.y === head.y)) {
gameOver = true;
scoreBoard.style.display = 'none';
alert('Game Over!');
setTimeout(function () {
location.reload(); // 重新載入頁面
}, 1000); // 1秒後自動重整頁面
return;
}
}
function addBody() {
let tail = snake[snake.length - 1];
snake.push({ x: tail.x, y: tail.y });
console.log('Snake length:', snake.length); // 調試信息
}
function controlSnake() {
if (gameOver) {
clearInterval(intervalId);
return;
}
moveSnake();
draw();
}
startBtn.addEventListener('click', function () {
scoreBoard.style.display = 'block';
init();
intervalId = setInterval(controlSnake, speed);
});
document.addEventListener('keydown', function (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;
}
});
</script>
</body>
<footer class="footerBG">
<p class="footer-text">© 2024 版權所有.</p>
</footer>
</html>