JavaScript實現(xiàn)簡單貪吃蛇效果
更新時間:2020年03月09日 15:05:16 作者:huangfuyk.
這篇文章主要為大家詳細(xì)介紹了JavaScript實現(xiàn)簡單貪吃蛇效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了js實現(xiàn)簡單貪吃蛇效果的具體代碼,供大家參考,具體內(nèi)容如下
上代碼之前,先給大家看一下效果:

是不是想說:我能這樣玩一天…
話不多說,代碼如下:
<script>
class Map{
constructor(){
// 提前設(shè)定將來的地圖的樣式數(shù)據(jù)
this.w = 450;
this.h = 250;
this.c = "#DDD";
// 執(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:100px auto;position:relative;border:solid 10px #AAA;`;
document.body.appendChild(this.mapEle);
}
}
class Food{
constructor(){
// 提前設(shè)定將來的食物的樣式數(shù)據(jù)
this.w = 10;
this.h = 10;
this.c = "red";
this.x = 0;
this.y = 0;
// 執(zhí)行創(chuàng)建食物方法
this.createEle();
}
createEle(){
this.foodEle = document.createElement("div");
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;border-radius:10px`;
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è)置位置時,要換算成像素,然后再生效
this.foodEle.style.left = this.x * this.w + "px";
this.foodEle.style.top = this.y * this.h + "px";
}
}
class Snake{
constructor(){
// 提前設(shè)定將來的蛇節(jié)的樣式數(shù)據(jù)
this.w = 10;
this.h = 10;
// 因為蛇由多個設(shè)計組成,每個蛇節(jié)都有自己的獨(dú)立信息,所以數(shù)據(jù)結(jié)構(gòu)設(shè)計成如下格式
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()
}];
// 提前設(shè)置默認(rèn)方向
this.d = "right";
// 開始創(chuàng)建蛇節(jié)元素,設(shè)置樣式
this.createEle();
}
createEle(){
// 使用循環(huán)多次創(chuàng)建,因為有多個蛇節(jié)
for(var i=0;i<this.body.length;i++){
// 創(chuàng)建之前,需要判斷元素是否已經(jīng)存在,如果已經(jī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;border-radius: 10px`;
}
// 延遲之后,開始移動
setTimeout(()=>{
this.move();
},200);
}
move(){
// 從最后一個元素向前找前一個元素的坐標(biāo),直到第一個
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;
}
// 第一個元素根據(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;
}
// 移動過程中,判斷是否撞到邊界,任意一個邊界都不行
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;
}
// 移動過程中,判斷是否與食物的坐標(biāo)重復(fù),如果重復(fù)
if(this.body[0].x === f.x && this.body[0].y === f.y){
// 給蛇增加一個蛇節(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();
}
// 移動過程中,判斷蛇頭的坐標(biāo)是否與某個任意一個蛇節(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;
}
}
this.createEle();
}
direct(type){
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();
f.randomPos();
var s = new Snake();
// 當(dāng)按下鍵盤時,將按下的鍵盤的code值,傳給蛇的專屬處理方法
document.onkeydown = function(eve){
var e = eve || window.event;
var code = e.keyCode || e.which;
s.direct(code);
}
</script>
小編還為大家準(zhǔn)備了精彩的專題:javascript經(jīng)典小游戲匯總
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Bootstrap柵格系統(tǒng)使用方法及頁面調(diào)整變形的解決方法
這篇文章主要介紹了Bootstrap柵格系統(tǒng)使用方法及頁面調(diào)整變形的解決方法,需要的朋友可以參考下2017-03-03
JS實現(xiàn)同一DOM元素上onClick事件與onDblClick事件并存的解決方法
這篇文章主要介紹了JS實現(xiàn)同一DOM元素上onClick事件與onDblClick事件并存的解決方法,結(jié)合實例形式分析了javascript通過針對單擊onclick事件增加定時器進(jìn)行onClick事件與onDblClick事件的區(qū)別判定操作,需要的朋友可以參考下2018-06-06
用JavaScript實現(xiàn)類似于ListBox功能示例代碼
這篇文章主要介紹了用JavaScript實現(xiàn)類似于ListBox功能,需要的朋友可以參考下2014-03-03
javascript面向?qū)ο蟪绦蛟O(shè)計高級特性經(jīng)典教程(值得收藏)
這篇文章主要介紹了javascript面向?qū)ο蟪绦蛟O(shè)計高級特性,結(jié)合實例形式詳細(xì)講述了javascript對象的創(chuàng)建,訪問,刪除,對象類型,擴(kuò)展等,需要的朋友可以參考下2016-05-05

