当前位置:   article > 正文

JS贪吃蛇_贪吃蛇游戏js代码

贪吃蛇游戏js代码

300行JS代码,让你重回经典

诺基亚贪吃蛇——我的游戏启蒙

  儿时的经典游戏,无非俄罗斯方块、贪吃蛇、推箱子,之后才有了智能拼图、麻将、象棋、连连看、消消乐。今天拿出来一款JS贪吃蛇供大家学习、参考。
  游戏玩法不多做赘述,可以调整关卡,可以调整爬行速度,祝您游戏愉快。

部分游戏截图:
在这里插入图片描述

在这里插入图片描述
(如果你对前端还比较陌生,偶然刷到了这篇文章又不知该如何操作,请点击“ 这里 ”,一次就能学废)

<!DOCTYPE html>
<html>
<head>
	<title>贪吃蛇</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
	<style type="text/css">
		.backDiv {
			text-align: center;
			position:absolute;
			top:20px;
			left:300px;
			width:820px;
			height:550px;
		}
		.display {
			position:absolute;
			top:0px;
			left:685px;
			width:180px;
			height:510px;
			border: 5px solid black;
			background-color: #ccffff;
		}
		#gamePanel table {
			border-collapse: collapse;/*合并单元格边框*/
			border: 5px solid black;
			background-color: #f0f0f0;
		}
		#gamePanel td {
			width:15px;
			height:15px;
		}
		#scoreDiv {
			position: absolute;
			top: 20px;
			left:20px;
			text-align: left;
			font-size: 20px;
			font-weight: bold;
		}
		#prompt {
			position: absolute;
			top: 180px;
			left: 20px;
			text-align: left;
			font-weight: bold;
		}
		#operator {
			position: absolute;
			top:550px;
			left:100px;
		}
		#result {
			position: absolute;
			top:200px;
			left:220px;
			background-color: #00ffff;
			font-size: 20px;
			font-weight: bold;
			text-align: left;
			padding: 20px;
		}
	</style>
	<script>
	var snake = [];// 蛇数组
	var barrers = [];// 障碍物数组
	var game = {sizeX:"30",sizeY:"40",record:"0"};
	var appearTime = disappearTime =  0;// 超级食物出现
	var superFoodX = superFoodY = null;
	var color = 0;// 食物颜色
	function init(){
		clearTimeout(game.time);
		snake = [];
		barrers = [];
		game.x = 0;game.y = 1;
		game.dir = null;
		game.direction = "right";
		game.rate = null;
		game.lengths = 2;// 蛇的长度
		game.score = 0;
		$("scoreDiv").innerHTML = "得分:"+game.score+"<br/><br/>长度:"+ game.lengths+"<br/><br/>"+"最高分:"+game.record;
		$("pass").disabled = false;
		$("rate").disabled = false;
		$("start").disabled = false;
		bord();// 设置界面
		barrer();// 设置障碍物
		$("result").style.visibility = "hidden";
		snake.push("tb_" + game.x + "_" + (game.y-1));
		snake.push("tb_" + game.x + "_" + game.y);// 蛇头
	}
	function $(id){
		return document.getElementById(id);
	}
	// 表格
	function bord(){
		var panel = [];// 游戏面板
		var proPanel = [];// 提示面板
		panel.push("<table>");
		for(var i=0; i<game.sizeX; i++){
			panel.push("<tr>");
			for(var j=0; j<game.sizeY; j++){
				panel.push('<td id="tb_' + i + "_" + j + '" > </td>');
			}
			panel.push("</tr>");
		}
		panel.push("</table>");
		$("gamePanel").innerHTML = panel.join("");
		proPanel.push("<table>");
		proPanel.push('<tr>'+'<td width="20px" height="20px" bgcolor="#ff3300"></td>' + '<td>&nbsp得分+10</td>'+'</tr>');
		proPanel.push('<tr>'+'<td width="20px" height="20px" bgcolor="#33cc33"></td>' + '<td>&nbsp得分+30</td>'+'</tr>');
		proPanel.push('<tr>'+'<td width="20px" height="20px" bgcolor="#99ff66"></td>' + '<td>&nbsp得分+50</td>'+'</tr>');
		proPanel.push('<tr>'+'<td width="20px" height="20px" bgcolor="#ffff00"></td>' + '<td>&nbsp得分+100</td>'+'</tr>');
		proPanel.push('<tr>'+'<td width="20px" height="20px" bgcolor="#006600"></td>' + '<td>&nbsp得分-30</td>'+'</tr>');
		proPanel.push('<tr>'+'<td width="20px" height="20px" bgcolor="#000000"></td>' + '<td>&nbsp得分-50</td>');
		proPanel.push("</table>");
		$("prompt").innerHTML = proPanel.join("");
	}
	// 关卡障碍物设置
	function barrer() {
		var passValue = $("pass").value;
		for(var i=0; i<barrers.length; i++) {
			$(barrers[i]).style.backgroundColor = "";
		}
		barrers = [];
		if(passValue == 2) {
			for(var i=8; i<game.sizeX-8; i++) {
				barrers.push("tb_" + i + "_" + 10);
				barrers.push("tb_" + i + "_" + (game.sizeY-10));
			}
		}
		if(passValue == 3) {
			for(var i=8; i<game.sizeY-8; i++) {
				barrers.push("tb_" + Math.floor(game.sizeX/2) + "_" + i);
			}
			for(var i=6; i<game.sizeX-6; i++) {
				barrers.push("tb_" + i + "_" + Math.floor(game.sizeY/2));
			}
		}
		if(passValue == 4) {
			for(var i=12; i<game.sizeY-11; i++) {
				barrers.push("tb_" + Math.floor(game.sizeX/2) + "_" + i);
			}
			for(var j=6; j<game.sizeX-6; j++) {
				barrers.push("tb_" + j + "_" + Math.floor(game.sizeY/2+1));
			}
			for(var i=6; i<game.sizeX-6; i++) {
				if(i < game.sizeX/2) {barrers.push("tb_"+i+"_"+12);}
				else{barrers.push("tb_"+i+"_"+(game.sizeY-11));}
			}
			for(var j=12; j<game.sizeY-11; j++) {
				if(j <= game.sizeY/2) {barrers.push("tb_"+(game.sizeX-7)+"_"+j);}
				else {barrers.push("tb_"+6+"_"+(j+1));}
			}
		}
		if(passValue == 5) {
			for(var i=0; i<14; i++) {
				barrers.push("tb_" + 7 + "_" + i);
				barrers.push("tb_" + (game.sizeX-8) + "_" + (game.sizeY-i-1));
			}
			for(var i=15; i<25; i++) {
				if(i < 18 || i > 21){
					barrers.push("tb_"+10 +"_"+i);
					barrers.push("tb_"+(game.sizeX-11)+"_"+i);
				}
			}
			for(var i=0; i<9; i++) {
				barrers.push("tb_" + i + "_" + (game.sizeY-13));
				barrers.push("tb_" + (game.sizeX-i-1) + "_" + 12);
			}
			for(var i=11; i<19; i++) {
				if(i < 13 || i > 16){
					barrers.push("tb_"+i+"_"+15);
					barrers.push("tb_"+i+"_"+(game.sizeY-16));
				}
			}
		}
		// 设置障碍物颜色
		for(var i=0; i<barrers.length; i++) {
			$(barrers[i]).style.backgroundColor = "#660033";
		}
	}
	// 开始游戏
	function startGame() {
		food();
		game.start = true;
		$("pass").disabled = true;
		$("rate").disabled = true;
		$("start").disabled = true;
		game.rate = $("rate").value;
		$(snake[0]).style.backgroundColor = "#0066ff";
		$(snake[1]).style.backgroundColor = "blue";
		game.time = setTimeout(function(){
			snakeMove();
		},game.rate);
		superFood();
	}
	// 蛇移动 速度,方向
	function snakeMove() {
		if(game.direction == "right") {game.y += 1; }// 右移
		if(game.direction == "left") {game.y -= 1; }// 左移
		if(game.direction == "up") {game.x -= 1; }// 上移
		if(game.direction == "down") {game.x += 1; }// 下移
		// 判断游戏是否结束
		if(result() == true) {
			clearTimeout(game.time); clearTimeout(appearTime); clearTimeout(disappearTime);
			game.start = false;
			var res = "游戏结束!<br/>";
			if(game.score > game.record){
				res += "恭喜你破纪录啦!<br/>";
				$("scoreDiv").innerHTML = "得分:" + game.score + "<br/><br/>最高分:" + game.record;
			}
			res += "您的得分为:" + game.score + "<br/>长度:"+game.lengths;
			$("result").style.visibility = "visible";
			$("result").innerHTML = res;
			return;
		}
		if(game.x==game.fx && game.y==game.fy) {eat(1);}
		if(game.x==superFoodX && game.y==superFoodY) {eat(2);}
		move();
		game.time = setTimeout(function() {
			snakeMove(game.rate,game.direction);
		},game.rate);
	}
	function move() {
		$(snake[0]).style.backgroundColor = "";
		// 保留蛇当前的位置
		for(var i=1; i<snake.length; i++) {
			snake[i-1] = snake[i];
		}
		snake[snake.length-1] = "tb_" + game.x + "_" + game.y;// 蛇头位置
		for(var i=0; i<snake.length-1; i++) {
			$(snake[i]).style.backgroundColor = "#0066ff";
		}
		$(snake[i]).style.backgroundColor = "blue";
	}
	// 食物 位置随机且不能与蛇位置重复
	function food() {
		game.fx = Math.floor(Math.random(game.sizeX)*game.sizeX);
		game.fy = Math.floor(Math.random(game.sizeY)*game.sizeY);
		if($("tb_" + game.fx + "_" + game.fy).style.backgroundColor != "") {food(); }
		else {$("tb_" + game.fx + "_" + game.fy).style.backgroundColor = "#ff3300"; }
	}
	// 超级食物出现
	function superFood() {
		appearTime = setTimeout(function(){
			var n = Math.floor(Math.random(10)*10);
			superFoodX = Math.floor(Math.random(game.sizeX)*game.sizeX);
			superFoodY = Math.floor(Math.random(game.sizeY)*game.sizeY);
			if($("tb_" + superFoodX + "_" + superFoodY).style.backgroundColor != "") {superFood(); }
			else{
				if(("tb_" + superFoodX + "_" + superFoodY) == ("tb_" + game.fx + "_" + game.fy)){superFood(); return ;}
				if(n < 3){$("tb_" + superFoodX + "_" + superFoodY).style.backgroundColor = "#33cc33"; color = 0;}
				else if(3 <= n && n < 5){$("tb_" + superFoodX + "_" + superFoodY).style.backgroundColor = "#006600"; color = 1;}
				else if(5 <= n && n < 7){$("tb_" + superFoodX + "_" + superFoodY).style.backgroundColor = "#99ff66"; color = 2;}
				else if(7 <= n && n < 9){$("tb_" + superFoodX + "_" + superFoodY).style.backgroundColor = "#000000"; color = 3;}
				else {$("tb_" + superFoodX + "_" + superFoodY).style.backgroundColor = "#ffff00"; color = 4;}
				clearTimeout(disappearTime);
				superFood();
				// 一定时间后超级食物消失
				disappearTime = setTimeout(function(){
					$("tb_" + superFoodX + "_" + superFoodY).style.backgroundColor = "";
					superFoodX = superFoodY = null;
				},game.rate*120-game.rate*50);
			}
		},game.rate*120);
	}
	// 蛇吃食物
	function eat(ef) {
		// 吃普通食物
		if(ef == 1) {snake.push("tb_" + game.fx + "_" + game.fy); game.score += 10; food();}
		// 吃超级食物
		if(ef == 2) {
			if(color == 0) {game.score += 30;}
			else if(color == 1) {game.score -= 30;}
			else if(color == 2) {game.score += 50;}
			else if(color == 3) {game.score -= 50;}
			else {game.score += 100;}
			snake.push("tb_" + superFoodX + "_" + superFoodY);
			if(game.score < 0) {game.score = 0;}
			clearTimeout(disappearTime);
		}
		game.lengths += 1;
		$("scoreDiv").innerHTML = "得分:"+game.score+"<br/><br/>长度:"+game.lengths+"<br/><br/>最高分:"+game.record;
	}
	// 游戏结束
	function result() {
		var next = "tb_" + game.x + "_" + game.y;
		if(game.x<0 || game.x>=game.sizeX || game.y<0 || game.y>=game.sizeY) {return true;}
		for(var i=0; i<snake.length; i++) {if(next == snake[i]) {return true;}}
		for(var i=0; i<barrers.length; i++) {if(next == barrers[i]) {return true;}}
		return false;
	}
	// 键盘控制
	function control(v) {
		var evt = v;
		game.dir = game.direction;
		if(37 <= evt.keyCode && evt.keyCode <= 40 && game.start){
			if(evt.keyCode == 37){game.dir=="right" ? game.direction = "right":game.direction = "left";}// 左
			if(evt.keyCode == 38){game.dir=="down" ? game.direction = "down" : game.direction = "up";}// 上
			if(evt.keyCode == 39){game.dir=="left" ? game.direction = "left" : game.direction = "right";}// 右
			if(evt.keyCode == 40){game.dir=="up" ? game.direction = "up" : game.direction = "down";}// 下
			if(game.dir != game.direction){clearTimeout(game.time); snakeMove(); }
		}
	}
	</script>
