“打工搬砖记”中吃什么的轮盘功能实现(二)

2024-05-15 01:20

本文主要是介绍“打工搬砖记”中吃什么的轮盘功能实现(二),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

    • 打工搬砖记
    • 转盘主要的逻辑实现
    • 转盘的素材
    • 小结

打工搬砖记

先来一个吃什么轮盘的预览图,这轮盘文案加字呈圆形铺出来,开始后旋转到指定的选项处停下来。
已上线小程序“打工人搬砖记”,可以扫码进行预览观看。
请添加图片描述
请添加图片描述

转盘主要的逻辑实现

1、通过绑定style绑定值为“prizeBoxStyle”,设置每个选项的颜色以及后续转盘转动需要操作这个值。
2、通过绑定style绑定值为“prizeStyle”,然后通过“rotateAngle()”方法计算出总共有几个选项,然后算出每个选项需要旋转的角度。
3、通过“totalRunAngle()”计算出转盘最后需要转动角度,所以说游戏在开始的一瞬间就程序就已经知道,抽中的是那一个。
4、为了使转盘有声音就使用“uni.createInnerAudioContext()”来创建音乐,这个地方需要注意的是,如果想要后续“.stop()”停下音乐,在创建的时候一定不要设置为自动播放,就是说“autoplay = false”。

具体代码如下:

