怎么使用vue.js編寫藍(lán)色拼圖小游戲

這篇文章主要介紹了怎么使用vue.js編寫藍(lán)色拼圖小游戲,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

創(chuàng)新互聯(lián)自2013年創(chuàng)立以來,先為富順等服務(wù)建站,富順等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為富順企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。

Vue的優(yōu)點

Vue具體輕量級框架、簡單易學(xué)、雙向數(shù)據(jù)綁定、組件化、數(shù)據(jù)和結(jié)構(gòu)的分離、虛擬DOM、運(yùn)行速度快等優(yōu)勢,Vue中頁面使用的是局部刷新,不用每次跳轉(zhuǎn)頁面都要請求所有數(shù)據(jù)和dom,可以大大提升訪問速度和用戶體驗。

Later equals never!說干就干。首先理解游戲的規(guī)則:第一關(guān)為1*1的方塊,第二關(guān)為2*2以此類推

怎么使用vue.js編寫藍(lán)色拼圖小游戲

該圖為第三關(guān)3*3的方塊。點擊一個小方塊,該方塊和它相鄰的方塊的的顏色會從黃色變?yōu)樗{(lán)色,全部變?yōu)樗{(lán)色就過關(guān)了。

現(xiàn)在規(guī)則清楚了,開動吧!

/*style*/
.game_bg{
background: #333;
width: 600px;
height: 600px;
margin: 30px auto;
border-radius: 3px;
}
.card{
background: #E6AB5E;
float: left;
margin: 6px 0 0 6px;
}
.blueCard{
background: #5C90FF;
}
/*html*/
<div id="game">
<div class='game_bg'>
<div></div>
</div>
</div>
/*js*/
var vm=ew Vue({
el:'#game',
data:{
margin:6,//每張卡片間的距離
level:1,//游戲等級
cards:[],//卡片
size:0,//每張卡片的尺寸
},
methods:{},
});

卡片數(shù)為等級的平方,而每張卡片有黃色和藍(lán)色兩種顏色,并且隨著游戲難度的升級,方塊間的距離也在變小。所以在vue構(gòu)造函數(shù)中添加初始化游戲方法

initGame:function(){//初始化游戲函數(shù)
if(this.level<4){
this.margin=12;
}else if(this.level<8){
this.margin=6;
}else if(this.level<16){
this.margin=3;
}else{
this.margin=1;
}
this.cards=[];
this.size=(600-(this.level+1)*this.margin)/this.level;
for(var i=this.level*this.level;i--;){
this.cards.push({
color:false,//false是黃色,true是藍(lán)色
})
}
}

<div class='game_bg'></div>中的div進(jìn)行數(shù)據(jù)綁定

<div class='card'
: 
:class="{'blueCard':card.color}" v-for="(index,card) in cards"></div>
</div>

 接下來就是點擊一個方塊進(jìn)行翻牌的方法。它本身和相鄰的卡片的color屬性取反就行了。而我們注意到:位于該卡片左邊的是下標(biāo)減1;右邊的是下標(biāo)加1;上面的是下標(biāo)減等級;下面的下標(biāo)加等級。要注意的vm.cards下標(biāo)不存在的時候和在最左邊或最右邊時雖然下標(biāo)有可能存在但是相鄰的卡片是可能沒有的。所以加了一個改變相鄰區(qū)域的顏色的方法和在methods中加了一個翻牌子的方法

var changeNeighbor=function(index){
var cards=vm.cards;
if(index>0){//左邊
if(index%vm.level){//不在最左邊
cards[index-1].color=!cards[index-1].color;
}
}
if(index<cards.length-1){//右邊
if((index+1)%vm.level){//不在最右邊
cards[index+1].color=!cards[index+1].color;
}
}
if(index-vm.level>=0){//上面
cards[index-vm.level].color=!cards[index-vm.level].color;
}
if(index+vm.level<cards.length){//下面
cards[index+vm.level].color=!cards[index+vm.level].color;
}
}
/*********************************************************/
flop:function(index){//翻牌
this.cards[index].color=!this.cards[index].color;
changeNeighbor(index);
}

每次點擊后都要判斷本關(guān)游戲是否結(jié)束。遍歷vm.cards。發(fā)現(xiàn)如果存在color屬性為false的就是沒有過關(guān),反之則關(guān)過。

var gameOver=function(){
var cards=vm.cards;
for(var i=cards.length;i--;){
if(!cards[i].color) return false;
}
return true
};

這樣游戲基本的功能就實現(xiàn)了。然后再加上過關(guān)之后將等級提高1。并且將等級存到localStorage中。每次進(jìn)入頁面都去localStorage中查詢等級。過關(guān)之后給個提示。將點擊的步驟數(shù)顯現(xiàn)出來。加上重置本輪和重置等級的方法。在細(xì)節(jié)上進(jìn)行一些修改和增加最后的代碼就是這樣

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
.game_bg{
background: #333;
width: 600px;
height: 600px;
margin: 30px auto;
border-radius: 3px;
}
.card{
background: #E6AB5E;
float: left;
margin: 6px 0 0 6px;
}
.blueCard{
background: #5C90FF;
}
.btn_box{
text-align: center;
}
.info_box{
text-align: center;
}
.info_box span{
padding: 20px;
}
.rule_box{
width: 300px;
position: fixed;
top: 100px;
left: 50px;
color: #333;
}
h2{
margin: 0;
text-align: center;
font-size: 28px;
margin-bottom: 10px;
}
</style>
</body>
<h2>翻牌子游戲</h2>
<div id="game">
<div class="info_box">
<span v-text="'第'+level+'關(guān)'"></span>
<span v-text="'點擊'+stepCount+'次'"></span>
</div>
<div class='game_bg'>
<div class='card' @click="flop(index)"
: 
:class="{'blueCard':card.color}" v-for="(index,card) in cards"></div>
</div>
<div class="rule_box">
<h4>游戲規(guī)則</h4>
<h5>點擊相應(yīng)的方塊該方塊和它相鄰的方塊的的顏色會發(fā)生變化,全部變?yōu)樗{(lán)色就過關(guān)了</h5>
</div>
<div class="btn_box">
<button @click="resetLevel">重置等級</button>
<button @click="initGame">重新開始本輪</button>
</div>
</div>
<script src="vue/Vue.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
/**
* 該函數(shù)用來改變點擊的卡片相鄰卡片的顏色
* 位于該卡片左邊的是下標(biāo)減1;右邊的是下標(biāo)加1;上面的是下標(biāo)減等級;下面的下標(biāo)加等級
*/
var changeNeighbor=function(index){
var cards=vm.cards;
if(index>0){//左邊
if(index%vm.level){//不在最左邊
cards[index-1].color=!cards[index-1].color;
}
}
if(index<cards.length-1){//右邊
if((index+1)%vm.level){//不在最右邊
cards[index+1].color=!cards[index+1].color;
}
}
if(index-vm.level>=0){//上面
cards[index-vm.level].color=!cards[index-vm.level].color;
}
if(index+vm.level<cards.length){//下面
cards[index+vm.level].color=!cards[index+vm.level].color;
}
}
/**
*該函數(shù)用來判斷游戲是否結(jié)束 
*/
var gameOver=function(){
var cards=vm.cards;
for(var i=cards.length;i--;){
if(!cards[i].color) return false;
}
setLevel(vm.level+1);
vm.stepCount=0;
return true
};
/**
* 將等級儲存止本地
*/
var setLevel=function(level){
localStorage.cardLevel=level;
};
/**
* 得到本地的等級
*/
var getLevel=function(){
if(localStorage.cardLevel) return localStorage.cardLevel*1;
return 0;
};
/**
* 構(gòu)建vue構(gòu)造函數(shù)
*/
var vm=new Vue({
el:'#game',
data:{
margin:6,//每張卡片間的距離
level:1,//游戲等級
cards:[],//卡片
size:0,//每張卡片的尺寸
stepCount:0,//每輪點擊的次數(shù)
},
methods:{
initGame:function(){//初始化游戲函數(shù)
var level=getLevel();
if(level){
this.level=level;
}
if(this.level<4){
this.margin=12;
}else if(this.level<8){
this.margin=6;
}else if(this.level<16){
this.margin=3;
}else{
this.margin=1;
}
this.cards=[];
this.size=(600-(this.level+1)*this.margin)/this.level;
for(var i=this.level*this.level;i--;){
this.cards.push({
color:false,//false是黃色,true是藍(lán)色
})
}
},
flop:function(index){//翻牌
this.stepCount++;
this.cards[index].color=!this.cards[index].color;
changeNeighbor(index);
if(gameOver()){
setTimeout(function(){
alert('恭喜通過第'+vm.level+'關(guān)');
vm.level++;
vm.initGame();
},200)
}
},
resetLevel:function(){//重置等級
this.level=1;
localStorage.cardLevel=1;
vm.initGame();
},
},
});
vm.initGame();
</script>
</html>

別忘了加上vue2.0。就可以玩了。

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“怎么使用vue.js編寫藍(lán)色拼圖小游戲”這篇文章對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!

標(biāo)題名稱:怎么使用vue.js編寫藍(lán)色拼圖小游戲
分享鏈接:http://muchs.cn/article46/gechhg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、企業(yè)建站關(guān)鍵詞優(yōu)化、電子商務(wù)、網(wǎng)站改版、移動網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司