<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>贪吃蛇</title>
<link rel="stylesheet" href="css/index.css">
<style>
.wrap {
position: relative;
width: 500px;
height: 500px;
margin: 50px auto;
background-color: #000;
overflow: hidden;
}
.round {
display: block;
position: absolute;
width: 20px;
height: 20px;
border-radius: 50%;
background-color: green;
}
.round.head {
background-color: red;
}
</style>
</head>
<body>
<div class="wrap"></div>
<script src="js/utils.js"></script>
<script>
window.onload = function () {
init();
}
function init() {
initGame();
}
var initGame = (function () {
var wrap = document.getElementsByClassName('wrap')[0],
t = null;
var Snake = function () {
this.bodyArr = [
{ x: 0, y: 0 },
{ x: 0, y: 20 },
{ x: 0, y: 40 },
{ x: 0, y: 60 },
{ x: 0, y: 80 },
{ x: 0, y: 100 },
];
this.dir = 'DOWN';
}
Snake.prototype = {
init: function () {
this.bindEvent();
this.initSnake();
this.run();
},
bindEvent: function () {
var _self = this;
addEvent(document, 'keydown', function () {
_self.changeDir();
})
},
initSnake: function () {
var arr = this.bodyArr,
len = arr.length,
frag = document.createDocumentFragment(), // 文档碎片
item;
for (var i = 0; i < len; i++) {
item = arr[i];
var round = document.createElement('i');
round.className = i === len - 1 ? 'round head' : 'round';
round.style.left = item.x + 'px';
round.style.top = item.y + 'px';
frag.appendChild(round);
}
wrap.appendChild(frag);
},
run: function () {
var _self = this;
t = setInterval(function () {
_self.move();
}, 300)
},
move: function () {
var arr = this.bodyArr,
len = arr.length,
head = arr[len - 1];
for (var i = 0; i < len; i++) {
if (i === len - 1) {
switch (this.dir) {
case 'LEFT':
head.x -= 20;
break;
case 'RIGHT':
head.x += 20;
break;
case 'UP':
head.y -= 20;
break;
case 'DOWN':
head.y += 20;
break;
default:
break;
}
} else {
arr[i].x = arr[i + 1].x;
arr[i].y = arr[i + 1].y;
}
}
this.removeSnake();
this.initSnake();
},
removeSnake: function () {
var bodys = document.getElementsByClassName('round');
while (bodys.length > 0) {
bodys[0].remove();
}
},
changeDir: function (e) {
var e = e || window.event,
code = e.keyCode;
this.setDir(code);
},
setDir: function (code) {
switch (code) {
case 37: //左
if (this.dir !== 'RIGHT' && this.dir !== 'LEFT') {
this.dir = 'LEFT';
}
break;
case 39: //右
if (this.dir !== 'RIGHT' && this.dir !== 'LEFT') {
this.dir = 'RIGHT';
}
break;
case 38: //上
if (this.dir !== 'UP' && this.dir !== 'DOWN') {
this.dir = 'UP';
}
break;
case 40: //下
if (this.dir !== 'UP' && this.dir !== 'DOWN') {
this.dir = 'DOWN';
}
break;
default:
break;
}
}
}
return new Snake().init();
})
</script>
</body>
</html>
相关文章
- 03-0802. Tomcat源代码—04.分析请求过程
- 03-08LDA主题模型学习笔记5:C源代码理解
- 03-08修改TFS与本地源代码映射路径
- 03-08解决方案看起来是受源代码管理,但无法找到它的绑定信息。保存解决方案的源代码管理设置的MSSCCPRJ.SCC文件或其他项可能己被删除。
- 03-08正则表达式 查找网页源代码 提取指定内容
- 03-08201871010127-谢金鑫- 常用源代码管理工具与开发工具
- 03-08201871030131-谢林江 常用源代码管理工具与开发工具
- 03-08jetty源代码剖析
- 03-08我的数据访问函数库的源代码(二)—— SQL语句部分
- 03-08从源代码更新glibc