javascript實(shí)現(xiàn)貪吃蛇小游戲
本文實(shí)例為大家分享了js實(shí)現(xiàn)貪吃蛇小游戲的具體代碼,供大家參考,具體內(nèi)容如下
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
</body>
<script>
// 貪吃蛇:
// 鍵盤的方向鍵,控制蛇的方向,碰撞食物,實(shí)現(xiàn)增加長度的效果,撞到墻壁或自身,游戲結(jié)束
// 分析:
// 地圖:提供邊界
// 食物:隨機(jī)出現(xiàn),可以被碰撞(坐標(biāo)重復(fù))
// 蛇:初始的固定長度,移動(dòng),改變方向,碰撞食物,碰撞墻,碰撞自己(坐標(biāo)重復(fù))
class Map{
constructor(){
// 提前設(shè)定將來的地圖的樣式數(shù)據(jù)
this.w = 800;
this.h = 400;
this.c = "#ccc";
// 執(zhí)行創(chuàng)建地圖方法
this.createEle();
}
createEle(){
this.mapEle = document.createElement("div");
this.mapEle.style.cssText = `width:${this.w}px;height:${this.h}px;background:${this.c};margin:0 auto;position:relative;border:solid 10px black;`;
document.body.appendChild(this.mapEle);
}
}
class Food{
constructor(){
// 提前設(shè)定將來的食物的樣式數(shù)據(jù)
this.w = 20;
this.h = 20;
this.c = "red";
this.x = 0;
this.y = 0;
// 執(zhí)行創(chuàng)建食物方法
this.createEle();
}
createEle(){
this.foodEle = document.createElement("div");
// 設(shè)置left和top時(shí)要注意,將地圖假設(shè)成了20個(gè)像素的一個(gè)格子,注意位置的換算
this.foodEle.style.cssText = `width:${this.w}px;height:${this.h}px;background:${this.c};position:absolute;left:${this.x * this.w}px;top:${this.y * this.h}px;`;
// console.log(m.mapEle);
m.mapEle.appendChild(this.foodEle);
}
randomPos(){
// 隨機(jī)位置,隨機(jī)產(chǎn)生的是格子的位置,不是真正的像素
this.x = random(0,(m.w-this.w) / this.w);
this.y = random(0,(m.h-this.h) / this.h);
// 設(shè)置位置時(shí),要換算成像素,然后再生效
this.foodEle.style.left = this.x * this.w + "px";
this.foodEle.style.top = this.y * this.h + "px";
}
}
// 至少得有多個(gè)元素(蛇節(jié))組成
// 每個(gè)元素都要有自己的標(biāo)簽,位置,寬高,顏色
// 單個(gè)元素,使用對象包含所有信息
// 所有元素怎么辦?來個(gè)數(shù)組,包裹起來
class Snake{
constructor(){
// 1.提前設(shè)定將來的蛇節(jié)的樣式數(shù)據(jù)
this.w = 20;
this.h = 20;
// 2.因?yàn)樯哂啥鄠€(gè)設(shè)計(jì)組成,每個(gè)蛇節(jié)都有自己的獨(dú)立信息,所以數(shù)據(jù)結(jié)構(gòu)設(shè)計(jì)成如下格式
this.body = [{
ele:null,
x:4,
y:3,
c:randomColor()
},{
ele:null,
x:3,
y:3,
c:randomColor()
},{
ele:null,
x:2,
y:3,
c:randomColor()
}];
// 7-1.提前設(shè)置默認(rèn)方向
this.d = "right";
// 3.開始創(chuàng)建蛇節(jié)元素,設(shè)置樣式
this.createEle();
}
createEle(){
// 4.使用循環(huán)多次創(chuàng)建,因?yàn)橛卸鄠€(gè)蛇節(jié)
for(var i=0;i<this.body.length;i++){
// 12.創(chuàng)建之前,需要判斷元素是否已經(jīng)存在,如果已經(jīng)存在,不需要?jiǎng)?chuàng)建
if(!this.body[i].ele){
this.body[i].ele = document.createElement("div");
m.mapEle.appendChild(this.body[i].ele);
}
this.body[i].ele.style.cssText = `width:${this.w}px;height:${this.h}px;background:${this.body[i].c};position:absolute;left:${this.body[i].x * this.w}px;top:${this.body[i].y * this.h}px;`;
}
// 找到蛇頭
this.body[0].ele.innerHTML = "0";
// 5.延遲之后,開始移動(dòng)
setTimeout(()=>{
this.move();
},300);
}
move(){
// 6.從最后一個(gè)元素向前找前一個(gè)元素的坐標(biāo),直到第一個(gè)
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;
}
// 7.第一個(gè)元素根據(jù)默認(rèn)方向,決定想哪走
switch(this.d){
case "left":
this.body[0].x -= 1;
break;
case "right":
this.body[0].x += 1;
break;
case "top":
this.body[0].y -= 1;
break;
case "bottom":
this.body[0].y += 1;
break;
}
// 8.移動(dòng)過程中,判斷是否撞到邊界,任意一個(gè)邊界都不行
if(this.body[0].x < 0 || this.body[0].y < 0 || this.body[0].x > ((m.w-this.w) / this.w) || this.body[0].y > ((m.h-this.h) / this.h)){
alert("撞墻了");
// 利用return的停止,結(jié)束程序
return;
}
// 9.移動(dòng)過程中,判斷是否與食物的坐標(biāo)重復(fù),如果重復(fù)
if(this.body[0].x === f.x && this.body[0].y === f.y){
// 給蛇增加一個(gè)蛇節(jié)
this.body.push({
ele:null,
x:this.body[this.body.length-1].x,
y:this.body[this.body.length-1].y,
c:randomColor()
})
// 刷新食物的坐標(biāo)
f.randomPos();
}
// 10.移動(dòng)過程中,判斷蛇頭的坐標(biāo)是否與某個(gè)任意一個(gè)蛇節(jié)的坐標(biāo)重復(fù)
for(var i=1;i<this.body.length;i++){
if(this.body[0].x == this.body[i].x && this.body[0].y == this.body[i].y){
// 如果重復(fù),撞到自己,結(jié)束程序
alert("撞到自己了");
return;
}
}
// 以上只是在修改坐標(biāo),生效了么?設(shè)置回去了么?
// 走的過程中有可能吃到食物,增加一個(gè)蛇節(jié)(元素),創(chuàng)建元素
// 11.所以,使用創(chuàng)建蛇節(jié)方法,重新設(shè)置蛇節(jié)的位置以及判斷是否需要?jiǎng)?chuàng)建新元素
this.createEle();
}
direct(type){
// 14.處理鍵盤穿件來的code值
// 處理之前要先判斷,當(dāng)前是否按下了相反方向
// 如果是相反方向,直接結(jié)束判斷,不執(zhí)行
// 如果不是相反方向,改變初始的默認(rèn)方向
switch(type){
case 37:
if(this.d === "right") break;
this.d = "left";
break;
case 38:
if(this.d === "bottom") break;
this.d = "top";
break;
case 39:
if(this.d === "left") break;
this.d = "right";
break;
case 40:
if(this.d === "top") break;
this.d = "bottom";
break;
}
}
}
function random(a,b){
return Math.round(Math.random()*(a-b)+b)
}
function randomColor(){
return `rgb(${random(0,255)},${random(0,255)},${random(0,255)})`
}
var m = new Map();
var f = new Food();
// 為了測試,先用計(jì)時(shí)器,重復(fù)執(zhí)行,看一看隨機(jī)效果
// setInterval(() => {
f.randomPos();
// }, 500);
var s = new Snake();
// 13.當(dāng)按下鍵盤時(shí),將按下的鍵盤的code值,傳給蛇的專屬處理方法
document.onkeydown = function(eve){
var e = eve || window.event;
var code = e.keyCode || e.which;
s.direct(code);
}
// 因?yàn)楹笃谝霾辉试S掉頭的效果
// 所以,采取當(dāng)前方法:兩個(gè)分支處理方向
</script>
</html>

