前言
不知道大家怀念以前当自己还是一个小孩子的时候拿着父母的按键手机也就是现在我们称的老人机的那种手机玩游戏的日子吗?小编在到现在还依稀记得那时候自己把父母的手机按键都按坏了。哈哈哈!!!!
现在回想起来都还记忆犹新。不知道你们在学习的时候会不会“摸鱼”?小编有时候还是会摸一下的。摸鱼都是拿出手机打开来看看,但是现在小编给你们带来了这个游戏,你们“摸鱼”的时候就不会只知道看手机了,而且小编还可以给你们回忆一下我们儿时的快乐,那就是小时候玩的非常happy的贪吃蛇小游戏。下面我们一起来看看如何用JavaScript写出一个代表我们童年的小游戏吧!
贪吃蛇小游戏展示
贪吃蛇小游戏
大家一起来看看这是不是你们童年时候玩的停不下来的小游戏。当然有兴趣的也可以自己动手来做一做,让自己“摸鱼”也能学到新东西。如果觉得这个颜色不适合你,那你大可自己来试试,改为你自己喜欢的样式,如何实现的我都给大家放下面了。有兴趣的去看看吧,没兴趣的你也可以试试,让你在“摸鱼”的时候给你增加一点乐趣。
HTML结构
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="css/index.css">
</head>
<body>
<div class="content">
<div class="btn startBtn"><button></button></div>
<div class="btn pauseBtn"><button></button></div>
<div id="snakeWrap">
<!-- <div class="snakeHead"></div>
<div class="snakeBody"></div>
<div class="food"></div> -->
</div>
</div>
<script src="js/index.js"></script>
</body>
</html>
CSS样式
.content {
width: 640px;
height: 640px;
margin: 100px auto;
position: relative;
}
.btn {
width: 100%;
height: 100%;
position: absolute;
top: 0;
left: 0;
background-color: rgba(255, 255, 255, .3);
z-index: 2;
}
.btn button {
background: none;
border: none;
background-size: 100% 100%;
cursor: pointer;
outline: none;
position: absolute;
left: 50%;
top: 50%;
}
.startBtn button {
width: 200px;
height: 80px;
background-image: url(../img/startBtn.gif);
margin-left: -100px;
margin-top: -40px;
}
.pauseBtn {
display: none;
}
.pauseBtn button {
width: 70px;
height: 70px;
background-image: url(../img/pauseBtn.png);
margin-left: -35px;
margin-top: -35px;
}
/* 蛇snakeWrap的样式 */
#snakeWrap {
width: 600px;
height: 600px;
background: rgba(252, 228, 236);
border: 20px solid rgba(248, 187, 208);
position: relative;
}
#snakeWrap div {
width: 20px;
height: 20px;
}
.snakeHead {
background-image: url(../img/蛇头.png);
background-size: cover;
}
.snakeBody {
background-color: #9ddbb1;
border-radius: 10px;
}
.food {
background-image: url(../img/草莓.png);
background-size: cover;
}
JavaScript实现
var sw = 20, //一个方块的宽
sh = 20, //一个方块的高
tr = 30, //行数
td = 30; //列数
var snake = null, //蛇的实例
food = null,
game = null; //食物的实例
//方块构造函数
function Square(x, y, classname) {
this.x = x * sw;
this.y = y * sh;
this.class = classname;
this.viewContent = document.createElement('div'); //方块对应的dom元素
this.viewContent.className = this.class;
this.parent = document.querySelector('#snakeWrap'); //方块的父级
};
Square.prototype.create = function() { //创建方块DOM 并添加到页面里
this.viewContent.style.position = 'absolute';
this.viewContent.style.width = sw + 'px';
this.viewContent.style.height = sh + 'px';
this.viewContent.style.left = this.x + 'px';
this.viewContent.style.top = this.y + 'px';
this.parent.appendChild(this.viewContent);
};
Square.prototype.remove = function() {
this.parent.removeChild(this.viewContent);
};
//创建蛇
function Snake() {
this.head = null; //存蛇头的信息
this.tail = null; //存蛇尾的信息
this.pos = []; //存蛇身的每一个方块的位置
this.directionNum = {
//存储蛇走的方向,用一个对象来表示
left: {
x: -1,
y: 0,
rotate: 180 //舌头在不同的方向应该进行对应度数的旋转,不应该始终向右
},
right: {
x: 1,
y: 0,
rotate: 0
},
up: {
x: 0,
y: -1,
rotate: -90
},
down: {
x: 0,
y: 1,
rotate: 90
}
};
}
Snake.prototype.init = function() {
//创建蛇头
var snakeHead = new Square(2, 0, 'snakeHead');
snakeHead.create();
this.head = snakeHead; //存储蛇头的信息
this.pos.push([2, 0]); //把舌头的位置存起来
//创建蛇身体1
var snakeBody1 = new Square(1, 0, 'snakeBody');
snakeBody1.create();
this.pos.push([1, 0]); //把蛇身1的坐标也存一下
//创建蛇身体2
var snakeBody2 = new Square(0, 0, 'snakeBody');
snakeBody2.create();
this.tail = snakeBody2; //把蛇尾的信息存储起来
this.pos.push([0, 0]); //把蛇身2的坐标也存一下
//形成链表关系
//蛇头关系
snakeHead.last = null;
snakeHead.next = snakeBody1;
//蛇身1
snakeBody1.last = snakeHead;
snakeBody1.next = snakeBody2;
//蛇身2
snakeBody2.last = snakeBody1;
snakeBody2.next = null;
//给蛇添加一条属性,用来表示蛇走的方向
this.direction = this.directionNum.right; //默认让蛇往右走
};
//这个个方法用来获取蛇头的下一个位置对应的元素,要根据元素做不同的动作
Snake.prototype.getNextPos = function() {
var nextPos = [ //蛇头要走的下一个点的坐标
this.head.x / sw + this.direction.x,
this.head.y / sh + this.direction.y
]
//下个点是自己,代表撞到了自己,游戏结束
var selfCollied = false; //是否撞到自己
this.pos.forEach(function(value) {
if (value[0] == nextPos[0] && value[1] == nextPos[1]) {
//如果数组中的两个数据都相等,就说明下一个点在蛇身上里面能找到,代表就撞到自己了
selfCollied = true;
}
});
if (selfCollied) {
console.log('撞到自己了');
this.strategies.die.call(this);
return;
}
//下一个点是围墙,代表撞到了墙,游戏结束
if (nextPos[0] < 0 || nextPos[1] < 0 || nextPos[0] > td - 1 || nextPos[1] > tr - 1) {
console.log('撞墙上了');
this.strategies.die.call(this);
return;
}
//下一个点是事物,代表吃。
//this.strategies.eat.call(this);
if (food && food.pos[0] == nextPos[0] && food.pos[1] == nextPos[1]) {
//如果这个条件成立说明现在蛇头要走的下一个是食物的那个点
console.log('撞到食物了');
this.strategies.eat.call(this);
return;
}
//下一个点是什么都没有,代表继续走
this.strategies.move.call(this);
};
//处理碰撞之后要做的事情
Snake.prototype.strategies = {
move: function(format) { //这个参数用于决定要不要删除最后一个方块(蛇尾) 当传了这个参数之后就是吃食物
//创建一个新的身体;在旧蛇头的位置
var newBody = new Square(this.head.x / sw, this.head.y / sh, 'snakeBody');
//更新链表的关系
newBody.next = this.head.next;
newBody.next.last = newBody;
newBody.last = null;
this.head.remove(); //把旧蛇头从原来的位置删除
newBody.create();
//创建一个新的蛇头(nextPos的位置,下一个要走到的点)
var newHead = new Square(this.head.x / sw + this.direction.x, this.head.y / sh + this.direction.y, 'snakeHead');
//更新链表关系
newHead.next = newBody;
newHead.last = null;
newBody.last = newHead;
newHead.viewContent.style.transform = 'rotate(' + this.direction.rotate + 'deg)';
newHead.create();
//蛇身上的每个方块的坐标需要更新
this.pos.splice(0, 0, [this.head.x / sw + this.direction.x, this.head.y / sh + this.direction.y]);
this.head = newHead; //还要把this.head的信心进行更新
//判断是删除蛇尾还是保留蛇尾
if (!format) { //如果format的值为false,表示删除(除吃以外的动作)
this.tail.remove();
this.tail = this.tail.last;
//删除蛇尾
this.pos.pop();
} else {
}
},
eat: function() {
this.strategies.move.call(this, true);
createFood();
//console.log('eat');
game.score++;
},
die: function() {
//console.log('die');
game.over();
}
}
snake = new Snake();
//创建食物
function createFood() {
//食物小方块的随机坐标
var x = null;
var y = null;
var include = true; //循环跳出的条件,true表示食物的坐标不在蛇身上(需要继续循环),false表示食物的坐标不在蛇身上(不循环了)
while (include) {
x = Math.round(Math.random() * (td - 1));
y = Math.round(Math.random() * (tr - 1));
snake.pos.forEach(function(value) {
if (x != value[0] && y != value[1]) {
//这个条件成立说明我再随机出来的这个坐标,在蛇身上并没有找到。
include = false;
}
});
}
//生成食物
food = new Square(x, y, 'food');
food.pos = [x, y]; //存储一下生成食物的坐标,用于跟蛇头要走的下一个点做对比
var foodDom = document.querySelector('.food');
if (foodDom) {
foodDom.style.left = x * sw + 'px';
foodDom.style.top = y * sh + 'px';
} else {
food.create();
}
}
//创建游戏逻辑
function Game() {
this.timer = null;
this.score = 0;
}
Game.prototype.init = function() {
snake.init();
//snake.getNextPos();
createFood();
//给上键盘事件
document.onkeydown = function(ev) {
if (ev.which == 37 && snake.direction != snake.directionNum.right) { //用户按下左键时候,这条蛇不能是正好往右走
snake.direction = snake.directionNum.left;
} else if (ev.which == 38 && snake.direction != snake.directionNum.down) { //用户按下上键时候,这条蛇不能是正好往下走
snake.direction = snake.directionNum.up;
} else if (ev.which == 39 && snake.direction != snake.directionNum.left) { //用户按下上键时候,这条蛇不能是正好往下走
snake.direction = snake.directionNum.right;
} else if (ev.which == 40 && snake.direction != snake.directionNum.up) { //用户按下上键时候,这条蛇不能是正好往下走
snake.direction = snake.directionNum.down;
}
}
this.start();
}
Game.prototype.start = function() { //开启一个定时器 开始游戏
this.timer = setInterval(function() {
snake.getNextPos();
}, 200);
}
//暂停游戏方法
Game.prototype.pause = function() {
clearInterval(this.timer);
}
Game.prototype.over = function() {
clearInterval(this.timer);
alert('你的游戏分数是' + this.score);
//游戏回到最初时候的状态
var snakeWrap = document.querySelector('#snakeWrap');
snakeWrap.innerHTML = '';
snake = new Snake();
game = new Game();
var startBtnWrap = document.querySelector('.startBtn');
startBtnWrap.style.display = 'block';
}
//开启游戏
game = new Game();
var startBtn = document.querySelector('.startBtn button');
startBtn.onclick = function() {
startBtn.parentNode.style.display = 'none';
game.init();
};
//暂停游戏
var snakeWrap = document.querySelector('#snakeWrap');
var pauseBtn = document.querySelector('.pauseBtn button');
snakeWrap.onclick = function() {
game.pause();
pauseBtn.parentNode.style.display = 'block';
}
pauseBtn.onclick = function() {
game.start();
pauseBtn.parentNode.style.display = 'none';
}
案例图片
大家在复制图片的时候一定要记得更改图片的地址哦!我把这个案例的图片给大家放在这里了!有需要的看看吧!
总结
看到这里了是不是认为这个很好玩,是否能让你在“摸鱼”的时候增加一点乐趣呢?如果有请点个赞再走吧!没有的都看到这了也为你看到这里的辛苦点个赞吧!