当前位置:   article > 正文

Js 贪吃蛇_js贪吃蛇有障碍

js贪吃蛇有障碍

 

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>贪吃蛇</title>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <style type="text/css">
  7. .backDiv {
  8. text-align: center;
  9. position: absolute;
  10. top: 20px;
  11. left: 300px;
  12. width: 820px;
  13. height: 550px;
  14. }
  15. .display {
  16. position: absolute;
  17. top: 0px;
  18. left: 685px;
  19. width: 180px;
  20. height: 510px;
  21. border: 5px solid black;
  22. background-color: #ccffff;
  23. }
  24. #gamePanel table {
  25. border-collapse: collapse; /*合并单元格边框*/
  26. border: 5px solid black;
  27. background-color: #f0f0f0;
  28. }
  29. #gamePanel td {
  30. width: 15px;
  31. height: 15px;
  32. }
  33. #scoreDiv {
  34. position: absolute;
  35. top: 20px;
  36. left: 20px;
  37. text-align: left;
  38. font-size: 20px;
  39. font-weight: bold;
  40. }
  41. #prompt {
  42. position: absolute;
  43. top: 180px;
  44. left: 20px;
  45. text-align: left;
  46. font-weight: bold;
  47. }
  48. #operator {
  49. position: absolute;
  50. top: 550px;
  51. left: 100px;
  52. }
  53. #result {
  54. position: absolute;
  55. top: 200px;
  56. left: 220px;
  57. background-color: #00ffff;
  58. font-size: 20px;
  59. font-weight: bold;
  60. text-align: left;
  61. padding: 20px;
  62. }
  63. </style>
  64. <script>
  65. var snake = []; // 蛇数组
  66. var barrers = []; // 障碍物数组
  67. var game = { sizeX: '30', sizeY: '40', record: '0' };
  68. var appearTime = (disappearTime = 0); // 超级食物出现
  69. var superFoodX = (superFoodY = null);
  70. var color = 0; // 食物颜色
  71. function init() {
  72. clearTimeout(game.time);
  73. snake = [];
  74. barrers = [];
  75. game.x = 0;
  76. game.y = 1;
  77. game.dir = null;
  78. game.direction = 'right';
  79. game.rate = null;
  80. game.lengths = 2; // 蛇的长度
  81. game.score = 0;
  82. $('scoreDiv').innerHTML =
  83. '得分:' +
  84. game.score +
  85. '<br/><br/>长度:' +
  86. game.lengths +
  87. '<br/><br/>' +
  88. '最高分:' +
  89. game.record;
  90. $('pass').disabled = false;
  91. $('rate').disabled = false;
  92. $('start').disabled = false;
  93. bord(); // 设置界面
  94. barrer(); // 设置障碍物
  95. $('result').style.visibility = 'hidden';
  96. snake.push('tb_' + game.x + '_' + (game.y - 1));
  97. snake.push('tb_' + game.x + '_' + game.y); // 蛇头
  98. }
  99. function $(id) {
  100. return document.getElementById(id);
  101. }
  102. // 表格
  103. function bord() {
  104. var panel = []; // 游戏面板
  105. var proPanel = []; // 提示面板
  106. panel.push('<table>');
  107. for (var i = 0; i < game.sizeX; i++) {
  108. panel.push('<tr>');
  109. for (var j = 0; j < game.sizeY; j++) {
  110. panel.push('<td id="tb_' + i + '_' + j + '" > </td>');
  111. }
  112. panel.push('</tr>');
  113. }
  114. panel.push('</table>');
  115. $('gamePanel').innerHTML = panel.join('');
  116. proPanel.push('<table>');
  117. proPanel.push(
  118. '<tr>' +
  119. '<td width="20px" height="20px" bgcolor="#ff3300"></td>' +
  120. '<td>&nbsp得分+10</td>' +
  121. '</tr>'
  122. );
  123. proPanel.push(
  124. '<tr>' +
  125. '<td width="20px" height="20px" bgcolor="#33cc33"></td>' +
  126. '<td>&nbsp得分+30</td>' +
  127. '</tr>'
  128. );
  129. proPanel.push(
  130. '<tr>' +
  131. '<td width="20px" height="20px" bgcolor="#99ff66"></td>' +
  132. '<td>&nbsp得分+50</td>' +
  133. '</tr>'
  134. );
  135. proPanel.push(
  136. '<tr>' +
  137. '<td width="20px" height="20px" bgcolor="#ffff00"></td>' +
  138. '<td>&nbsp得分+100</td>' +
  139. '</tr>'
  140. );
  141. proPanel.push(
  142. '<tr>' +
  143. '<td width="20px" height="20px" bgcolor="#006600"></td>' +
  144. '<td>&nbsp得分-30</td>' +
  145. '</tr>'
  146. );
  147. proPanel.push(
  148. '<tr>' +
  149. '<td width="20px" height="20px" bgcolor="#000000"></td>' +
  150. '<td>&nbsp得分-50</td>'
  151. );
  152. proPanel.push('</table>');
  153. $('prompt').innerHTML = proPanel.join('');
  154. }
  155. // 关卡障碍物设置
  156. function barrer() {
  157. var passValue = $('pass').value;
  158. for (var i = 0; i < barrers.length; i++) {
  159. $(barrers[i]).style.backgroundColor = '';
  160. }
  161. barrers = [];
  162. if (passValue == 2) {
  163. for (var i = 8; i < game.sizeX - 8; i++) {
  164. barrers.push('tb_' + i + '_' + 10);
  165. barrers.push('tb_' + i + '_' + (game.sizeY - 10));
  166. }
  167. }
  168. if (passValue == 3) {
  169. for (var i = 8; i < game.sizeY - 8; i++) {
  170. barrers.push('tb_' + Math.floor(game.sizeX / 2) + '_' + i);
  171. }
  172. for (var i = 6; i < game.sizeX - 6; i++) {
  173. barrers.push('tb_' + i + '_' + Math.floor(game.sizeY / 2));
  174. }
  175. }
  176. if (passValue == 4) {
  177. for (var i = 12; i < game.sizeY - 11; i++) {
  178. barrers.push('tb_' + Math.floor(game.sizeX / 2) + '_' + i);
  179. }
  180. for (var j = 6; j < game.sizeX - 6; j++) {
  181. barrers.push('tb_' + j + '_' + Math.floor(game.sizeY / 2 + 1));
  182. }
  183. for (var i = 6; i < game.sizeX - 6; i++) {
  184. if (i < game.sizeX / 2) {
  185. barrers.push('tb_' + i + '_' + 12);
  186. } else {
  187. barrers.push('tb_' + i + '_' + (game.sizeY - 11));
  188. }
  189. }
  190. for (var j = 12; j < game.sizeY - 11; j++) {
  191. if (j <= game.sizeY / 2) {
  192. barrers.push('tb_' + (game.sizeX - 7) + '_' + j);
  193. } else {
  194. barrers.push('tb_' + 6 + '_' + (j + 1));
  195. }
  196. }
  197. }
  198. if (passValue == 5) {
  199. for (var i = 0; i < 14; i++) {
  200. barrers.push('tb_' + 7 + '_' + i);
  201. barrers.push('tb_' + (game.sizeX - 8) + '_' + (game.sizeY - i - 1));
  202. }
  203. for (var i = 15; i < 25; i++) {
  204. if (i < 18 || i > 21) {
  205. barrers.push('tb_' + 10 + '_' + i);
  206. barrers.push('tb_' + (game.sizeX - 11) + '_' + i);
  207. }
  208. }
  209. for (var i = 0; i < 9; i++) {
  210. barrers.push('tb_' + i + '_' + (game.sizeY - 13));
  211. barrers.push('tb_' + (game.sizeX - i - 1) + '_' + 12);
  212. }
  213. for (var i = 11; i < 19; i++) {
  214. if (i < 13 || i > 16) {
  215. barrers.push('tb_' + i + '_' + 15);
  216. barrers.push('tb_' + i + '_' + (game.sizeY - 16));
  217. }
  218. }
  219. }
  220. // 设置障碍物颜色
  221. for (var i = 0; i < barrers.length; i++) {
  222. $(barrers[i]).style.backgroundColor = '#660033';
  223. }
  224. }
  225. // 开始游戏
  226. function startGame() {
  227. food();
  228. game.start = true;
  229. $('pass').disabled = true;
  230. $('rate').disabled = true;
  231. $('start').disabled = true;
  232. game.rate = $('rate').value;
  233. $(snake[0]).style.backgroundColor = '#0066ff';
  234. $(snake[1]).style.backgroundColor = 'blue';
  235. game.time = setTimeout(function () {
  236. snakeMove();
  237. }, game.rate);
  238. superFood();
  239. }
  240. // 蛇移动 速度,方向
  241. function snakeMove() {
  242. if (game.direction == 'right') {
  243. game.y += 1;
  244. } // 右移
  245. if (game.direction == 'left') {
  246. game.y -= 1;
  247. } // 左移
  248. if (game.direction == 'up') {
  249. game.x -= 1;
  250. } // 上移
  251. if (game.direction == 'down') {
  252. game.x += 1;
  253. } // 下移
  254. // 判断游戏是否结束
  255. if (result() == true) {
  256. clearTimeout(game.time);
  257. clearTimeout(appearTime);
  258. clearTimeout(disappearTime);
  259. game.start = false;
  260. var res = '游戏结束!<br/>';
  261. if (game.score > game.record) {
  262. res += '恭喜你破纪录啦!<br/>';
  263. $('scoreDiv').innerHTML =
  264. '得分:' + game.score + '<br/><br/>最高分:' + game.record;
  265. }
  266. res += '您的得分为:' + game.score + '<br/>长度:' + game.lengths;
  267. $('result').style.visibility = 'visible';
  268. $('result').innerHTML = res;
  269. return;
  270. }
  271. if (game.x == game.fx && game.y == game.fy) {
  272. eat(1);
  273. }
  274. if (game.x == superFoodX && game.y == superFoodY) {
  275. eat(2);
  276. }
  277. move();
  278. game.time = setTimeout(function () {
  279. snakeMove(game.rate, game.direction);
  280. }, game.rate);
  281. }
  282. function move() {
  283. $(snake[0]).style.backgroundColor = '';
  284. // 保留蛇当前的位置
  285. for (var i = 1; i < snake.length; i++) {
  286. snake[i - 1] = snake[i];
  287. }
  288. snake[snake.length - 1] = 'tb_' + game.x + '_' + game.y; // 蛇头位置
  289. for (var i = 0; i < snake.length - 1; i++) {
  290. $(snake[i]).style.backgroundColor = '#0066ff';
  291. }
  292. $(snake[i]).style.backgroundColor = 'blue';
  293. }
  294. // 食物 位置随机且不能与蛇位置重复
  295. function food() {
  296. game.fx = Math.floor(Math.random(game.sizeX) * game.sizeX);
  297. game.fy = Math.floor(Math.random(game.sizeY) * game.sizeY);
  298. if ($('tb_' + game.fx + '_' + game.fy).style.backgroundColor != '') {
  299. food();
  300. } else {
  301. $('tb_' + game.fx + '_' + game.fy).style.backgroundColor = '#ff3300';
  302. }
  303. }
  304. // 超级食物出现
  305. function superFood() {
  306. appearTime = setTimeout(function () {
  307. var n = Math.floor(Math.random(10) * 10);
  308. superFoodX = Math.floor(Math.random(game.sizeX) * game.sizeX);
  309. superFoodY = Math.floor(Math.random(game.sizeY) * game.sizeY);
  310. if (
  311. $('tb_' + superFoodX + '_' + superFoodY).style.backgroundColor != ''
  312. ) {
  313. superFood();
  314. } else {
  315. if (
  316. 'tb_' + superFoodX + '_' + superFoodY ==
  317. 'tb_' + game.fx + '_' + game.fy
  318. ) {
  319. superFood();
  320. return;
  321. }
  322. if (n < 3) {
  323. $('tb_' + superFoodX + '_' + superFoodY).style.backgroundColor =
  324. '#33cc33';
  325. color = 0;
  326. } else if (3 <= n && n < 5) {
  327. $('tb_' + superFoodX + '_' + superFoodY).style.backgroundColor =
  328. '#006600';
  329. color = 1;
  330. } else if (5 <= n && n < 7) {
  331. $('tb_' + superFoodX + '_' + superFoodY).style.backgroundColor =
  332. '#99ff66';
  333. color = 2;
  334. } else if (7 <= n && n < 9) {
  335. $('tb_' + superFoodX + '_' + superFoodY).style.backgroundColor =
  336. '#000000';
  337. color = 3;
  338. } else {
  339. $('tb_' + superFoodX + '_' + superFoodY).style.backgroundColor =
  340. '#ffff00';
  341. color = 4;
  342. }
  343. clearTimeout(disappearTime);
  344. superFood();
  345. // 一定时间后超级食物消失
  346. disappearTime = setTimeout(function () {
  347. $('tb_' + superFoodX + '_' + superFoodY).style.backgroundColor =
  348. '';
  349. superFoodX = superFoodY = null;
  350. }, game.rate * 120 - game.rate * 50);
  351. }
  352. }, game.rate * 120);
  353. }
  354. // 蛇吃食物
  355. function eat(ef) {
  356. // 吃普通食物
  357. if (ef == 1) {
  358. snake.push('tb_' + game.fx + '_' + game.fy);
  359. game.score += 10;
  360. food();
  361. }
  362. // 吃超级食物
  363. if (ef == 2) {
  364. if (color == 0) {
  365. game.score += 30;
  366. } else if (color == 1) {
  367. game.score -= 30;
  368. } else if (color == 2) {
  369. game.score += 50;
  370. } else if (color == 3) {
  371. game.score -= 50;
  372. } else {
  373. game.score += 100;
  374. }
  375. snake.push('tb_' + superFoodX + '_' + superFoodY);
  376. if (game.score < 0) {
  377. game.score = 0;
  378. }
  379. clearTimeout(disappearTime);
  380. }
  381. game.lengths += 1;
  382. $('scoreDiv').innerHTML =
  383. '得分:' +
  384. game.score +
  385. '<br/><br/>长度:' +
  386. game.lengths +
  387. '<br/><br/>最高分:' +
  388. game.record;
  389. }
  390. // 游戏结束
  391. function result() {
  392. var next = 'tb_' + game.x + '_' + game.y;
  393. if (
  394. game.x < 0 ||
  395. game.x >= game.sizeX ||
  396. game.y < 0 ||
  397. game.y >= game.sizeY
  398. ) {
  399. return true;
  400. }
  401. for (var i = 0; i < snake.length; i++) {
  402. if (next == snake[i]) {
  403. return true;
  404. }
  405. }
  406. for (var i = 0; i < barrers.length; i++) {
  407. if (next == barrers[i]) {
  408. return true;
  409. }
  410. }
  411. return false;
  412. }
  413. // 键盘控制
  414. function control(v) {
  415. var evt = v;
  416. game.dir = game.direction;
  417. if (37 <= evt.keyCode && evt.keyCode <= 40 && game.start) {
  418. if (evt.keyCode == 37) {
  419. game.dir == 'right'
  420. ? (game.direction = 'right')
  421. : (game.direction = 'left');
  422. } //
  423. if (evt.keyCode == 38) {
  424. game.dir == 'down'
  425. ? (game.direction = 'down')
  426. : (game.direction = 'up');
  427. } //
  428. if (evt.keyCode == 39) {
  429. game.dir == 'left'
  430. ? (game.direction = 'left')
  431. : (game.direction = 'right');
  432. } //
  433. if (evt.keyCode == 40) {
  434. game.dir == 'up'
  435. ? (game.direction = 'up')
  436. : (game.direction = 'down');
  437. } //
  438. if (game.dir != game.direction) {
  439. clearTimeout(game.time);
  440. snakeMove();
  441. }
  442. }
  443. }
  444. </script>
  445. </head>
  446. <body onload="init();" onkeyup="control(event);">
  447. <div class="backDiv">
  448. <div class="display">
  449. <div id="scoreDiv">分数:0</div>
  450. <br />
  451. <div id="prompt"></div>
  452. </div>
  453. <div id="gamePanel"></div>
  454. <br />
  455. <div id="operator">
  456. 选择关卡:<select id="pass" onchange="barrer();">
  457. <option value="1">关卡1</option>
  458. <option value="2">关卡2</option>
  459. <option value="3">关卡3</option>
  460. <option value="4">关卡4</option>
  461. <option value="5">关卡5</option> </select
  462. >&nbsp&nbsp 速度:<select id="rate">
  463. <option value="500"></option>
  464. <option value="450">较慢</option>
  465. <option value="300">中等</option>
  466. <option value="200">较快</option>
  467. <option value="100"></option> </select
  468. >&nbsp&nbsp
  469. <input
  470. type="button"
  471. id="start"
  472. value="开始"
  473. onclick="startGame();"
  474. />&nbsp&nbsp
  475. <input type="button" id="resets" value="重玩" onclick="init();" />
  476. </div>
  477. <div id="result"></div>
  478. </div>
  479. <div style="text-align: center;">
  480. <p><a href="http://www.mycodes.net/" target="_blank">源码之家</a></p>
  481. </div>
  482. </body>
  483. </html>

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

闽ICP备14008679号