当前位置: 首页 > news >正文

vue3+uniapp在微信小程序实现一个2048小游戏

一、效果展示

二、代码

<template><view class="page"><view class="top"><view class="score">得分:{{total}}</view><view class="time">用时:{{allTime}}s</view></view><view class="center"><view class="mainBox"><view class="row" v-for="(row, rowIndex) in gameBoard" :key="rowIndex"><view class="cell" v-for="(cell, cellIndex) in row" :key="cellIndex"><!-- 	<view :class="cell!==0?'cellBox':''"> --><view:class="cellIndex==newArr[0][1]&&rowIndex==newArr[0][0]||cellIndex==newArr[1][1]&&rowIndex==newArr[1][0]?'newBox':cell!==0?'cellBox':''"><view class="colorBox":style="{backgroundColor:cell==2?'#ff3a3a':cell==4?'#ff9b29':cell==8?'#ebff31':cell==16?'#34ff31':cell==32?'#369083':cell==64?'#2e3cff':cell==128?'#c12fff':cell==256?'#ff77ed':cell==512?'#ffe9fe':cell==1024?'#fffcd4':cell==2048?'#04010b':''}"><text v-show=" cell!==0&&cell!==1">{{ cell }}</text></view></view></view></view></view></view><view class="bottom"><view class="kaishi" v-show="gameStatus==false"><view class="flexBox"> <button @click="gameStart()"> 游戏开始</button></view></view><view class="jinxing" v-show="gameStatus==true"><view class="flexBox"><view class="gameOver"><view class="gameOverButton" @click="gameOver()">结束</view></view><view class="contorl"><view class="shang" @click="shang()"></view><view class="xia" @click="xia()"></view><view class="zuo" @click="zuo()"></view><view class="you" @click="you()"></view></view></view></view></view></view>
</template><script lang="ts" setup>import { ref } from 'vue'// 游戏状态const gameStatus = ref<boolean>(false);// 显示的数组let gameBoard = ref<number[][]>(Array.from({ length: 4 }, () => Array(4).fill(0)));// 新增的俩let newArr = ref<number[][]>(Array.from({ length: 2 }, () => Array(2).fill(null)))// 得分const total = ref<number>();// 用时const allTime = ref(0)const timer1 = ref()// 游戏开始const gameStart = () => {total.value = 0;allTime.value = 0gameStatus.value = true;gameBoard.value = numInit()timer1.value = setInterval(() => {allTime.value = allTime.value + 1;}, 1000)}// 游戏结束const gameOver = () => {gameStatus.value = false;clearInterval(timer1.value)timer1.value = null;newArr.value = Array.from({ length: 2 }, () => Array(2).fill(null));}// 获取随机数的函数const getRandomlet = (min, max) => {min = Math.ceil(min);max = Math.floor(max);return Math.floor(Math.random() * (max - min + 1)) + min;}// 随机初始化数值const numInit = () => {const array = Array.from({ length: 4 }, () => Array(4).fill(0));const positions = [];// 生成一个包含所有可能位置的数组  for (let i = 0; i < 4; i++) {for (let j = 0; j < 4; j++) {positions.push({ x: i, y: j });}}// 随机选择6个位置  const selectedPositions = [];for (let i = 0; i < 6; i++) {const randomIndex = getRandomlet(0, positions.length - 1);selectedPositions.push(positions[randomIndex]);positions.splice(randomIndex, 1); // 从数组中移除已选位置,避免重复选择  }// 设置前4个位置为2  for (let i = 0; i < 4; i++) {const position = selectedPositions[i];array[position.x][position.y] = 2;}// 对于剩下的2个位置,随机设置为4或8  for (let i = 4; i < 6; i++) {const position = selectedPositions[i];const randomValue = getRandomlet(1, 2) === 1 ? 4 : 8;array[position.x][position.y] = randomValue;}return array;}// 旋转数组const rotate90Clockwise = (matrix) => {const n = matrix.length;let rotatedMatrix = Array.from({ length: n }, () => []);// 顺时针旋转90度for (let i = 0; i < n; i++) {for (let j = 0; j < n; j++) {rotatedMatrix[j][n - i - 1] = matrix[i][j];}}return rotatedMatrix;}// 累计与填入const addNum = (arr) => {let copiedArray = JSON.parse(JSON.stringify(arr));let defen = 0;for (let i = 0; i < copiedArray.length; i++) {for (let j = 0; j < copiedArray[i].length; j++) {// 找到第一个不为0if (copiedArray[i][j] !== 0) {for (let p = 0; p < j; p++) {if (copiedArray[i][p] == copiedArray[i][j]) {copiedArray[i][p] = copiedArray[i][j] + copiedArray[i][p];defen = defen + copiedArray[i][p] / 2;copiedArray[i][j] = 0;}// 移动到第一个0if (copiedArray[i][p] == 0) {copiedArray[i][p] = copiedArray[i][j];copiedArray[i][j] = 0;}}}}}total.value = total.value + defenreturn copiedArray;}// 添加新数字const addRandomNumbersToZeros = (arr) => {let matrix = JSON.parse(JSON.stringify(arr));// 存储所有值为0的元素的坐标  let zeroIndices = [];// 遍历二维数组,找到值为0的元素的坐标  for (let i = 0; i < matrix.length; i++) {for (let j = 0; j < matrix[i].length; j++) {if (matrix[i][j] === 0) {zeroIndices.push([i, j]);}}}// 如果没有0,则无法添加数字  if (zeroIndices.length < 2) {gameOver()return;}// 从所有0的坐标中随机选择两个  let randomIndices = zeroIndices.sort(() => 0.5 - Math.random()).slice(0, 2);// 为这两个坐标对应的元素添加随机数字  let randomNumbers = [2, 4, 8];for (let index of randomIndices) {let [row, col] = index;let randomNumber = randomNumbers[Math.floor(Math.random() * randomNumbers.length)];matrix[row][col] = randomNumber;}newArr.value = randomIndices;return matrix;}// 移动const moveAndMerge = (dir) => {if (dir == 'shang') {gameBoard.value = addNum(gameBoard.value)}else if (dir == 'zuo') {let newArr = JSON.parse(JSON.stringify(gameBoard.value));newArr = rotate90Clockwise(addNum(rotate90Clockwise(rotate90Clockwise(rotate90Clockwise(newArr)))))gameBoard.value = newArr} else if (dir == 'you') {let newArr = JSON.parse(JSON.stringify(gameBoard.value));newArr = rotate90Clockwise(rotate90Clockwise(rotate90Clockwise(addNum(rotate90Clockwise(newArr)))))gameBoard.value = newArr} else if (dir == 'xia') {let newArr = JSON.parse(JSON.stringify(gameBoard.value));newArr = rotate90Clockwise(rotate90Clockwise(addNum(rotate90Clockwise(rotate90Clockwise(newArr)))))gameBoard.value = newArr}gameBoard.value = addRandomNumbersToZeros(gameBoard.value)}// 操作const shang = () => {moveAndMerge('shang')}const xia = () => {moveAndMerge('xia')}const zuo = () => {moveAndMerge('zuo')}const you = () => {moveAndMerge('you')}
</script><style lang="scss" scoped>.page {width: 100vw;overflow: hidden;height: 100vh;background-color: #c6ffe6;display: flex;flex-direction: column;font-family: cuteFont;.top {width: 80%;height: 20vw;display: flex;align-items: center;margin-left: 10%;font-size: 2rem;.score {flex: 1;}.time {flex: 1;}}.center {width: 100vw;height: 100vw;.mainBox {width: 80%;margin: 10% 10%;height: 80%;border-radius: 15px;display: flex;.row {flex: 1;display: flex;flex-direction: column;}.cell {flex: 1;border: 1px solid #ff80c2;background-color: #b5f2ff;display: flex;justify-content: center;align-items: center;color: #ffffff;font-size: 2rem;.newBox {width: 90%;height: 90%;background-color: #9d6fff;border-radius: 15px;display: flex;justify-content: center;align-items: center;animation: newBox 0.5s;}.cellBox {width: 90%;height: 90%;background-color: #9d6fff;border-radius: 15px;}.colorBox {width: 100%;border-radius: 15px;height: 100%;display: flex;justify-content: center;align-items: center;}}}}.bottom {flex: 1;position: relative;.kaishi {width: 100%;height: 100%;background-color: #86ff61;position: absolute;.flexBox {width: inherit;height: inherit;display: flex;justify-content: center;align-items: center;}}.jinxing {width: 100%;height: 100%;position: absolute;.flexBox {width: inherit;height: inherit;display: flex;flex-direction: row;.contorl {flex: 1;.shang {width: 40px;height: 40%;position: absolute;left: 50%;background-color: #ff0777;clip-path: polygon(0% 50%, 50% 0%, 100% 50%, 80% 50%, 80% 100%, 20% 100%, 20% 50%);}.shang:hover {border: 1px solid #3d37ff;}.xia {width: 40px;height: 40%;position: absolute;top: 50%;left: 50%;background-color: #ff0777;clip-path: polygon(20% 0%, 80% 0%, 80% 50%, 100% 50%, 50% 100%, 0% 50%, 20% 50%);}.xia:hover {border: 1px solid #3d37ff;}.zuo {width: 120px;height: 40px;position: absolute;top: calc(50% - 30px);left: calc(50% - 120px);background-color: #ff0777;clip-path: polygon(0% 50%, 50% 0%, 50% 20%, 100% 20%, 100% 80%, 50% 80%, 50% 100%);}.zuo:hover {border: 1px solid #3d37ff;}.you {width: 120px;height: 40px;position: absolute;top: calc(50% - 30px);left: calc(50% + 40px);background-color: #ff0777;clip-path: polygon(0% 20%, 50% 20%, 50% 0%, 100% 50%, 50% 100%, 50% 80%, 0% 80%);}.you:hover {border: 1px solid #3d37ff;}}.gameOver {.gameOverButton {width: 50px;height: 100%;font-size: 2rem;display: flex;justify-content: center;align-items: center;background-color: #fff;border-radius: 0 15px 0 0;border: 1px solid #a860ff;}}}}}}@keyframes newBox {0% {width: 0%;height: 0%;}100% {width: 90%;height: 90%;}}
</style>

三、体验地址

微信小程序搜索《静远的工具箱》:偶数求和那个功能

相关文章:

vue3+uniapp在微信小程序实现一个2048小游戏

一、效果展示 二、代码 <template><view class"page"><view class"top"><view class"score">得分:{{total}}</view><view class"time">用时:{{allTime}}s</view></view><view cl…...

常见的浏览器跨域解决方法

1. 前端方法&#xff1a;JSONP&#xff08;仅适用于GET请求&#xff09; JSONP&#xff08;JSON with Padding&#xff09;是一种利用<script>标签的src属性不受同源策略限制的特性来实现跨域数据请求的方法。JSONP通过在前端动态创建<script>标签&#xff0c;并将…...

飞桨模型转ONNX模型教程

文章目录 飞桨模型转ONNX模型教程1. ONNX简介2. Paddle2ONNX安装3. 获取Paddle2ONNX模型库4. 飞桨转ONNX教程4.1 飞桨训练模型导出为ONNX模型4.2 飞桨部署模型转为ONNX模型4.3 验证ONNX模型4.4 使用ONNX模型进行推理 5. 注意事项 飞桨模型转ONNX模型教程 1. ONNX简介 ONNX是一…...

vue使用swiper(轮播图)-真实项目使用

一、安装 我直接安装的vue-awesome-swiper": "^3.1.3"指定版本 npm install vue-awesome-swiper3.1.3 swiper --save二、vue页面使用&#xff0c;写了一个小demo <template><div class"vue-swiper"><h1>{{ msg }}</h1><…...

C++ 创建并初始化对象

创建并初始化C对象 当我们创建一个C对象时&#xff0c;它需要占用一些内存&#xff0c;即使我们写一个完全为空的类&#xff0c;类中没有成员&#xff0c;什么也没有&#xff0c;它至少也要占用一个字节的内存。但是我们类中有很多成员&#xff0c;它们需要存储在某地方&#…...

大数据可视化python01

import pandas as pd import matplotlib.pyplot as plt# 设置中文改写字体 plt.rcParams[font.sans-serif] [SimHei]# 读取数据 data pd.read_csv(C:/Users/wzf/Desktop/读取数据进行数据可视化练习/实训作业练习/瓜果类单位面积产量.csv ,encoding utf-8)#输出 print(data)…...

Java底层自学大纲_分布式篇

分布式专题_自学大纲所属类别学习主题建议课时&#xff08;h&#xff09;A 分布式锁001 Zookeeper实现分布式锁l-常规实现方式2.5A 分布式锁002 Zookeeper实现分布式锁II-续命&超时&羊群效应问题解决方案2.5A 分布式锁003 Zookeeper实现分布式锁III-基于Curator框架实现…...

Thread多线程(创建,方法,安全,通信,线程池,并发,并行,线程的生命周期)【全详解】

目录 1.多线程概述 2.多线程的创建 3.Thread的常用方法 4.线程安全 5.线程同步 6.线程通信 7.线程池 8.其它细节知识&#xff1a;并发、并行 9.其它细节知识&#xff1a;线程的生命周期 1.多线程概述 线程是什么&#xff1f; 线程(Thread)是一个程序内部的一条执行…...

自定义View中的ListView和ScrollView嵌套的问题

当我们在使用到ScrollView和ListView的时候可能会出现显示不全的问题。那我们可以进行以下分析 ScrollView在测量子布局的时候会用UNSPECIFIED。通过源码观察&#xff0c; 在ScrollView的onMeasure方法中 Overrideprotected void onMeasure(int widthMeasureSpec, int heightMe…...

支持向量机 SVM | 线性可分:硬间隔模型公式推导

目录 一. SVM的优越性二. SVM算法推导小节概念 在开始讲述SVM算法之前&#xff0c;我们先来看一段定义&#xff1a; 支持向量机(Support VecorMachine, SVM)本身是一个二元分类算法&#xff0c;支持线性分类和非线性分类的分类应用&#xff0c;同时通过OvR或者OvO的方式可以应用…...

【Unity实战】UGUI和Z轴排序那点事儿

如果读者是从Unity 4.x时代过来的&#xff0c;可能都用过NGUI这个插件&#xff08;后来也是土匪成了正规军&#xff09;&#xff0c;NGUI一大特点是可以靠transform位移的Z值进行遮挡排序&#xff0c;然而这个事情在UGUI成了难题&#xff08;Sorting Layer、Inspector顺序等因素…...

Vue/React 前端高频面试

说一说vue钩子函数 钩子函数是Vue实例创建和销毁过程中自动执行的函数。按照组件生命周期的过程分为&#xff1a;挂载阶段 -> 更新阶段 -> 销毁阶段。 每个阶段对应的钩子函数分别为&#xff1a;挂载阶段&#xff08;beforeCreate&#xff0c;created&#xff0c;befor…...

[技巧]Arcgis之图斑四至范围批量计算

ArcGIS图层&#xff08;点、线、面三类图形&#xff09;四至范围计算 例外一篇介绍&#xff1a;[技巧]Arcgis之图斑四至点批量计算 说明&#xff1a;如下图画出来的框&#xff08;范围标记不是很准&#xff09; &#xff0c;图斑的x最大和x最小&#xff0c;y最大&#xff0c;…...

C/C++工程师面试题(STL篇)

STL 中有哪些常见的容器 STL 中容器分为顺序容器、关联式容器、容器适配器三种类型&#xff0c;三种类型容器特性分别如下&#xff1a; 1. 顺序容器 容器并非排序的&#xff0c;元素的插入位置同元素的值无关&#xff0c;包含 vector、deque、list vector&#xff1a;动态数组…...

Effective Programming 学习笔记

1 基本语句 1.1 断言 在南溪看来&#xff0c;断言可以用来有效地确定编程中当前代码运行的前置条件&#xff0c;尤其是以下情况&#xff1a; 第三方工具库对输入数据的依赖&#xff0c;例如&#xff1a;minitouch库对Android版本的要求...

【MGR】MySQL Group Replication 背景

目录 17.1 Group Replication Background 17.1.1 Replication Technologies 17.1.1.1 Primary-Secondary Replication 17.1.1.2 Group Replication 17.1.2 Group Replication Use Cases 17.1.2.1 Examples of Use Case Scenarios 17.1.3 Group Replication Details 17.1…...

300分钟吃透分布式缓存-17讲:如何理解、选择并使用Redis的核心数据类型?

Redis 数据类型 首先&#xff0c;来看一下 Redis 的核心数据类型。Redis 有 8 种核心数据类型&#xff0c;分别是 &#xff1a; & string 字符串类型&#xff1b; & list 列表类型&#xff1b; & set 集合类型&#xff1b; & sorted set 有序集合类型&…...

思科网络设备监控

思科是 IT 行业的先驱之一&#xff0c;提供从交换机到刀片服务器的各种设备&#xff0c;以满足中小企业和企业的各种 IT 管理需求。管理充满思科的 IT 车间涉及许多管理挑战&#xff0c;例如监控可用性和性能、管理配置更改、存档防火墙日志、排除带宽问题等等&#xff0c;这需…...

深入剖析k8s-控制器思想

引言 本文是《深入剖析Kubernetes》学习笔记——《深入剖析Kubernetes》 正文 控制器都遵循K8s的项目中一个通用的编排模式——控制循环 for {实际状态 : 获取集群中对象X的实际状态期望状态 : 获取集群中对象X的期望状态if 实际状态 期望状态 {// do nothing} else {执行…...

go并发模式之----使用时顺序模式

常见模式之二&#xff1a;使用时顺序模式 定义 顾名思义&#xff0c;起初goroutine不管是怎么个先后顺序&#xff0c;等到要使用的时候&#xff0c;需要按照一定的顺序来&#xff0c;也被称为未来使用模式 使用场景 每个goroutine函数都比较独立&#xff0c;不可通过参数循环…...

Appium+python自动化(十六)- ADB命令

简介 Android 调试桥(adb)是多种用途的工具&#xff0c;该工具可以帮助你你管理设备或模拟器 的状态。 adb ( Android Debug Bridge)是一个通用命令行工具&#xff0c;其允许您与模拟器实例或连接的 Android 设备进行通信。它可为各种设备操作提供便利&#xff0c;如安装和调试…...

工业安全零事故的智能守护者:一体化AI智能安防平台

前言&#xff1a; 通过AI视觉技术&#xff0c;为船厂提供全面的安全监控解决方案&#xff0c;涵盖交通违规检测、起重机轨道安全、非法入侵检测、盗窃防范、安全规范执行监控等多个方面&#xff0c;能够实现对应负责人反馈机制&#xff0c;并最终实现数据的统计报表。提升船厂…...

【力扣数据库知识手册笔记】索引

索引 索引的优缺点 优点1. 通过创建唯一性索引&#xff0c;可以保证数据库表中每一行数据的唯一性。2. 可以加快数据的检索速度&#xff08;创建索引的主要原因&#xff09;。3. 可以加速表和表之间的连接&#xff0c;实现数据的参考完整性。4. 可以在查询过程中&#xff0c;…...

连锁超市冷库节能解决方案:如何实现超市降本增效

在连锁超市冷库运营中&#xff0c;高能耗、设备损耗快、人工管理低效等问题长期困扰企业。御控冷库节能解决方案通过智能控制化霜、按需化霜、实时监控、故障诊断、自动预警、远程控制开关六大核心技术&#xff0c;实现年省电费15%-60%&#xff0c;且不改动原有装备、安装快捷、…...

srs linux

下载编译运行 git clone https:///ossrs/srs.git ./configure --h265on make 编译完成后即可启动SRS # 启动 ./objs/srs -c conf/srs.conf # 查看日志 tail -n 30 -f ./objs/srs.log 开放端口 默认RTMP接收推流端口是1935&#xff0c;SRS管理页面端口是8080&#xff0c;可…...

高等数学(下)题型笔记(八)空间解析几何与向量代数

目录 0 前言 1 向量的点乘 1.1 基本公式 1.2 例题 2 向量的叉乘 2.1 基础知识 2.2 例题 3 空间平面方程 3.1 基础知识 3.2 例题 4 空间直线方程 4.1 基础知识 4.2 例题 5 旋转曲面及其方程 5.1 基础知识 5.2 例题 6 空间曲面的法线与切平面 6.1 基础知识 6.2…...

IoT/HCIP实验-3/LiteOS操作系统内核实验(任务、内存、信号量、CMSIS..)

文章目录 概述HelloWorld 工程C/C配置编译器主配置Makefile脚本烧录器主配置运行结果程序调用栈 任务管理实验实验结果osal 系统适配层osal_task_create 其他实验实验源码内存管理实验互斥锁实验信号量实验 CMISIS接口实验还是得JlINKCMSIS 简介LiteOS->CMSIS任务间消息交互…...

【数据分析】R版IntelliGenes用于生物标志物发现的可解释机器学习

禁止商业或二改转载&#xff0c;仅供自学使用&#xff0c;侵权必究&#xff0c;如需截取部分内容请后台联系作者! 文章目录 介绍流程步骤1. 输入数据2. 特征选择3. 模型训练4. I-Genes 评分计算5. 输出结果 IntelliGenesR 安装包1. 特征选择2. 模型训练和评估3. I-Genes 评分计…...

Go 语言并发编程基础:无缓冲与有缓冲通道

在上一章节中&#xff0c;我们了解了 Channel 的基本用法。本章将重点分析 Go 中通道的两种类型 —— 无缓冲通道与有缓冲通道&#xff0c;它们在并发编程中各具特点和应用场景。 一、通道的基本分类 类型定义形式特点无缓冲通道make(chan T)发送和接收都必须准备好&#xff0…...

宇树科技,改名了!

提到国内具身智能和机器人领域的代表企业&#xff0c;那宇树科技&#xff08;Unitree&#xff09;必须名列其榜。 最近&#xff0c;宇树科技的一项新变动消息在业界引发了不少关注和讨论&#xff0c;即&#xff1a; 宇树向其合作伙伴发布了一封公司名称变更函称&#xff0c;因…...