国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

vue3+uniapp在微信小程序?qū)崿F(xiàn)一個(gè)2048小游戲

這篇具有很好參考價(jià)值的文章主要介紹了vue3+uniapp在微信小程序?qū)崿F(xiàn)一個(gè)2048小游戲。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

一、效果展示

vue 小程序 游戲,uni-app,微信小程序,小程序

二、代碼

<template>
	<view class="page">
		<view class="top">
			<view class="score">
				得分:{{total}}
			</view>
			<view class="time">
				用時(shí):{{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()">
							結(jié)束
						</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'
	// 游戲狀態(tài)
	const gameStatus = ref<boolean>(false);
	// 顯示的數(shù)組
	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>();
	// 用時(shí)
	const allTime = ref(0)
	const timer1 = ref()
	// 游戲開始
	const gameStart = () => {
		total.value = 0;
		allTime.value = 0
		gameStatus.value = true;
		gameBoard.value = numInit()
		timer1.value = setInterval(() => {
			allTime.value = allTime.value + 1;
		}, 1000)
	}
	// 游戲結(jié)束
	const gameOver = () => {
		gameStatus.value = false;
		clearInterval(timer1.value)
		timer1.value = null;
		newArr.value = Array.from({ length: 2 }, () => Array(2).fill(null));

	}
	// 獲取隨機(jī)數(shù)的函數(shù)
	const getRandomlet = (min, max) => {
		min = Math.ceil(min);
		max = Math.floor(max);
		return Math.floor(Math.random() * (max - min + 1)) + min;
	}
	// 隨機(jī)初始化數(shù)值
	const numInit = () => {
		const array = Array.from({ length: 4 }, () => Array(4).fill(0));
		const positions = [];
		// 生成一個(gè)包含所有可能位置的數(shù)組  
		for (let i = 0; i < 4; i++) {
			for (let j = 0; j < 4; j++) {
				positions.push({ x: i, y: j });
			}
		}
		// 隨機(jī)選擇6個(gè)位置  
		const selectedPositions = [];
		for (let i = 0; i < 6; i++) {
			const randomIndex = getRandomlet(0, positions.length - 1);
			selectedPositions.push(positions[randomIndex]);
			positions.splice(randomIndex, 1); // 從數(shù)組中移除已選位置,避免重復(fù)選擇  
		}
		// 設(shè)置前4個(gè)位置為2  
		for (let i = 0; i < 4; i++) {
			const position = selectedPositions[i];
			array[position.x][position.y] = 2;
		}
		// 對(duì)于剩下的2個(gè)位置,隨機(jī)設(shè)置為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;
	}
	// 旋轉(zhuǎn)數(shù)組
	const rotate90Clockwise = (matrix) => {
		const n = matrix.length;
		let rotatedMatrix = Array.from({ length: n }, () => []);

		// 順時(shí)針旋轉(zhuǎ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;
	}
	// 累計(jì)與填入
	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++) {
				// 找到第一個(gè)不為0
				if (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;
						}
						// 移動(dòng)到第一個(gè)0
						if (copiedArray[i][p] == 0) {
							copiedArray[i][p] = copiedArray[i][j];
							copiedArray[i][j] = 0;
						}
					}
				}

			}
		}
		total.value = total.value + defen
		return copiedArray;
	}
	// 添加新數(shù)字
	const addRandomNumbersToZeros = (arr) => {
		let matrix = JSON.parse(JSON.stringify(arr));
		// 存儲(chǔ)所有值為0的元素的坐標(biāo)  
		let zeroIndices = [];

		// 遍歷二維數(shù)組,找到值為0的元素的坐標(biāo)  
		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]);
				}
			}
		}

		// 如果沒(méi)有0,則無(wú)法添加數(shù)字  
		if (zeroIndices.length < 2) {
			gameOver()
			return;
		}

		// 從所有0的坐標(biāo)中隨機(jī)選擇兩個(gè)  
		let randomIndices = zeroIndices.sort(() => 0.5 - Math.random()).slice(0, 2);

		// 為這兩個(gè)坐標(biāo)對(duì)應(yīng)的元素添加隨機(jī)數(shù)字  
		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;
	}

	// 移動(dòng)
	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>

三、體驗(yàn)地址

微信小程序搜索《靜遠(yuǎn)的工具箱》:偶數(shù)求和那個(gè)功能文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-848352.html