更多有趣的經(jīng)典小游戲?qū)崿F(xiàn)專題,分享給大家:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Bootstrap布局組件應(yīng)用實(shí)例講解
這篇文章主要針對Bootstrap布局組件應(yīng)用進(jìn)行實(shí)例講解,感興趣的小伙伴們可以參考一下2016-02-02
js 去掉空格實(shí)例 Trim() LTrim() RTrim()
js 去掉空格實(shí)例Trim(),LTrim(),RTrim() 需要的朋友可以過來參考下,希望對大家有所幫助2014-01-01
利用js實(shí)現(xiàn)Vue2.0中數(shù)據(jù)的雙向綁定功能
vue數(shù)據(jù)雙向綁定是通過數(shù)據(jù)劫持結(jié)合發(fā)布者-訂閱者模式的方式來實(shí)現(xiàn)的,下面這篇文章主要給大家介紹了關(guān)于如何利用js實(shí)現(xiàn)Vue2.0中數(shù)據(jù)的雙向綁定功能的相關(guān)資料,需要的朋友可以參考下2021-07-07
一文搞懂JSON(JavaScript Object Notation)
Json 有兩種基本的結(jié)構(gòu),即 Json對象 和 Json 數(shù)組。通過 Json 對象和 Json 數(shù)組這兩種結(jié)構(gòu)的組合可以表示各種復(fù)雜的結(jié)構(gòu),今天通過本文給大家介紹JavaScript Object Notation的基本知識(shí),感興趣的朋友一起看看吧2021-10-10
bootstrap table 數(shù)據(jù)表格行內(nèi)修改的實(shí)現(xiàn)代碼
這篇文章主要介紹了bootstrap table 數(shù)據(jù)表格行內(nèi)修改的實(shí)現(xiàn)代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-02-02
fireworks菜單生成器mm_menu.js在 IE 7.0 顯示問題的解決方法
由于公司官網(wǎng)采用的是dreamwaver / fireworks 內(nèi)建的彈出式菜單的JS,在IE7下發(fā)現(xiàn)菜單項(xiàng)文字顯示都變成一排,無法正確瀏覽.2009-10-10

