- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
Latest commit
217 lines (181 loc) · 5.71 KB
/
Copy pathscript.js
File metadata and controls
217 lines (181 loc) · 5.71 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
constcanvas=document.getElementById("gameCanvas");
constctx=canvas.getContext("2d");
constscoreEl=document.getElementById("score");
constmessageEl=document.getElementById("message");
constrestartBtn=document.getElementById("restartBtn");
constwrapAroundCheckbox=document.getElementById("wrapAroundCheckbox");// 新增:穿墙复选框
constgridSize=20;
consttileCount=canvas.width/gridSize;
constgameSpeedMs=120;
letsnake;
letdirection;
letqueuedDirection;
letinputQueue;// 新增:输入队列,用于缓存按键指令
letfood;
letscore;
letisGameOver;
letisGameStarted;// 新增:游戏是否已开始
letisPaused;// 新增:游戏是否暂停
letgameLoopId;
functionrandomTile(){
returnMath.floor(Math.random()*tileCount);
}
functionspawnFood(){
letnextFood={x: randomTile(),y: randomTile()};
while(snake.some((part)=>part.x===nextFood.x&&part.y===nextFood.y)){
nextFood={x: randomTile(),y: randomTile()};
}
food=nextFood;
}
functionresetGame(){
snake=[
{x: 10,y: 10},
{x: 9,y: 10},
{x: 8,y: 10},
];
direction={x: 1,y: 0};
queuedDirection={x: 1,y: 0};
inputQueue=[];// 初始化输入队列
score=0;
isGameOver=false;
isGameStarted=false;// 重置后不自动开始
isPaused=false;// 重置暂停状态
scoreEl.textContent="0";
messageEl.textContent="按Enter开始游戏";// 显示开始提示
spawnFood();
if(gameLoopId){
clearInterval(gameLoopId);
}
draw();// 只绘制初始画面,不启动游戏循环
}
functionstartGame(){
if(isGameStarted||isGameOver)return;
isGameStarted=true;
isPaused=false;
messageEl.textContent="";// 清除提示
if(gameLoopId){
clearInterval(gameLoopId);
}
gameLoopId=setInterval(update,gameSpeedMs);
}
functiontogglePause(){
if(!isGameStarted||isGameOver)return;
isPaused=!isPaused;
if(isPaused){
clearInterval(gameLoopId);
messageEl.textContent="已暂停 - 按P继续";
}else{
messageEl.textContent="";
gameLoopId=setInterval(update,gameSpeedMs);
}
}
functionsetDirection(nextX,nextY){
if(isGameOver||!isGameStarted)return;
// Prevent turning directly into your own body.
if(direction.x===-nextX&&direction.y===-nextY){
return;
}
// 将方向加入输入队列,而不是直接设置
constlastQueued=inputQueue.length>0
? inputQueue[inputQueue.length-1]
: queuedDirection;
// 防止在同一帧内连续转向自身
if(lastQueued.x===-nextX&&lastQueued.y===-nextY){
return;
}
inputQueue.push({x: nextX,y: nextY});
}
functionupdate(){
if(isGameOver||!isGameStarted)return;
// 从输入队列中取出下一个方向(如果有的话)
if(inputQueue.length>0){
queuedDirection=inputQueue.shift();
}
direction=queuedDirection;
letnewHead={
x: snake[0].x+direction.x,
y: snake[0].y+direction.y,
};
// 处理穿墙逻辑
constwrapAround=wrapAroundCheckbox.checked;
if(wrapAround){
// 穿墙模式:从一边出去,从另一边进来
if(newHead.x<0)newHead.x=tileCount-1;
if(newHead.x>=tileCount)newHead.x=0;
if(newHead.y<0)newHead.y=tileCount-1;
if(newHead.y>=tileCount)newHead.y=0;
}
consthitWall=!wrapAround&&(
newHead.x<0||
newHead.x>=tileCount||
newHead.y<0||
newHead.y>=tileCount
);
consthitSelf=snake.some((part)=>part.x===newHead.x&&part.y===newHead.y);
if(hitWall||hitSelf){
isGameOver=true;
isGameStarted=false;
clearInterval(gameLoopId);
messageEl.textContent="游戏结束 - 按Enter或Restart以重新开始";
draw();
return;
}
snake.unshift(newHead);
if(newHead.x===food.x&&newHead.y===food.y){
score+=1;
scoreEl.textContent=String(score);
spawnFood();
}else{
snake.pop();
}
draw();
}
functiondraw(){
ctx.fillStyle="#0b1220";
ctx.fillRect(0,0,canvas.width,canvas.height);
ctx.fillStyle="#ef4444";
ctx.fillRect(food.x*gridSize,food.y*gridSize,gridSize,gridSize);
snake.forEach((part,index)=>{
ctx.fillStyle=index===0 ? "#22c55e" : "#4ade80";
ctx.fillRect(part.x*gridSize,part.y*gridSize,gridSize,gridSize);
});
// Draw light grid lines for easier visual tracking.
ctx.strokeStyle="rgba(255,255,255,0.06)";
for(leti=0;i<=tileCount;i+=1){
ctx.beginPath();
ctx.moveTo(i*gridSize,0);
ctx.lineTo(i*gridSize,canvas.height);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(0,i*gridSize);
ctx.lineTo(canvas.width,i*gridSize);
ctx.stroke();
}
}
window.addEventListener("keydown",(event)=>{
constkey=event.key.toLowerCase();
// 处理Enter键开始游戏
if(key==="enter"){
if(!isGameStarted&&!isGameOver){
startGame();
}elseif(isGameOver){
resetGame();
setTimeout(()=>startGame(),100);// 短暂延迟后开始
}
return;
}
// 处理P键暂停/继续
if(key==="p"){
togglePause();
return;
}
// 方向键处理(暂停时不允许移动)
if(key==="arrowup"||key==="w")setDirection(0,-1);
if(key==="arrowdown"||key==="s")setDirection(0,1);
if(key==="arrowleft"||key==="a")setDirection(-1,0);
if(key==="arrowright"||key==="d")setDirection(1,0);
});
restartBtn.addEventListener("click",()=>{
resetGame();
});
resetGame();