到了這里,關(guān)于vue3+uniapp在微信小程序?qū)崿F(xiàn)一個(gè)2048小游戲的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • uniapp微信小程序投票系統(tǒng)實(shí)戰(zhàn) (SpringBoot2+vue3.2+element plus ) -投票帖子明細(xì)實(shí)現(xiàn)

    uniapp微信小程序投票系統(tǒng)實(shí)戰(zhàn) (SpringBoot2+vue3.2+element plus ) -投票帖子明細(xì)實(shí)現(xiàn)

    鋒哥原創(chuàng)的uniapp微信小程序投票系統(tǒng)實(shí)戰(zhàn): uniapp微信小程序投票系統(tǒng)實(shí)戰(zhàn)課程 (SpringBoot2+vue3.2+element plus ) ( 火爆連載更新中... )_嗶哩嗶哩_bilibili uniapp微信小程序投票系統(tǒng)實(shí)戰(zhàn)課程 (SpringBoot2+vue3.2+element plus ) ( 火爆連載更新中... )共計(jì)21條視頻,包括:uniapp微信小程序投票系統(tǒng)實(shí)

    2024年01月22日
    瀏覽(29)
  • uniapp微信小程序投票系統(tǒng)實(shí)戰(zhàn) (SpringBoot2+vue3.2+element plus ) -投票帖子詳情實(shí)現(xiàn)

    uniapp微信小程序投票系統(tǒng)實(shí)戰(zhàn) (SpringBoot2+vue3.2+element plus ) -投票帖子詳情實(shí)現(xiàn)

    鋒哥原創(chuàng)的uniapp微信小程序投票系統(tǒng)實(shí)戰(zhàn): uniapp微信小程序投票系統(tǒng)實(shí)戰(zhàn)課程 (SpringBoot2+vue3.2+element plus ) ( 火爆連載更新中... )_嗶哩嗶哩_bilibili uniapp微信小程序投票系統(tǒng)實(shí)戰(zhàn)課程 (SpringBoot2+vue3.2+element plus ) ( 火爆連載更新中... )共計(jì)21條視頻,包括:uniapp微信小程序投票系統(tǒng)實(shí)

    2024年01月16日
    瀏覽(26)
  • uniapp微信小程序投票系統(tǒng)實(shí)戰(zhàn) (SpringBoot2+vue3.2+element plus ) -投票帖子排行實(shí)現(xiàn)

    uniapp微信小程序投票系統(tǒng)實(shí)戰(zhàn) (SpringBoot2+vue3.2+element plus ) -投票帖子排行實(shí)現(xiàn)

    鋒哥原創(chuàng)的uniapp微信小程序投票系統(tǒng)實(shí)戰(zhàn): uniapp微信小程序投票系統(tǒng)實(shí)戰(zhàn)課程 (SpringBoot2+vue3.2+element plus ) ( 火爆連載更新中... )_嗶哩嗶哩_bilibili uniapp微信小程序投票系統(tǒng)實(shí)戰(zhàn)課程 (SpringBoot2+vue3.2+element plus ) ( 火爆連載更新中... )共計(jì)21條視頻,包括:uniapp微信小程序投票系統(tǒng)實(shí)

    2024年01月19日
    瀏覽(20)
  • uni-app:vue3 + uni-app 在微信小程序中無(wú)法使用app.component全局注冊(cè)組件

    按上文中的代碼執(zhí)行后,會(huì)發(fā)現(xiàn)在微信小程序開發(fā)中全局注冊(cè)的組件是無(wú)法顯示的,這是uniapp的一個(gè)未解決bug, 在uniapp中出了可以通過(guò)vue實(shí)例的component方法注冊(cè)全局組件外,uniapp支持另一種全局注冊(cè)的方式,就是通過(guò) easycom 掃描注冊(cè),步驟如下 easycom 的掃描流程是:通過(guò)代碼

    2024年02月16日
    瀏覽(105)
  • uniapp微信小程序投票系統(tǒng)實(shí)戰(zhàn) (SpringBoot2+vue3.2+element plus ) -全局異常統(tǒng)一處理實(shí)現(xiàn)

    uniapp微信小程序投票系統(tǒng)實(shí)戰(zhàn) (SpringBoot2+vue3.2+element plus ) -全局異常統(tǒng)一處理實(shí)現(xiàn)

    鋒哥原創(chuàng)的uniapp微信小程序投票系統(tǒng)實(shí)戰(zhàn): uniapp微信小程序投票系統(tǒng)實(shí)戰(zhàn)課程 (SpringBoot2+vue3.2+element plus ) ( 火爆連載更新中... )_嗶哩嗶哩_bilibili uniapp微信小程序投票系統(tǒng)實(shí)戰(zhàn)課程 (SpringBoot2+vue3.2+element plus ) ( 火爆連載更新中... )共計(jì)21條視頻,包括:uniapp微信小程序投票系統(tǒng)實(shí)

    2024年02月03日
    瀏覽(23)
  • uniapp微信小程序投票系統(tǒng)實(shí)戰(zhàn) (SpringBoot2+vue3.2+element plus ) -我創(chuàng)建的投票列表實(shí)現(xiàn)

    uniapp微信小程序投票系統(tǒng)實(shí)戰(zhàn) (SpringBoot2+vue3.2+element plus ) -我創(chuàng)建的投票列表實(shí)現(xiàn)

    鋒哥原創(chuàng)的uniapp微信小程序投票系統(tǒng)實(shí)戰(zhàn): uniapp微信小程序投票系統(tǒng)實(shí)戰(zhàn)課程 (SpringBoot2+vue3.2+element plus ) ( 火爆連載更新中... )_嗶哩嗶哩_bilibili uniapp微信小程序投票系統(tǒng)實(shí)戰(zhàn)課程 (SpringBoot2+vue3.2+element plus ) ( 火爆連載更新中... )共計(jì)21條視頻,包括:uniapp微信小程序投票系統(tǒng)實(shí)

    2024年01月21日
    瀏覽(24)
  • 【vue3】uniapp 微信小程序 打包優(yōu)化【超詳細(xì)】

    【vue3】uniapp 微信小程序 打包優(yōu)化【超詳細(xì)】

    使用HBuilder編輯器編譯uniapp的項(xiàng)目轉(zhuǎn)為微信小程序,并上傳發(fā)布項(xiàng)目 微信小程序官網(wǎng)限制發(fā)布的主包大小不能超過(guò)2mb,我的項(xiàng)目編譯后大小為3mb 1.vendor.js文件過(guò)大,打包的時(shí)候并沒(méi)有設(shè)置為mini版 2.項(xiàng)目的主包太大,并沒(méi)有分包出去(后面會(huì)詳細(xì)說(shuō)明如何分包) 1.把微信小程序右

    2024年02月09日
    瀏覽(178)
  • vue3+ts+vite 搭建uniapp項(xiàng)目(微信小程序)

    vue3+ts+vite 搭建uniapp項(xiàng)目(微信小程序)

    模板下載: uniapp 官網(wǎng)通過(guò)vue-cli 命令行創(chuàng)建uniapp,參考uni-app官網(wǎng),使用? npx degit dcloudio/uni-preset-vue#vite-ts my-vue3-project 下載模板; 安裝css預(yù)處理 sass: 項(xiàng)目終端輸入: yarn add node-sass@^4.0.0 sass-loader@^10.0.1 sass (模板沒(méi)有默認(rèn)安裝sass, 如果不安裝直接使用會(huì)報(bào)錯(cuò)) ?安裝uni-ui組件

    2024年02月09日
    瀏覽(51)
  • 【vue】uniapp vue3 vite代理設(shè)置問(wèn)題【H5 微信小程序】

    基于vue3版本的uniapp運(yùn)行h5和微信小程序 uniapp運(yùn)行h5請(qǐng)求接口成功,運(yùn)行微信小程序請(qǐng)求接口不成功 vite.config.ts配置proxy .env配置請(qǐng)求接口域名 request.ts 請(qǐng)求接口文件 微信小程序識(shí)別不了代理的配置 需要判斷當(dāng)前是h5還是微信小程序端,對(duì)請(qǐng)求接口文件進(jìn)行修改,其他文件不修

    2024年02月09日
    瀏覽(26)
  • 微信小程序uniapp+vue3+ts+pinia的環(huán)境搭建

    一.創(chuàng)建uniapp項(xiàng)目 通過(guò)vue-cli創(chuàng)建 二.安裝依賴: 1.pnpm i 2.運(yùn)行項(xiàng)目: 將package.json的 3.導(dǎo)入微信小程序開發(fā)工具 打開微信開發(fā)者工具, 導(dǎo)入 distdevmp-weixin 運(yùn)行 三. TS 類型校驗(yàn) 在tsconfig.json文件中\(zhòng)\\"compilerOptions\\\"配置項(xiàng)內(nèi)添加\\\"ignoreDeprecations\\\": “5.0” 額外配置Ts類型校驗(yàn): 安裝類型

    2024年04月10日
    瀏覽(99)

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包