当前位置:网站首页>pygame拖动条的实现方法
pygame拖动条的实现方法
2022-08-02 14:09:00 【天山没有长寿茶】
实现效果如下:
我们以音量的调整为例,首先绘制水平直线与实心圆,具体位置、大小可自己指定:
# 实心圆横坐标位置
VPOS = 300
# 绘制水平直线,参数依次为(surface, color, start_pos, end_pos, width)
pygame.draw.line(self.screen, self.BLUE, (150, 900), (450, 900), 5)
# 绘制实心圆作为按钮,参数依次为(surface, color, center, radius, width)
volume_button = pygame.draw.circle(self.screen, self.GREEN, (VPOS, 900), 10, width=0)
为了拖动手感更好,我们只要按下按键后直到抬起期间均可拖动圆的位置。为此我们设置变量值volume_state,当该值为0时,实心圆不会随着鼠标改变位置;而该值为1时,则会随着鼠标改变。一般状态下该值为0,当鼠标在实心圆上按下时刻我们就令该值为1,直到鼠标抬起,该值变回0。
# 默认为不可调整状态
volume_state = 0
# pygame循环
while running:
# 当检测到鼠标在按钮处按下,即可调整按钮位置与音量
if pygame.mouse.get_pressed()[0]:
if volume_button.collidepoint(pos):
volume_state = 1
# 当检测到鼠标抬起,则停止调整,在下一次鼠标按下后不可再变化
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONUP and volume_state:
volume_state = 0
# 获取鼠标当前位置
pos = pygame.mouse.get_pos()
# 调整音量具体过程
if volume_state:
# 圆的位置与鼠标横坐标位置一致
VPOS = pos[0]
# 限制按键圆心位置在水平直线上
if VPOS > 450:
VPOS = 450
elif VPOS < 150:
VPOS = 150
# 实际音量值
volume = (VPOS - 150) / 300.0
边栏推荐
- 13.56MHZ刷卡芯片CI521兼容cv520/ci520支持A卡B卡MIFARE协议
- Win10安装了固态硬盘还是有明显卡顿怎么办?
- PyTorch(12)---损失函数和反向传播
- Win11 system cannot find dll file how to fix
- define #使用
- win10 system update error code 0x80244022 how to do
- ASR6601牛羊定位器芯片GPS国内首颗支持LoRa的LPWAN SoC
- 2021-10-14
- PyTorch⑤---卷积神经网络_卷积层
- 使用libcurl将Opencv Mat的图像上传到文件服务器,基于post请求和ftp协议两种方法
猜你喜欢
Binder机制(下篇)
FP7122降压恒流内置MOS耐压100V共正极阳极PWM调光方案原理图
Win7 encounters an error and cannot boot into the desktop normally, how to solve it?
机器学习和深度学习中的梯度下降及其类型
GPT2计算流程详解
General syntax and usage instructions of SQL (picture and text)
Win10系统设置application identity自动提示拒绝访问怎么办
PyTorch⑤---卷积神经网络_卷积层
为android系统添加产品的过程
基于51单片机和物联网的智能家居系统(ESP8266物联网模块)
随机推荐
Win7 encounters an error and cannot boot into the desktop normally, how to solve it?
The overlapping effect of the two surfaceviews is similar to the video and handout practice in the live effect
使用预训练语言模型进行文本生成的常用微调策略
PHY6222蓝牙5.2支持MESH组网M0内核超低功耗
LORA芯片ASR6601支持M4内核的远距离传输芯片
让深度学习歇一会吧
针对多轮推理分类问题的软标签构造方法
【系统设计与实现】基于flink的分心驾驶预测与数据分析系统
TypeScript 快速进阶
tensorflow实战之手写体识别
2020-02-06-快速搭建个人博客
使用libcurl将Opencv Mat的图像上传到文件服务器,基于post请求和ftp协议两种方法
CS4398音频解码替代芯片DP4398完全兼容DAC解码
刷卡芯片CI520可直接PIN对PIN替换CV520支持SPI通讯接口
FP6195耐压60V电流降压3.3V5V模块供电方案
Win7遇到错误无法正常开机进桌面怎么解决?
基于51单片机和物联网的智能家居系统(ESP8266物联网模块)
【我的电赛日记(二)】ADF4351锁相环模块
Failed to install using npx -p @storybook/cli sb init, build a dedicated storybook by hand
The problem that UIWindow's makeKeyAndVisible does not call viewDidLoad of rootviewController