Use vue.js to write fun puzzle game example code
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
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='game_bg'> <p></p> </p> </p> /*js*/ var vm=ew Vue({ el:'#game', 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='card' :style="{'width':size+'px','height':size+'px','marginTop':margin+'px','marginLeft':margin+'px'}" :class="{'blueCard':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 id="翻牌子游戏">翻牌子游戏</h1> <p id="game"> <p class="info_box"> <span v-text="'第'+level+'关'"></span> <span v-text="'点击'+stepCount+'次'"></span> </p> <p class='game_bg'> <p class='card' @click="flop(index)" :style="{'width':size+'px','height':size+'px','marginTop':margin+'px','marginLeft':margin+'px'}" :class="{'blueCard':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:'#game', 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('恭喜通过第'+vm.level+'关'); 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
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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.

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.

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.

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.

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

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

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.

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.
