目錄
前言
貪吃蛇小游戲展示
HTML結構
CSS樣式
JavaScript實作
案例圖片
總結
前言
不知道大家懷念以前當自己還是一個小孩子的時候拿著父母的按鍵手機也就是現在我們稱的老人機的那種手機玩游戲的日子嗎?小編在到現在還依稀記得那時候自己把父母的手機按鍵都按壞了,哈哈哈!!!!

現在回想起來都還記憶猶新,不知道你們在學習的時候會不會“摸魚”?小編有時候還是會摸一下的,摸魚都是拿出手機打開來看看,但是現在小編給你們帶來了這個游戲,你們“摸魚”的時候就不會只知道看手機了,而且小編還可以給你們回憶一下我們兒時的快樂,那就是小時候玩的非常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';
}
案例圖片
大家在復制圖片的時候一定要記得更改圖片的地址哦!我把這個案例的圖片給大家放在這里了!有需要的看看吧!



總結
看到這里了是不是認為這個很好玩,是否能讓你在“摸魚”的時候增加一點樂趣呢?如果有請點個贊再走吧!沒有的都看到這了也為你看到這里的辛苦點個贊吧!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/403987.html
標籤:其他
