uni-app(H5)拼图游戏

业务需求

给你一张图片,平均分成4份(左1),打乱顺序(注意,拆分后的单图只是水平方向上的位置切换,右1),用户移动图片,从右1还原成左1。
在这里插入图片描述

思路

一张图片分四份,怎么拆?
找UI要psd,拿到图后,用工具切出1,2,3,4位置的图,然后画页面布局好。
拆完怎么随机?

let puzzleArray= [
	{
		index:0,
		imgUrl:'game1.png'
	},
	{
		index:1,
		imgUrl:'game2.png'
	},
	{
		index:2,
		imgUrl:'game3.png'
	},
	{
		index:3,
		imgUrl:'game4.png'
	},
]
puzzleArray.sort(()=>{
	return Math.round(Math.random()) - 0.5;
})
<view v-for="(item,index) in arr"></view>

随机排列好,怎么移动?移动完怎么交换?

//利用移动端的touch事件 具体可查看项目源码
touchstart touchmove touchend

交完完怎么校验是否拼好了呢?

var tmpArr= puzzleArray.filter((item, index) => {
	if (index != item.index) {
		return true
	} else {
		return false
	}
})
tmpArr.length == 0 证明拼好了

功能实现了,但不友好。这只是4份,如果是9*9呢?81份!
怎么办?
在这里插入图片描述
精灵图

一张图分成4份:

let puzzleArray = [
    {
        "backgroundPosition":"background-position: 0.00% 0.00%",
        "index":0,
        "leftStart":0,
        "leftEnd":0,
        "topStart":0,
        "topEnd":0,
        "id":"game_1_puzzle_0"
    },
    {
        "backgroundPosition":"background-position: 100.00% 0.00%",
        "index":1,
        "leftStart":0,
        "leftEnd":0,
        "topStart":0,
        "topEnd":0,
        "id":"game_1_puzzle_1"
    },
    {
        "backgroundPosition":"background-position: 0.00% 100.00%",
        "index":2,
        "leftStart":0,
        "leftEnd":0,
        "topStart":0,
        "topEnd":0,
        "id":"game_1_puzzle_2"
    },
    {
        "backgroundPosition":"background-position: 100.00% 100.00%",
        "index":3,
        "leftStart":0,
        "leftEnd":0,
        "topStart":0,
        "topEnd":0,
        "id":"game_1_puzzle_3"
    }
]
puzzleArray.sort(() => {
		return Math.round(Math.random()) - 0.5;
	})

使用精灵图,无论多少份,我们只需要计算出每一块位置的background-position即可。
项目源码请移步:uniapp-component

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值