当前位置:网站首页>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
边栏推荐
猜你喜欢
基于无监督医学图像配准论文(1)
Mapreduce环境详细搭建和案例实现
【我的电赛日记(三)】STM32学习笔记与要点总结
二叉树遍历之后序遍历(非递归、递归)入门详解
Win10 Settings screen out from lack of sleep?Win10 set the method that never sleep
flink+sklearn——使用jpmml实现flink上的机器学习模型部署
【使用Pytorch实现VGG16网络模型】
PHY6222蓝牙5.2支持MESH组网M0内核超低功耗
FP7195转模拟恒流调光芯片在机器视觉光源的应用优势
2020-02-06-快速搭建个人博客
随机推荐
define #使用
【我的电赛日记(二)】ADF4351锁相环模块
A clean start Windows 7?How to load only the basic service start Windows 7 system
LORA芯片ASR6505无线远距离传输8位MCU
FP6293电池升压5V-12V大电流2APWM模式升压方案
推开机电的大门《电路》(一):电压,电流,参考方向
Binder机制(中篇)
2.4G无线小模块CI24R1超低成本
Win11没有本地用户和组怎么解决
FP7128内置MOS降压恒流调光深度0.01%高辉共阳调光方案
小T成长记-网络篇-1-什么是网络?
arm ldr系列指令
TCP三次握手、四次挥手
General syntax and usage instructions of SQL (picture and text)
FP6296锂电池升压 5V9V12V内置 MOS 大功率方案原理图
Tensorflow常用函数
PyTorch⑤---卷积神经网络_卷积层
Win10电脑需要安装杀毒软件吗?
jest测试,组件测试
Mysql的锁