当前位置:网站首页>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边栏推荐
- Fast advanced TypeScript
- LeetCode2 电话号码的字母组合
- FP6195耐压60V电流降压3.3V5V模块供电方案
- PyTorch②---transforms结构及用法、常见的Transforms
- A clean start Windows 7?How to load only the basic service start Windows 7 system
- DP4344兼容CS4344-DA转换器
- FP7128内置MOS降压恒流调光深度0.01%高辉共阳调光方案
- 用U盘怎么重装Win7系统?如何使用u盘重装系统win7?
- TypeScript 快速进阶
- FP7126降压恒流65536级高辉无频闪调光共阳极舞台灯RGB驱动方案
猜你喜欢

tensorflow实战之手写体识别

win10 system update error code 0x80244022 how to do

Win11 computer off for a period of time without operating network how to solve

Win7 encounters an error and cannot boot into the desktop normally, how to solve it?

FP7195大功率零压差全程无频闪调光DC-DC恒流芯片(兼容调光器:PWM调光,无极调光,0/1-10V调光)

语言模型(NNLM)

How to set the win10 taskbar does not merge icons

win10任务栏不合并图标如何设置

关于c语言的调试技巧

Win7怎么干净启动?如何只加载基本服务启动Win7系统
随机推荐
Tensorflow常用函数
PyTorch(13)---优化器_随机梯度下降法
使用预训练语言模型进行文本生成的常用微调策略
jest测试,组件测试
FP5207电池升压 5V9V12V24V36V42V大功率方案
How to update Win11 sound card driver?Win11 sound card driver update method
Policy Evaluation收敛性、炼丹与数学家
FP7128内置MOS降压恒流调光深度0.01%高辉共阳调光方案
基于无监督医学图像配准论文(1)
Do Windows 10 computers need antivirus software installed?
关系代数、SQL与逻辑式语言
PyTorch⑨---卷积神经网络_线性层
Win11系统找不到dll文件怎么修复
图像配置分类及名词解释
How to add a one-key shutdown option to the right-click menu in Windows 11
2021-10-14
什么是外生变量和内生变量
日常-笔记
What should I do if I install a solid-state drive in Win10 and still have obvious lags?
PHY6222蓝牙5.2支持MESH组网M0内核超低功耗