<style>
* {
margin: 0;
padding: 0;
}
/* 按钮样式 */
#begin {
width: 100px;
height: 40px;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
border-radius: 10px;
border: none;
box-shadow: 0 0 10px #b0b0b0;
}
/* 容器 */
#map {
position: relative;
width: 800px;
height: 400px;
background: rgb(238, 211, 211);
margin: 100px auto;
}
</style>
</head>
<body>
<div id="map">
<button id="begin">开始游戏</button>
</div>
<script>
var map = document.getElementById('map');
// 使用构造函数创建蛇,
function Snake() {
// 设置蛇的宽、高、默认走的方向
this.width = 10;
this.height = 10;
this.direction = 'right';
// 记住蛇的状态,初始为3个小点为一个蛇,
this.body = [{
x: 2,
y: 0
}, // 蛇头
{
x: 1,
y: 0
}, // 蛇身
{
x: 0,
y: 0
} // 蛇尾
];
// 显示蛇
this.display = function() {
// 建立蛇
for (var i = 0; i < this.body.length; i++) {
if (this.body[i].x != null) {
var s = document.createElement('div');
// 将节点保存到状态中
this.body[i].flag = s;
// 设置宽高
s.style.width = this.width + 'px';
s.style.height = this.height + 'px';
s.style.background = '#333';
s.style.border = '1px solid #b0b0b0'
// 设置位置
s.style.position = 'absolute';
s.style.left = this.body[i].x * this.width + 'px';
s.style.top = this.body[i].y * this.height + 'px';
// 添加
map.appendChild(s);
}
}
};
// 让蛇动起来
this.run = function() {
// 后一个元素到前一个元素的位置
for (var i = this.body.length - 1; i > 0; i--) {
this.body[i].x = this.body[i - 1].x;
this.body[i].y = this.body[i - 1].y;
}
// 根据方向处理蛇头
switch (this.direction) {
case "left":
this.body[0].x -= 1;
break;
case "right":
this.body[0].x += 1;
break;
case "up":
this.body[0].y -= 1;
break;
case "down":
this.body[0].y += 1;
break;
}
// 判断是否出界
if (this.body[0].x < 0 || this.body[0].x > 79 || this.body[0].y < 0 || this.body[0].y > 39) {
clearInterval(timer); // 清除定时器,
alert("游戏结束");
location.reload()
// 删除旧的
for (var i = 0; i < this.body.length; i++) {
if (this.body[i].flag != null) { // 如果刚吃完就死掉,会加一个值为null的
map.removeChild(this.body[i].flag);
}
}
this.body = [ // 回到初始状态,
{
x: 2,
y: 0
}, {
x: 1,
y: 0
}, {
x: 0,
y: 0
}
];
this.direction = 'right';
this.display(); // 显示初始状态
return false; // 结束
}
// 判断蛇头吃到食物
if (this.body[0].x == food.x && this.body[0].y == food.y) {
// 蛇加一节
this.body.push({
x: null,
y: null,
flag: null
});
// 清除食物,重新生成食物
map.removeChild(food.flag);
food.display();
}
// 吃到自己死亡
for (var i = 4; i < this.body.length; i++) {
if (this.body[0].x == this.body[i].x && this.body[0].y == this.body[i].y) {
clearInterval(timer); // 清除定时器,
alert("不要自己吃自己");
location.reload()
// 删除旧的
for (var i = 0; i < this.body.length; i++) {
if (this.body[i].flag != null) {
map.removeChild(this.body[i].flag);
}
}
// 回到初始状态,
this.body = [{
x: 2,
y: 0
}, {
x: 1,
y: 0
}, {
x: 0,
y: 0
}];
this.direction = 'right';
this.display(); // 显示回初始状态
return false;
}
}
// 先删掉初始的蛇,然后显示新蛇
for (var i = 0; i < this.body.length; i++) {
if (this.body[i].flag != null) {
map.removeChild(this.body[i].flag);
}
}
// 重新显示蛇
this.display();
}
}
// 构造食物
function Food() {
/* 样式 */
this.width = 10;
this.height = 10;
this.display = function() {
var f = document.createElement('div');
this.flag = f;
f.style.width = this.width + 'px';
f.style.height = this.height + 'px';
f.style.background = '#333';
f.style.position = 'absolute';
this.x = Math.floor(Math.random() * 80);
this.y = Math.floor(Math.random() * 40); //位置随机
f.style.left = this.x * this.width + 'px';
f.style.top = this.y * this.height + 'px';
map.appendChild(f);
}
}
var snake = new Snake();
var food = new Food();
snake.display(); // 初始化显示
food.display();
// 加键盘事件,上下左右
document.body.onkeydown = function(e) {
// 兼容低版本浏览器
var ev = e || window.event;
switch (ev.keyCode) {
case 38:
if (snake.direction != 'down') {
snake.direction = "up";
}
break;
case 40:
if (snake.direction != "up") {
snake.direction = "down";
}
break;
case 37:
if (snake.direction != "right") {
snake.direction = "left";
}
break;
case 39:
if (snake.direction != "left") {
snake.direction = "right";
}
break;
}
};
// 点击开始时,动起来
var begin = document.getElementById('begin');
var timer;
/* 按钮 */
begin.onclick = function() {
/* 点击之后按钮隐藏 */
begin.style.display = "none"
clearInterval(timer);
timer = setInterval("snake.run()", 100);
};
</script>
</body>