<template><view><image src="../../static/lunch/lunchBcg.jpg" class="bcgImg" mode=""></image><view class="container"><view class="container-title"></view><view class="prize-box-relative"><view class="prize-box"><view class="prize-list" ref="prizeWrap" :style="prizeBoxStyle"><div class="prize-item" v-for="(item, index) in prizeList" :style="[prizeStyle(index)]"><text>{{ item }}</text></div></view></view></view><image @click="openGame()" src="../../static/lunch/turntable.png" class="turntableImg" mode=""></image></view><!-- 弹框 :抽中要吃的--><view v-show="tipShow" class="tip_box"><view class="tip_content"><image src="../../static/lunch/tip.png" class="tip_img" mode=""></image><view class="tip_txt">{{prizeList[prizeId] }}</view><view class="tip_btn" @click="tipShow = false"></view></view></view></view>
</template><script>import {turnList} from 'config/api.js';export default {data() {return {// colorList: ['#f69602', '#f8ca41', '#ffe7c9','#afe2ec'],colorList: ['#fe9539', '#fadf94', '#5fcbd1', '#ffffff'],prizeList: ["豆浆", "胡辣汤", "韭菜鸡蛋韭菜鸡蛋", "奖品4", "奖品5", "奖品6", "奖品4", "奖品5", "奖品6"],isRunning: false, // 是否正在抽奖baseRunAngle: 360 * 5, // 总共转动角度 至少5圈prizeId: 0, // 中奖idrotatePrize: 40,tipShow: false,prizeBoxStyle: '',audioMusic: '',}},onLoad() {this.getPrizeList()uni.$on('updateTurn', (data) => {this.getPrizeList()})},methods: {getPrizeList() {turnList({data: {eatType: this.activeMenu},custom: {auth: true,toast: false,catch: true}}).then((res) => {let arr = []res.map(value => {arr.push(value.name)})this.prizeList = arrthis.rotatePrize = 360 / arr.lengththis.prizeBoxStyle = this.bgColor()this.initGame()})},// 初始化转盘游戏initGame() {this.rotateAngle()},// 开始游戏openGame() {this.startGame()},// 开始游戏startGame() {if (!this.isRunning) {this.isRunning = truethis.prizeId = this.getRandomNum()this.startRun()}},// 游戏运行startRun() {this.prizeBoxStyle = `${this.bgColor()} transform: rotate(${this.totalRunAngle()}deg);transition: all 4s ease;`this.audioMusic = uni.createInnerAudioContext();this.audioMusic.autoplay = false;this.audioMusic.startTime = 2;this.audioMusic.sessionCategory = 'ambient';this.audioMusic.src = '/static/lunch/music01.mp3';this.audioMusic.play();setTimeout(() => {this.audioMusic.stop()this.audioMusic.destroy()this.audioMusic = null}, 3700)this.playAudio()setTimeout(() => {this.tipShow = truethis.isRunning = falsethis.prizeBoxStyle = `${this.bgColor()} transform: rotate(${this.totalRunAngle() - this.baseRunAngle}deg);`}, 4000)},getRandomNum() {const num = Math.floor(Math.random() * this.prizeList.length)return num},// 平均每个奖品角度rotateAngle() {this.rotatePrize = 360 / this.prizeList.length},// 奖品布局prizeStyle(i) {let _degree = this.rotatePrizelet style = {}style.width = 2 * 130 * Math.sin(_degree / 2 * Math.PI / 180) + 'px'style.height = '130px'style.transform = `rotate(${_degree * i + _degree / 2}deg)`style.transformOrigin = '50% 100%'return style},// 计算绘制转盘背景bgColor() {let colorVal = ''for (let i = 0; i < this.prizeList.length; i++) {colorVal += `${this.colorList[i % 4]} ${this.rotatePrize * i}deg ${this.rotatePrize * (i + 1)}deg,`}return ` background: conic-gradient(${colorVal.slice(0, -1)});transform: rotate(-${this.rotatePrize/2}deg); `},// 要执行总转角度数totalRunAngle() {return this.baseRunAngle + 360 - this.prizeId * this.rotatePrize - this.rotatePrize / 2},// 转盘停下来时的声音playAudio() {setTimeout(() => {const innerAudioContext = uni.createInnerAudioContext();innerAudioContext.autoplay = true;innerAudioContext.sessionCategory = 'ambient';innerAudioContext.src = '/static/lunch/music.mp3';innerAudioContext.onPlay();}, 3800)}}}
</script><style lang="scss">.bcgImg {position: fixed;width: 750rpx;height: 100vh;}.container {position: relative;width: 660rpx;height: 660rpx;margin: 0 auto;}.container-title {font-size: 56rpx;font-weight: bold;color: #FFFFFF;text-align: center;line-height: 160rpx;height: 160rpx;letter-spacing: 20px;}.turntableImg {width: 100%;height: 100%;position: absolute;top: 160rpx;}.prize-box-relative {width: 100%;height: 100%;display: flex;align-items: center;justify-content: center;}.prize-box {width: 300px;height: 300px;position: relative;}.prize-list {width: 100%;height: 100%;border-radius: 50%;overflow: hidden;}.prize-item {/*border: 2px solid red;*/position: absolute;left: 0;right: 0;top: 20px;margin: auto;text-align: center;}.prize-item text {display: flex;align-items: center;writing-mode: vertical-rl;color: #333333;font-size: 26rpx;font-weight: bold;text-align: center;line-height: 20px;width: 100%;}.tip_box {z-index: 10;position: fixed;top: 0;width: 100%;height: 100vh;background: rgba(0, 0, 0, 0.5);display: flex;align-items: center;justify-content: center;}.tip_content {position: relative;}.tip_img {width: 640rpx;height: 480rpx;}.tip_txt {position: absolute;top: 160rpx;width: 600rpx;text-align: center;font-weight: bold;font-size: 38rpx;}.tip_btn {position: absolute;bottom: 60rpx;left: 210rpx;width: 200rpx;height: 100rpx;}.food_menu_list {position: fixed;bottom: 100rpx;width: 710rpx;padding: 50rpx 20rpx;display: flex;flex-wrap: wrap;font-size: 26rpx;color: #999999;font-weight: bold;.menu_box {margin: 0 10rpx;width: 174rpx;background: rgba(244, 244, 244, 0.6);box-shadow: 0 0 10rpx #FFFFFF;display: flex;align-items: center;padding: 20rpx;border-radius: 10rpx;margin-bottom: 30rpx;.menu_img {width: 40rpx;height: 40rpx;margin-right: 10rpx;}}.menu_active {color: #53851b;background: #f4f4f4;box-shadow: 0 0 10rpx #f4f4f4;}.menu_btn {font-size: 36rpx;color: #ffffff;line-height: 80rpx;text-align: center;width: 100%;border-radius: 10rpx;border: 2rpx #FFFFFF solid;}}
</style>

转盘的素材

我发现穿上后好像就成为了jpg格式了,注意转盘哪个一定要是png格式的,不然会把转盘的内容给覆盖住,音乐和背景图的话就自己找找吧!
请添加图片描述
请添加图片描述

小结

总的来说实现过程不难,难的是思路以及行动力。加油打工人!!!

这篇关于“打工搬砖记”中吃什么的轮盘功能实现(二)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/990403

相关文章

C++中unordered_set哈希集合的实现

《C++中unordered_set哈希集合的实现》std::unordered_set是C++标准库中的无序关联容器,基于哈希表实现,具有元素唯一性和无序性特点,本文就来详细的介绍一下unorder... 目录一、概述二、头文件与命名空间三、常用方法与示例1. 构造与析构2. 迭代器与遍历3. 容量相关4

C++中悬垂引用(Dangling Reference) 的实现

《C++中悬垂引用(DanglingReference)的实现》C++中的悬垂引用指引用绑定的对象被销毁后引用仍存在的情况,会导致访问无效内存,下面就来详细的介绍一下产生的原因以及如何避免,感兴趣... 目录悬垂引用的产生原因1. 引用绑定到局部变量,变量超出作用域后销毁2. 引用绑定到动态分配的对象,对象

SpringBoot基于注解实现数据库字段回填的完整方案

《SpringBoot基于注解实现数据库字段回填的完整方案》这篇文章主要为大家详细介绍了SpringBoot如何基于注解实现数据库字段回填的相关方法,文中的示例代码讲解详细,感兴趣的小伙伴可以了解... 目录数据库表pom.XMLRelationFieldRelationFieldMapping基础的一些代

Java HashMap的底层实现原理深度解析

《JavaHashMap的底层实现原理深度解析》HashMap基于数组+链表+红黑树结构,通过哈希算法和扩容机制优化性能,负载因子与树化阈值平衡效率,是Java开发必备的高效数据结构,本文给大家介绍... 目录一、概述:HashMap的宏观结构二、核心数据结构解析1. 数组(桶数组)2. 链表节点(Node

Java AOP面向切面编程的概念和实现方式

《JavaAOP面向切面编程的概念和实现方式》AOP是面向切面编程,通过动态代理将横切关注点(如日志、事务)与核心业务逻辑分离,提升代码复用性和可维护性,本文给大家介绍JavaAOP面向切面编程的概... 目录一、AOP 是什么?二、AOP 的核心概念与实现方式核心概念实现方式三、Spring AOP 的关

Python实现字典转字符串的五种方法

《Python实现字典转字符串的五种方法》本文介绍了在Python中如何将字典数据结构转换为字符串格式的多种方法,首先可以通过内置的str()函数进行简单转换;其次利用ison.dumps()函数能够... 目录1、使用json模块的dumps方法:2、使用str方法:3、使用循环和字符串拼接:4、使用字符

Linux下利用select实现串口数据读取过程

《Linux下利用select实现串口数据读取过程》文章介绍Linux中使用select、poll或epoll实现串口数据读取,通过I/O多路复用机制在数据到达时触发读取,避免持续轮询,示例代码展示设... 目录示例代码(使用select实现)代码解释总结在 linux 系统里,我们可以借助 select、

Linux挂载linux/Windows共享目录实现方式

《Linux挂载linux/Windows共享目录实现方式》:本文主要介绍Linux挂载linux/Windows共享目录实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录文件共享协议linux环境作为服务端(NFS)在服务器端安装 NFS创建要共享的目录修改 NFS 配

通过React实现页面的无限滚动效果

《通过React实现页面的无限滚动效果》今天我们来聊聊无限滚动这个现代Web开发中不可或缺的技术,无论你是刷微博、逛知乎还是看脚本,无限滚动都已经渗透到我们日常的浏览体验中,那么,如何优雅地实现它呢?... 目录1. 早期的解决方案2. 交叉观察者:IntersectionObserver2.1 Inter

Spring Gateway动态路由实现方案

《SpringGateway动态路由实现方案》本文主要介绍了SpringGateway动态路由实现方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随... 目录前沿何为路由RouteDefinitionRouteLocator工作流程动态路由实现尾巴前沿S