0


微信小程序|使用小程序制作一个2048小游戏

文章目录

一、文章前言

此文主要通过小程序实现2048游戏,游戏操作简单,容易上手。
规则:正常打开游戏的界面,会只有两个2,每次移动后都会出现一个2,数字大了之后会出现4和8。
只有数字相同,才能够相加,每次相加之后都会变成原本的二倍,比如两个2相加变成4,两个4相加变成8。
使大数在一边,小数在一边,这样相同的数才能更好的结合在一起。

二、创建小程序

  1. 访问微信公众平台,点击账号注册。

在这里插入图片描述

  1. 选择小程序,并在表单填写所需的各项信息进行注册。

在这里插入图片描述
在这里插入图片描述

  1. 在开发管理选择开发设置,将AppID及AppSecret复制出来进行存储。

在这里插入图片描述

  1. 下载安装微信web开发者工具并创建一个新的项目,填入上图所复制的AppId。

在这里插入图片描述
在这里插入图片描述

三、功能开发

  1. 在创建的index这个page中,实现一个4*4的矩阵。

在这里插入图片描述

在这里插入图片描述

  1. <view class="grid-container"><view class="grid-row"><view class="grid-cell"><view class="tile tile"></view></view></view></view>
  1. 初始化矩阵数据,让其在页面加载时随机在两个方格上显示两个2。

在这里插入图片描述

在这里插入图片描述

  1. <view class="grid-container"><view wx:for="{{grids}}" wx:for-index="rowIdx" wx:for-item="row" class="grid-row"><view wx:for="{{row}}" wx:for-index="colIdx" wx:for-item="cell" class="grid-cell"><view class="tile tile-{{cell.value}}"><view wx:if="{{cell}}" class="tile-inner">{{cell.value}}</view></view></view></view></view>
  • 外部JS
  1. function Grid(size){
  2. this.size = size;
  3. this.cells = this.empty();}
  4. Grid.prototype ={// 构造一个空的矩阵[[null,..,size.length],[]]
  5. empty:function(){
  6. var cells =[];for(var x =0; x < this.size; x++){
  7. var row = cells[x]=[];for(var y =0; y < this.size; y++){
  8. row.push(null);}}// [[{x:0,y:0},{x:0,y:1}],[]]return cells;},// 在空格子中随机挑选出一个格子
  9. randomAvailableCell:function(){
  10. var cells = this.availableCells();// 存在可填充的格子if(cells.length){return cells[Math.floor(Math.random()* cells.length)];}},// 获取可填充的格子坐标
  11. availableCells:function(){
  12. var cells =[];for(var i =0; i < this.size; i++){for(var j =0; j < this.size; j++){// 当前格子无内容if(!this.cells[i][j]){
  13. cells.push({
  14. x: i,
  15. y: j
  16. });}}}return cells;},// 是否存在空单元格
  17. cellsAvailable:function(){return!!this.availableCells().length;},
  18. cellAvailable:function(cell){return!this.cellContent(cell);},
  19. insertTile:function(tile){
  20. this.cells[tile.x][tile.y]= tile;},
  21. removeTile:function(tile){
  22. this.cells[tile.x][tile.y]= null;},/*
  23. * 获取单元格内容
  24. * @param {object} cell {x:0,y:0} 单元格坐标
  25. */
  26. cellContent:function(cell){if(this.withinBounds(cell)){return this.cells[cell.x][cell.y]|| null;}else{return null;}},/*
  27. * 空单元格,格子还未填充数字
  28. */
  29. emptyCell:function(cell){return!this.cellContent(cell);},
  30. withinBounds:function(cell){return cell.x >=0&& cell.x < this.size && cell.y >=0&& cell.y < this.size;},
  31. eachCell:function(callback){for(var x =0; x < this.size; x++){for(var y =0; y < this.size; y++){callback(x, y, this.cells[x][y]);}}}}
  32. module.exports = Grid;
  1. 给遍历出来的矩阵方块增加bindtouchstart,bindtouchmove,bindtouchend等手指触摸响应事件。

在这里插入图片描述

  1. touchStart:function(events){// 多指操作
  2. this.isMultiple = events.touches.length >1;if(this.isMultiple){return;}
  3. var touch = events.touches[0];
  4. this.touchStartClientX = touch.clientX;
  5. this.touchStartClientY = touch.clientY;},
  6. touchMove:function(events){
  7. var touch = events.touches[0];
  8. this.touchEndClientX = touch.clientX;
  9. this.touchEndClientY = touch.clientY;},
  1. 在事件响应的同时记录分数。

在这里插入图片描述

  1. var highscore44 = wx.getStorageSync('highscore44')||0;if(data.score > highscore44){
  2. wx.setStorageSync('highscore44', data.score);}
  1. <view class="heading"><view class="scores-container"><view class="score-container">{{score}}</view><view class="best-container">{{highscore44}}</view></view></view>
  1. 在游戏结束的时候将分数存入小程序缓存,并获取之前的分数进行比对,判断是否最高分。

在这里插入图片描述

  1. //-----------储存最高分-------------------------------------
  2. wx.getStorage({
  3. key:'highscore44',
  4. success:function(res){
  5. let highscore44 = res.data
  6. wx.setStorage({
  7. key:'highscore44',
  8. data: highscore44,})},
  9. fail:()=>{
  10. let highscore44 =[]
  11. wx.setStorage({
  12. key:'highscore44',
  13. data: highscore44,})}})
  1. 在页面增加重新开始按钮,并绑定对应的事件。

在这里插入图片描述

  1. <view class="above-game"><text class="restart-button" bindtap="restart">重新开始</text></view>
  1. // 重新开始
  2. restart:function(){
  3. this.updateView({
  4. grids: this.GameManager.restart(),
  5. over: false,
  6. won: false,
  7. score:0});},
  1. 可以将分数通过云开发或者数据库的方式进行存储,将分数进行排行展示。

在这里插入图片描述

  1. rankInfo4x4:[{ name:'摔跤猫子', score:180000, img:'rank1.png'},{ name:'siri', score:163148, img:'rank2.png'},{ name:'kitten', score:146088, img:'rank3.png'},{ name:'admin', score:136024, img:'rank4.png'},{ name:'无语小咪', score:122908, img:'rank5.png'},{ name:'汤姆', score:115283, img:'rank6.png'}],
  1. 实现分享功能,邀请好友一起玩。
  1. //---------------/**
  2. * 用户点击右上角分享
  3. */
  4. onShareAppMessage:function(){return{
  5. title:'诚邀你一起来挑战2048排行!',
  6. path:'/pages/index/index',}}}

本文转载自: https://blog.csdn.net/weixin_42794881/article/details/127959800
版权归原作者 摔跤猫子 所有, 如有侵权,请联系我们删除。

“微信小程序|使用小程序制作一个2048小游戏”的评论:

还没有评论