微信小程序|使用小程序制作一个2048小游戏
创始人
2024-04-11 02:32:53
0

文章目录

    • 一、文章前言
    • 二、创建小程序
    • 三、功能开发

一、文章前言

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

二、创建小程序

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

在这里插入图片描述

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

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

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

在这里插入图片描述

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

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

三、功能开发

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

在这里插入图片描述

在这里插入图片描述

	    
  1. 初始化矩阵数据,让其在页面加载时随机在两个方格上显示两个2。

在这里插入图片描述

在这里插入图片描述

  {grids}}" wx:for-index="rowIdx" wx:for-item="row" class="grid-row">{row}}" wx:for-index="colIdx" wx:for-item="cell" class="grid-cell">{cell.value}}">{cell}}" class="tile-inner">{{cell.value}}
  • 外部JS
function Grid(size) {this.size = size;this.cells = this.empty();
}Grid.prototype = {// 构造一个空的矩阵[[null,..,size.length],[]]empty: function() {var cells = [];for (var x = 0; x < this.size; x++) {var row = cells[x] = [];for (var y = 0; y < this.size; y++) {row.push(null);}}// [[{x:0,y:0},{x:0,y:1}],[]]return cells;},// 在空格子中随机挑选出一个格子randomAvailableCell: function() {var cells = this.availableCells();// 存在可填充的格子if (cells.length) {return cells[Math.floor(Math.random() * cells.length)];}},// 获取可填充的格子坐标availableCells: function() {var cells = [];for (var i = 0; i < this.size; i++) {for (var j = 0; j < this.size; j++) {// 当前格子无内容if (!this.cells[i][j]) {cells.push({x: i,y: j});}}}return cells;},// 是否存在空单元格cellsAvailable: function() {return !!this.availableCells().length;},cellAvailable: function(cell) {return !this.cellContent(cell);},insertTile: function(tile) {this.cells[tile.x][tile.y] = tile;},removeTile: function(tile) {this.cells[tile.x][tile.y] = null;},/* * 获取单元格内容* @param {object} cell {x:0,y:0} 单元格坐标*/cellContent: function(cell) {if (this.withinBounds(cell)) {return this.cells[cell.x][cell.y] || null;} else {return null;}},/** 空单元格,格子还未填充数字*/emptyCell: function(cell) {return !this.cellContent(cell);},withinBounds: function(cell) {return cell.x >= 0 && cell.x < this.size && cell.y >= 0 && cell.y < this.size;},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]);}}}
}module.exports = Grid;
  1. 给遍历出来的矩阵方块增加bindtouchstart,bindtouchmove,bindtouchend等手指触摸响应事件。

在这里插入图片描述

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

在这里插入图片描述

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

在这里插入图片描述

//-----------储存最高分-------------------------------------wx.getStorage({key: 'highscore44',success: function (res) {let highscore44 = res.datawx.setStorage({key: 'highscore44',data: highscore44,})},fail: () => {let highscore44 = []wx.setStorage({key: 'highscore44',data: highscore44,})}})
  1. 在页面增加重新开始按钮,并绑定对应的事件。

在这里插入图片描述

 重新开始
  // 重新开始restart: function () {this.updateView({grids: this.GameManager.restart(),over: false,won: false,score: 0});},
  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. 实现分享功能,邀请好友一起玩。
//---------------/*** 用户点击右上角分享*/onShareAppMessage: function () {return {title: '诚邀你一起来挑战2048排行!',path: '/pages/index/index',}}
}

相关内容

热门资讯

前端-session、jwt 目录:   (1)session (2&#x...
linux入门---制作进度条 了解缓冲区 我们首先来看看下面的操作: 我们首先创建了一个文件并在这个文件里面添加了...
关于测试,我发现了哪些新大陆 关于测试 平常也只是听说过一些关于测试的术语,但并没有使用过测试工具。偶然看到编程老师...
前缀和与对数器与二分法 1. 前缀和 假设有一个数组,我们想大量频繁的去访问L到R这个区间的和,...
nodejs:本地安装nvm实... 一、背景-使用不同版本node的原因 vue3+ts、nuxt3版本,node...
JAVA集合知识整理 Java集合知识整理 HashMap相关 HashMap的底层数据结构:jdk1.8之...
无刷直流电机介绍及单片机控制实... 无刷直流电机介绍及单片机控制实例前言基本概念优势与劣势使用寿命基本结构使用单片机控制实例电子调速器&...
fwdiary(2) dp2 1.传纸条  AcWing 275. 传纸条 - AcWing 走两条路,走一条最大的...
常用的DOS命令 常用的DOS命令 DOS(Disk Operating System,磁...
<C++> 类和对象(下) 1.const成员函数将const修饰的“成员函数”称之为const成员函数,cons...