Table of Contents
翻牌子游戏
游戏规则
点击相应的方块该方块和它相邻的方块的的颜色会发生变化,全部变为蓝色就过关了
Home Web Front-end JS Tutorial Use vue.js to write fun puzzle game example code

Use vue.js to write fun puzzle game example code

Mar 25, 2017 am 11:18 AM
js vue Games

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:{},
});
Copy after login

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是蓝色
})
}
}
Copy after login

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>
Copy after login

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);
}
Copy after login

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
};
Copy after login

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 id="翻牌子游戏">翻牌子游戏</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 id="游戏规则">游戏规则</h3>
<h4 id="点击相应的方块该方块和它相邻的方块的的颜色会发生变化-全部变为蓝色就过关了">点击相应的方块该方块和它相邻的方块的的颜色会发生变化,全部变为蓝色就过关了</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>
Copy after login

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

How to add functions to buttons for vue How to add functions to buttons for vue Apr 08, 2025 am 08:51 AM

You can add a function to the Vue button by binding the button in the HTML template to a method. Define the method and write function logic in the Vue instance.

How to use watch in vue How to use watch in vue Apr 07, 2025 pm 11:36 PM

The watch option in Vue.js allows developers to listen for changes in specific data. When the data changes, watch triggers a callback function to perform update views or other tasks. Its configuration options include immediate, which specifies whether to execute a callback immediately, and deep, which specifies whether to recursively listen to changes to objects or arrays.

What does vue multi-page development mean? What does vue multi-page development mean? Apr 07, 2025 pm 11:57 PM

Vue multi-page development is a way to build applications using the Vue.js framework, where the application is divided into separate pages: Code Maintenance: Splitting the application into multiple pages can make the code easier to manage and maintain. Modularity: Each page can be used as a separate module for easy reuse and replacement. Simple routing: Navigation between pages can be managed through simple routing configuration. SEO Optimization: Each page has its own URL, which helps SEO.

How to reference js file with vue.js How to reference js file with vue.js Apr 07, 2025 pm 11:27 PM

There are three ways to refer to JS files in Vue.js: directly specify the path using the &lt;script&gt; tag;; dynamic import using the mounted() lifecycle hook; and importing through the Vuex state management library.

How to return to previous page by vue How to return to previous page by vue Apr 07, 2025 pm 11:30 PM

Vue.js has four methods to return to the previous page: $router.go(-1)$router.back() uses &lt;router-link to=&quot;/&quot; component window.history.back(), and the method selection depends on the scene.

How to use vue traversal How to use vue traversal Apr 07, 2025 pm 11:48 PM

There are three common methods for Vue.js to traverse arrays and objects: the v-for directive is used to traverse each element and render templates; the v-bind directive can be used with v-for to dynamically set attribute values ​​for each element; and the .map method can convert array elements into new arrays.

How to jump to the div of vue How to jump to the div of vue Apr 08, 2025 am 09:18 AM

There are two ways to jump div elements in Vue: use Vue Router and add router-link component. Add the @click event listener and call this.$router.push() method to jump.

See all articles