Home  >  Article  >  Web Front-end  >  Use vue.js to write fun puzzle game example code

Use vue.js to write fun puzzle game example code

高洛峰
高洛峰Original
2017-03-25 11:18:312524browse

I saw the small game "Blue Puzzle" on the Internet before. The author wrote it in jquery. Through this article, I will share with you how to write a blue puzzle game based on vue.js. Let’s take a look at the implementation code

Later equals never! Just do it. First understand the rules of the game: the first level is 1*1 blocks, the second level is 2*2 and so on

Use vue.js to write fun puzzle game example code

The picture is the third level 3*3 of squares. Click on a small square, and the color of the square and its adjacent squares will change from yellow to blue. If all of them turn blue, you will pass the level.

Now that the rules are clear, let’s get started!

/*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*/
<p id="game">
<p class=&#39;game_bg&#39;>
<p></p>
</p>
</p>
/*js*/
var vm=ew Vue({
el:&#39;#game&#39;,
data:{
margin:6,//每张卡片间的距离
level:1,//游戏等级
cards:[],//卡片
size:0,//每张卡片的尺寸
},
methods:{},
});

The number of cards is the square of the level, and each card has two colors, yellow and blue, and as the difficulty of the game increases, the distance between the blocks also becomes smaller. So add the initialization game method

initGame:function(){//初始化游戏函数
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是蓝色
})
}
}

in the vue

constructor

to <p class="game_bg"></p> pData binding

<p class=&#39;card&#39;
:style="{&#39;width&#39;:size+&#39;px&#39;,&#39;height&#39;:size+&#39;px&#39;,&#39;marginTop&#39;:margin+&#39;px&#39;,&#39;marginLeft&#39;:margin+&#39;px&#39;}" 
:class="{&#39;blueCard&#39;:card.color}" v-for="(index,card) in cards"></p>
</p>

The next step is to click on a square to flip the cards. Just invert the color attribute of itself and the adjacent card. And we noticed: the one on the left of the card is the subscript minus 1; the one on the right is the subscript plus 1; the one above is the subscript minus the level; the one below is the subscript plus the level. It should be noted that when the vm.cards subscript does not exist and when it is on the far left or right, although the subscript may exist, the adjacent card may not. So I added a method to change the color of adjacent areas and a method to flip cards in 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);
}

Every time you click, you have to judge whether the game is over. Traverse vm.cards. It is found that if there is a color attribute with false, it will not pass, otherwise it will pass.

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

In this way, the basic functions of the game are realized. Then, after passing the level, the level will be increased by 1. And save the level to localStorage. Every time you enter the page, go to localStorage to query the level. Give me a hint after passing the level. Displays the number of clicked steps. Plus methods to reset this round and reset level. Make some modifications in the details and add the final code like this

<!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;
}
h1{
margin: 0;
text-align: center;
font-size: 28px;
margin-bottom: 10px;
}
</style>
</body>
<h1>翻牌子游戏</h1>
<p id="game">
<p class="info_box">
<span v-text="&#39;第&#39;+level+&#39;关&#39;"></span>
<span v-text="&#39;点击&#39;+stepCount+&#39;次&#39;"></span>
</p>
<p class=&#39;game_bg&#39;>
<p class=&#39;card&#39; @click="flop(index)"
:style="{&#39;width&#39;:size+&#39;px&#39;,&#39;height&#39;:size+&#39;px&#39;,&#39;marginTop&#39;:margin+&#39;px&#39;,&#39;marginLeft&#39;:margin+&#39;px&#39;}" 
:class="{&#39;blueCard&#39;:card.color}" v-for="(index,card) in cards"></p>
</p>
<p class="rule_box">
<h3>游戏规则</h3>
<h4>点击相应的方块该方块和它相邻的方块的的颜色会发生变化,全部变为蓝色就过关了</h4>
</p>
<p class="btn_box">
<button @click="resetLevel">重置等级</button>
<button @click="initGame">重新开始本轮</button>
</p>
</p>
<script src="vue/Vue.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
/**
* 该函数用来改变点击的卡片相邻卡片的颜色
* 位于该卡片左边的是下标减1;右边的是下标加1;上面的是下标减等级;下面的下标加等级
*/
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;
}
}
/**
*该函数用来判断游戏是否结束 
*/
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;
};
/**
* 构建vue构造函数
*/
var vm=new Vue({
el:&#39;#game&#39;,
data:{
margin:6,//每张卡片间的距离
level:1,//游戏等级
cards:[],//卡片
size:0,//每张卡片的尺寸
stepCount:0,//每轮点击的次数
},
methods:{
initGame:function(){//初始化游戏函数
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是蓝色
})
}
},
flop:function(index){//翻牌
this.stepCount++;
this.cards[index].color=!this.cards[index].color;
changeNeighbor(index);
if(gameOver()){
setTimeout(function(){
alert(&#39;恭喜通过第&#39;+vm.level+&#39;关&#39;);
vm.level++;
vm.initGame();
},200)
}
},
resetLevel:function(){//重置等级
this.level=1;
localStorage.cardLevel=1;
vm.initGame();
},
},
});
vm.initGame();
</script>
</html>

Don’t forget to add vue2.0. It’s ready to play.

Related articles:

Take you through Vue.js components in minutes

Detailed explanation of Vue.js development with pictures and texts How to quickly build the environment

Using require.js+vue to develop the WeChat upload image component method

The above is the detailed content of Use vue.js to write fun puzzle game example code. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn