原生js編寫貪吃蛇小游戲
更新時(shí)間:2021年05月12日 09:07:55 作者:你想變強(qiáng)嘛
這篇文章主要為大家詳細(xì)介紹了原生js編寫貪吃蛇小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了js編寫貪吃蛇小游戲的具體代碼,供大家參考,具體內(nèi)容如下
剛學(xué)完js模仿著教程,把自己寫的js原生小程序。
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 id="snakeWrap"></div>
</div>
<!-- 引入外部js文件 -->
<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;
left: 0;
top: 0;
background-color: rgba(0, 0, 0, 0.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: url(../images/Snipaste_2021-05-08_08-52-45.png) no-repeat;
background-size: contain;
margin-left: -100px;
margin-top: 222px;
}
#snakeWrap{
width: 600px;
height: 600px;
background: #73aad4;
border: 20px solid #13649c;
position: relative;
}
.snakeHead{
background-color: yellowgreen;
border-radius: 50%;
}
.snakeBody{
background-color: black;
border-radius: 50%;
}
.food{
background-color: red;
border-radius: 50%;
}
js部分
var sw = 20, //一個(gè)方塊的寬
sh = 20, //一個(gè)方塊的寬
tr = 30, //行數(shù)
td = 30; //列數(shù)
var snake = null, //生成蛇的實(shí)例
food = null; //生成食物的實(shí)例
game = null; //創(chuàng)建游戲?qū)嵗?
//把整體看成是一個(gè)一個(gè)小方塊 移動(dòng)的的時(shí)候創(chuàng)建和刪除方塊(后續(xù)所有方塊的生成都會(huì)調(diào)用)
// 方塊構(gòu)造函數(shù)
function Square(x,y,classname){ //對(duì)應(yīng)css中三種蛇的樣式(蛇頭 蛇身 蛇尾)
this.x = x * sw;
this.y = y * sh;
this.class = classname;
this.viewContent = document.createElement('div');
this.viewContent.className = this.class; //將創(chuàng)建出來的div添加對(duì)應(yīng)css樣式
this.parent = document.getElementById('snakeWrap');
}
//在方塊構(gòu)造函數(shù)的 原型鏈 上創(chuàng)建create方法 確定新div的具體信息
//this指向Square
Square.prototype.create = function(){
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); //把新創(chuàng)建的div添加到頁(yè)面
}
//在方塊構(gòu)造函數(shù)的 原型鏈 上創(chuàng)建remove方法 用于移動(dòng)時(shí)刪除方塊
Square.prototype.remove = function(){
this.parent.removeChild(this.viewContent);
}
// 蛇
function Snake(){
this.head = null; //存儲(chǔ)蛇頭信息
this.tail = null; //存儲(chǔ)蛇尾信息
this.pos = []; //存儲(chǔ)蛇身上的每一個(gè)方塊的位置
this.directionNum = { //存儲(chǔ)蛇走的方向
left : {
x : -1,
y : 0
},
right : {
x : 1,
y : 0
},
up : {
x : 0,
y : -1
},
down : {
x : 0,
y : 1
}
}
}
//this 指向 Snake
Snake.prototype.init = function(){ //初始化
// 創(chuàng)建蛇頭
var snakeHead = new Square(2,0,'snakeHead');
snakeHead.create();
this.head = snakeHead;
this.pos.push([2,0]); //儲(chǔ)存蛇頭信息
// 創(chuàng)建蛇身1
var snakeBody1 = new Square(1,0,'snakeBody');
snakeBody1.create();
this.pos.push([1,0]); //儲(chǔ)存蛇身信息
// 創(chuàng)建蛇尾
var snakeBody2 = new Square(0,0,'snakeBody');
snakeBody2.create();
this.tail = snakeBody2;
this.pos.push([0,0]); /儲(chǔ)存蛇尾信心
//形成鏈表關(guān)系
//蛇頭 蛇身 蛇尾的前后關(guān)系
snakeHead.last = null;
snakeHead.next = snakeBody1;
snakeBody1.last = snakeHead;
snakeBody1.next = snakeBody2;
snakeBody2.last = snakeBody1;
snakeBody2.next = null;
//給蛇 添加一個(gè)默認(rèn)方向 向右
this.direction = this.directionNum.right;
}
// 獲取蛇頭的下一個(gè)位置對(duì)應(yīng)的元素(this指向Snake)
// 獲取下一個(gè)點(diǎn)的坐標(biāo)并儲(chǔ)存到nextPos數(shù)組
Snake.prototype.getNextPos = function(){
var nextPos = [
this.head.x/sw + this.direction.x, //this.direction.x、y 下面會(huì)將方向與鍵盤事件綁定 來確定下一個(gè)點(diǎn)生成的位置
this.head.y/sh + this.direction.y
]
// 下個(gè)點(diǎn)是自己,撞到了自己 游戲結(jié)束
var selfCollied = false;
this.pos.forEach(function(value){ //forEach遍歷數(shù)組 兩數(shù)組比較看是否有重復(fù)坐標(biāo)
if (value[0] == nextPos[0] && value[1] == nextPos[1]){
selfCollied = true;
}
})
//撞到了自己 游戲結(jié)束
if(selfCollied){
this.、
.die.call(this);
return;
}
// 下個(gè)點(diǎn)是圍墻 游戲結(jié)束
if(nextPos[0] > 29 || nextPos[0] < 0 || nextPos[1] > 29 || nextPos[1] < 0){
this.strategies.die.call(this);
return;
}
// 下個(gè)點(diǎn)是食物 吃
if(food && food.pos[0] == nextPos[0] && food.pos[1] == nextPos[1]){
this.strategies.eat.call(this);
return;
}
// 下個(gè)點(diǎn)什么都不是 走
this.strategies.move.call(this);
}
// 碰撞后要做的事
Snake.prototype.strategies = {
move : function(format){ //參數(shù)用于判斷是否刪除蛇尾
// 創(chuàng)建一個(gè)newbody,刪掉蛇頭
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();
// 創(chuàng)建一個(gè)新蛇頭
var newx = this.head.x/sw + this.direction.x;
var newy = this.head.y/sh + this.direction.y;
var newHead = new Square(newx,newy,'snakeHead')
newHead.next = newBody;
newBody.last = newHead;
newHead.last = null;
newHead.create();
// 更新蛇身的坐標(biāo)
this.pos.splice(0,0,[newx,newy]);
this.head = newHead;
//如果為false 則吃
if(!format){
this.tail.remove();
this.tail = this.tail.last;
this.pos.pop();
}
},
eat : function(){
this.strategies.move.call(this,true);
game.score ++;
createFood();
},
die : function(){
game.over();
}
}
snake = new Snake();
// 創(chuàng)建食物
function createFood(){
// 食物小方塊坐標(biāo)
var x = null;
var y = null;
var include = true;
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();
}
}
// 創(chuàng)建游戲邏輯
function Game(){
this.timer = null;
this.score = 0;
}
Game.prototype.init = function(){
snake.init();
createFood();
//這里曾經(jīng)的e.keycode e.which 都已禁用 使用e.key
window.addEventListener('keydown',function(e){
if(e.key == 'ArrowLeft' && snake.direction != snake.directionNum.right){
snake.direction = snake.directionNum.left;
}else if(e.key == 'ArrowUp' && snake.direction != snake.directionNum.down){
snake.direction = snake.directionNum.up;
}else if(e.key == 'ArrowRight' && snake.direction != snake.directionNum.left){
snake.direction = snake.directionNum.right;
}else if(e.key == 'ArrowDown' && snake.direction != snake.directionNum.up){
snake.direction = snake.directionNum.down;
}
});
this.start();
}
Game.prototype.start = function(){
this.timer = setInterval(function(){
snake.getNextPos();
},0.0000000000000001)
}
Game.prototype.over = function(){
clearInterval(this.timer);
alert('你的得分為' + this.score);
// 游戲回到最初始狀態(tài)
var snakeWrap = document.getElementById('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();
}
簡(jiǎn)單的一個(gè)小游戲,如有問題請(qǐng)大佬指正。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
相關(guān)文章
JavaScript之移動(dòng)端H5生成圖片解決方案講解
這篇文章主要介紹了JavaScript之移動(dòng)端H5生成圖片解決方案講解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
html頁(yè)面顯示年月日時(shí)分秒和星期幾的兩種方式
在html頁(yè)面中顯示時(shí)間是很常見的,實(shí)現(xiàn)的方法有很多,下面為大家簡(jiǎn)單介紹兩種方式,有需求的朋友可以參考下,希望對(duì)大家有所幫助2013-08-08
Chrome調(diào)試折騰記之JS斷點(diǎn)調(diào)試技巧
這篇文章主要介紹了Chrome調(diào)試折騰記之JS斷點(diǎn)調(diào)試技巧,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09
基于javascript實(shí)現(xiàn)根據(jù)身份證號(hào)碼識(shí)別性別和年齡
這篇文章主要介紹了基于javascript實(shí)現(xiàn)根據(jù)身份證號(hào)碼識(shí)別性別和年齡的相關(guān)資料,需要的朋友可以參考下2016-01-01