</head>
<body onload="init();" onkeyup="control(event);" >
<div class="backDiv">
	<div class="display">
		<div id="scoreDiv">分数:0</div><br/>
		<div id="prompt"></div>
	</div>
	<div id="gamePanel" ></div><br/>
	<div id="operator">
		选择关卡:<select id="pass" onchange="barrer();">
			<option value="1">关卡1</option>
			<option value="2">关卡2</option>
			<option value="3">关卡3</option>
			<option value="4">关卡4</option>
			<option value="5">关卡5</option>
		</select>&nbsp&nbsp
		速度:<select id="rate">
			<option value="500"></option>
			<option value="250"></option>
			<option value="100"></option>
		</select>&nbsp&nbsp
		<input type="button" id="start" value="开始" onclick="startGame();" />&nbsp&nbsp
		<input type="button" id="resets" value="重玩" onclick="init();" />
	</div>
	<div id="result"></div>
</div>
</body>
</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
  • 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
  • 332
  • 333
文章中的主体部分都添加了注释,不懂的地方可以评论在下方,博主常在,欢迎探讨学习,能帮到路过的你,我非常荣幸

如果你觉得我的代码还算有趣,在你的学习中能有所帮助,请查看我的置顶文章,我由衷感谢!

推荐阅读:

游动的花花肠子

阿波罗的轻语

拟态小象

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/626120
推荐阅读
相关标签
  

闽ICP备14008679号