小程序翻牌效果

在这里插入图片描述

<view class="wrap">
    <view class="card-module">
        <view class="card {{ item.showClass ? 'change' : ''}} "  wx:for="{{cardData}}" wx:for-index="index" wx:for-item="item"
            wx:key="{{ item.id }}" animation="{{item.animationData}}" bindtap="handleCurClick" data-id="{{ item.id }}" data-disabled="{{ item.disabled }}" style="widht: {{carWidth}}; height: {{carWidth}}">
            <view class="front card-item"><image src="{{item.front}}"></image></view>
            <view class="back card-item {{ item.opacity ? 'opacity' : ''}}"><image src="{{item.back}}"></image></view>
        </view>
    </view>
    <view class="num">你有抽奖机会{{ number }}次</view>
</view>
.card-module {padding: 20rpx;display: flex;flex-direction: row;flex-wrap: wrap;justify-content: space-around;transform: translate3d(0, 0, 0);}

.card-module .card {width: 200rpx;height: 200rpx;line-height: 200rpx;text-align: center;color: #fff;margin: 11rpx;position: relative;overflow: hidden;}

.card-module .card .card-item { position: absolute;left: 0;top: 0;width: 100%;height: 100%;transition: all .5s ease-in-out;transform-style: preserve-3d;backface-visibility: hidden;box-sizing: border-box;}
.card-module .card image {width: 100%;height: 100%;}

.card-module .card .front {/* background-color: red; */transform: rotateY(0deg);-webkit-transform: rotateY(0deg); z-index: 2;}

.card-module .card .back {/* background-color: #009fff; */transform: rotateY(180deg);-webkit-transform: rotateY(180deg);z-index: 1;}

.card-module .card.change .front {z-index: 1;transform: rotateY(180deg);-webkit-transform: rotateY(180deg);}

.card-module .card.change .back {z-index: 2;transform: rotateY(0deg);-webkit-transform: rotateY(0deg);}
.card-module .card.change .opacity {opacity: 0.5;}
Page({
    data: {
        carWidth: '', //卡片宽度
        number: 10,
        cardData: [{
                animationData: {},
                front: '../../images/1f.png',
                back: '../../images/1z.png',
                id: '1',
                showClass: false,  // 控制翻转
                opacity: false, // 控制翻转过来以后的 opacity
                money: 1,
                disabled: false,  // disabled 属性,控制手速点击过快,导致多个牌被翻开, 默认为false 可以点击
            },
            {
                animationData: {},
                front: '../../images/2f.png',
                back: '../../images/2z.png',
                id: '2',
                showClass: false,
                opacity: false,
                money: 2,
                disabled: false,
            },
            {
                animationData: {},
                front: '../../images/3f.png',
                back: '../../images/3z.png',
                id: '3',
                showClass: false,
                opacity: false,
                money: 3,
                disabled: false,
            },
            {
                animationData: {},
                front: '../../images/4f.png',
                back: '../../images/4z.png',
                id: '4',
                showClass: false,
                opacity: false,
                money: 4,
                disabled: false,
            },
            {
                animationData: {},
                front: '../../images/5f.png',
                back: '../../images/5z.png',
                id: '5',
                showClass: false,
                opacity: false,
                money: 5,
                disabled: false,
            },
            {
                animationData: {},
                front: '../../images/6f.png',
                back: '../../images/6z.png',
                id: '6',
                showClass: false,
                opacity: false,
                money: 6,
                disabled: false,
            },
            {
                animationData: {},
                front: '../../images/7f.png',
                back: '../../images/7z.png',
                id: '7',
                showClass: false,
                opacity: false,
                money: 7,
                disabled: false,
            },
            {
                animationData: {},
                front: '../../images/8f.png',
                back: '../../images/8z.png',
                id: '8',
                showClass: false,
                opacity: false,
                money: 8,
                disabled: false,
            },
            {
                animationData: {},
                front: '../../images/9f.png',
                back: '../../images/9z.png',
                id: '9',
                showClass: false,
                opacity: false,
                money: 9,
                disabled: false,
            },
        ],
    },
    onLoad() {
        let carWidth = 0;
          const { cardData } = this.data;  //es6写法 相当于const cardData = this.data.cardData;
        this.addPosition(cardData); // 数组添加移动坐标位置
        wx.getSystemInfo({
            success(res) {
                carWidth = parseInt((res.windowWidth - 40) / 3);
                
            }
        })
        this.setData({
            carWidth
        })          
    },

    // 数组添加移动坐标值 并且把所有的disabled 状态还原会false 
    addPosition(cardData){
        const lineTotal = 3 // 单行数
        cardData.map((item, index) => {
            let x = index % lineTotal
            let y = parseInt(index / lineTotal)
            item.twoArry = { x, y }
            item.disabled = false;   // 还原所有的disabled 状态
        })
        this.setData({cardData})
    },

    //全部翻转
    allChange() {
        const { cardData } = this.data
        cardData.map(item => {
            if (!item.showClass) {
                item.showClass = true;
            }
        })
        this.setData({
            cardData
        })
    },

    //洗牌
    allMove() {
        const { carWidth, cardData } = this.data;
        // 110 是卡牌宽度加边距
        this.shuffle(carWidth) //移动到中心,  110 是牌的宽度,加上外边距边框
        let timer = setTimeout(() => {
            // 每次移动到中心位置以后,先打乱数组顺序,给数组每一项重新添加移动坐标值,setData({cardData}) 然后在散开
            cardData.sort(this.randomsort);
            this.addPosition(cardData)
            clearTimeout(timer)
            this.shuffle(0) // 间隔1秒钟,移动到原来位置
        }, 1000)
    },
    // 洗牌函数
    shuffle(translateUnit) {
        let { cardData } = this.data;
        console.log(cardData)
        cardData.map((item, index) => {
            let animation = wx.createAnimation({
                duration: 500,
                timingFunction: 'ease'
            })
            animation.export()
            const translateUnitX = translateUnit * (1 - item.twoArry.x)
            const translateUnitY = translateUnit * (1 - item.twoArry.y)
            animation.translate(translateUnitX, translateUnitY).step()
            item.animationData = animation.export()
            item.opacity = false;
            if (item.showClass) {
                item.showClass = false;
            }
        })
        this.setData({
            cardData
        })
    },

    // 打乱数组顺序
    randomsort(a, b) {
        return Math.random()>.5 ? -1 : 1;
        //用Math.random()函数生成0~1之间的随机数与0.5比较,返回-1或1
    },

    // 处理单个点击翻转
    handleCurClick(event) {
        let curId = event.currentTarget.dataset.id;
        // 每次点击时获取被点击拍的disable 属性,
        let disabled = event.currentTarget.dataset.disabled;
        //如果为true 就返回不继续向下执行
        if(disabled){
           return; 
        }
        let { cardData, number, carWidth } = this.data;
        let money = '';
        cardData.forEach(item => {
            item.disabled = true;  // 点击一张拍以后,把所有的牌的disabled 属性改成true ,
            if (item.id === curId) {
                item.showClass = true;
                money = item.money;
            }else {
                item.opacity = true
            }
        })
        number -= 1;
        this.setData({
            cardData, 
            number
        })
        setTimeout(() => {
            this.allChange()
        }, 1000);
        let _this = this;
        setTimeout(() => {
            wx.showModal({
                title: '提示',
                content: '恭喜您中奖'+ money +'元!',
                cancelText: '去看看',
                confirmText: '再翻一次',
                success(res) {
                    if (res.confirm) {
                        console.log('用户点击确定')
                        _this.shuffle(carWidth) //移动到中心,  110 是牌的宽度,加上外边距边框
                        wx.showLoading({
                            title: '获取数据中...',
                        })
                        // 这里去请求接口重新获取数据,获取成功以后调用 this.shuffle(0) 这里用
                        setTimeout(() => {
                            wx.hideLoading()
                            // 每次移动到中心位置以后,先打乱数组顺序,给数组每一项重新添加移动坐标值,setData({cardData}) 然后在散开
                            cardData.sort(_this.randomsort);
                            _this.addPosition(cardData)
                            _this.shuffle(0)
                        }, 3000)
                    } else if (res.cancel) {
                        console.log('用户点击取消')
                    }
                }
            })
        }, 3000);
    }
})
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个简单的微信小程序翻牌游戏项目实例: 1. 在微信开发者工具中创建一个新的小程序项目,选择“小程序开发框架”为“原生框架”,并填写相应的项目信息。 2. 在项目根目录下创建一个名为“pages”的文件夹,用于存放小程序的页面。 3. 在“pages”文件夹下创建一个名为“index”的文件夹,用于存放小程序的首页。 4. 在“index”文件夹下创建一个名为“index.wxml”的文件,用于编写小程序的页面结构。 5. 在“index”文件夹下创建一个名为“index.wxss”的文件,用于编写小程序的页面样式。 6. 在“index”文件夹下创建一个名为“index.js”的文件,用于编写小程序的页面逻辑。 7. 在“index.wxml”文件中编写小程序的页面结构,例如: ```html <view class="container"> <view class="card" bindtap="flipCard" data-index="0"> <image class="front" src="{{cards[0].front}}" /> <image class="back" src="{{cards[0].back}}" /> </view> <view class="card" bindtap="flipCard" data-index="1"> <image class="front" src="{{cards[1].front}}" /> <image class="back" src="{{cards[1].back}}" /> </view> <!-- 其他卡牌 --> </view> ``` 8. 在“index.wxss”文件中编写小程序的页面样式,例如: ```css .container { display: flex; flex-wrap: wrap; justify-content: center; } .card { width: 100px; height: 100px; margin: 10px; position: relative; } .front, .back { width: 100%; height: 100%; position: absolute; top: 0; left: 0; transition: transform 0.5s ease-in-out; transform-style: preserve-3d; backface-visibility: hidden; } .front { transform: rotateY(0deg); } .back { transform: rotateY(180deg); } ``` 9. 在“index.js”文件中编写小程序的页面逻辑,例如: ```javascript Page({ data: { cards: [ { front: 'front1.png', back: 'back.png', flipped: false }, { front: 'front2.png', back: 'back.png', flipped: false }, // 其他卡牌 ], flippedCards: [] }, flipCard: function (event) { var index = event.currentTarget.dataset.index; var card = this.data.cards[index]; if (!card.flipped && this.data.flippedCards.length < 2) { card.flipped = true; this.data.flippedCards.push(card); this.setData({ cards: this.data.cards }); if (this.data.flippedCards.length == 2) { setTimeout(this.checkMatch, 1000); } } }, checkMatch: function () { var card1 = this.data.flippedCards[0]; var card2 = this.data.flippedCards[1]; if (card1.front == card2.front) { card1.matched = true; card2.matched = true; this.setData({ cards: this.data.cards }); } else { card1.flipped = false; card2.flipped = false; this.setData({ cards: this.data.cards }); } this.data.flippedCards = []; } }); ``` 以上代码实现了一个简单的翻牌游戏,玩家需要翻开两张卡牌,如果两张卡牌的正面图片相同,则这两张卡牌会被消除,否则这两张卡牌会被翻回去。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值