当前位置:网站首页>Article pygame drag the implementation of the method
Article pygame drag the implementation of the method
2022-08-02 15:26:00 【Tianshan has no longevity tea】
The effect is as follows:
Take the volume adjustment as an example, first draw a horizontal line and a solid circle, the specific position and size can be specified by yourself:
# filled circle abscissa positionVPOS = 300# Draw a horizontal straight line, the parameters are (surface, color, start_pos, end_pos, width)pygame.draw.line(self.screen, self.BLUE, (150, 900), (450, 900), 5)# Draw a solid circle as a button, the parameters are (surface, color, center, radius, width)volume_button = pygame.draw.circle(self.screen, self.GREEN, (VPOS, 900), 10, width=0)
In order to make the drag feel better, we can drag the position of the circle as long as the button is pressed until it is lifted.To this end, we set the variable value volume_state. When the value is 0, the filled circle will not change position with the mouse; when the value is 1, it will change with the mouse.Under normal conditions, the value is 0. When the mouse is pressed on the solid circle, we set the value to 1, and the value changes back to 0 until the mouse is lifted.
# default is not adjustablevolume_state = 0# pygame loopwhile running:# When it is detected that the mouse is pressed at the button, the button position and volume can be adjustedif pygame.mouse.get_pressed()[0]:if volume_button.collidepoint(pos):volume_state = 1# When it is detected that the mouse is lifted, the adjustment is stopped, and it cannot be changed after the next mouse pressfor event in pygame.event.get():if event.type == pygame.MOUSEBUTTONUP and volume_state:volume_state = 0# Get the current position of the mousepos = pygame.mouse.get_pos()# The specific process of adjusting the volumeif volume_state:# The position of the circle is the same as the position of the horizontal coordinate of the mouseVPOS = pos[0]# Limit the position of the button center to the horizontal lineif VPOS > 450:VPOS = 450elif VPOS < 150:VPOS = 150# Actual volume valuevolume = (VPOS - 150) / 300.0
边栏推荐
- PyTorch③---torchvision中数据集的使用
- DP1332E内置c8051的mcu内核NFC刷卡芯片国产兼容NXP
- FP6296锂电池升压 5V9V12V内置 MOS 大功率方案原理图
- BLE蓝牙5.2-PHY6222系统级芯片(SoC)智能手表/手环
- Makefile容易犯错的语法
- 推开机电的大门《电路》(三):说说不一样的电阻与电导
- Win10上帝模式干嘛的?Win10怎么开启上帝模式?
- Actual combat Meituan Nuxt +Vue family bucket, server-side rendering, mailbox verification, passport authentication service, map API reference, mongodb, redis and other technical points
- KiCad常用快捷键
- 13.56MHZ刷卡芯片CI521兼容cv520/ci520支持A卡B卡MIFARE协议
猜你喜欢
Win10 computer can't read U disk?Don't recognize U disk how to solve?
总结计算机网络超全面试题
Win7 encounters an error and cannot boot into the desktop normally, how to solve it?
What should I do if Windows 10 cannot connect to the printer?Solutions for not using the printer
FP6293电池升压5V-12V大电流2APWM模式升压方案
FP7195大功率零压差全程无频闪调光DC-DC恒流芯片(兼容调光器:PWM调光,无极调光,0/1-10V调光)
Binder ServiceManager解析
source /build/envsetup.sh和lunch)
推开机电的大门《电路》(二):功率计算与判断
【我的电赛日记(三)】STM32学习笔记与要点总结
随机推荐
Please make sure you have the correct access rights and the repository exists. Problem solved
Win11系统找不到dll文件怎么修复
LORA芯片ASR6601支持M4内核的远距离传输芯片
golang之GMP调度模型
What should I do if Windows 10 cannot connect to the printer?Solutions for not using the printer
BLE蓝牙5.2-PHY6222系统级芯片(SoC)智能手表/手环
FP7128内置MOS降压恒流调光深度0.01%高辉共阳调光方案
【我的电赛日记(三)】STM32学习笔记与要点总结
win10无法直接用照片查看器打开图片怎么办
Makefile容易犯错的语法
MATLAB绘制平面填充图入门详解
STM32LL库使用——SPI通信
DP1332E内置c8051的mcu内核NFC刷卡芯片国产兼容NXP
Win7 encounters an error and cannot boot into the desktop normally, how to solve it?
TCP三次握手、四次挥手
GICv3/v4-软件概述
FP5207电池升压 5V9V12V24V36V42V大功率方案
PyTorch⑥---卷积神经网络_池化层
FP7195转模拟调光技术解决智能家居调光频闪和电感噪音的原理
PyTorch②---transforms结构及用法