当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
PyTorch①---加载数据、tensorboard的使用
Mysql connection error solution
用U盘怎么重装Win7系统?如何使用u盘重装系统win7?
vscode镜像
Please make sure you have the correct access rights and the repository exists. Problem solved
PyTorch(15)---模型保存和加载
Bash shell位置参数
2020-02-06-快速搭建个人博客
内存申请(malloc)和释放(free)之上篇
FP7122降压恒流内置MOS耐压100V共正极阳极PWM调光方案原理图
Win10系统设置application identity自动提示拒绝访问怎么办
CI24R1小模块2.4G收发模块无线通信低成本兼容si24r1/XN297超低功耗
Win10上帝模式干嘛的?Win10怎么开启上帝模式?
使用 腾讯云搭建一个个人博客
PyTorch⑦---卷积神经网络_非线性激活
Win11没有本地用户和组怎么解决
arm ldr系列指令
蓝牙温度检测系统(基于BT08-B蓝牙模块)
boost库智能指针
Mapreduce环境详细搭建和案例实现