当前位置:网站首页>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边栏推荐
猜你喜欢
随机推荐
基于51单片机和物联网的智能家居系统(ESP8266物联网模块)
DP1332E内置c8051的mcu内核NFC刷卡芯片国产兼容NXP
发布模块到npm应该怎么操作?及错误问题解决方案
win10 system update error code 0x80244022 how to do
图像配置分类及名词解释
日常-笔记
2021-10-14
FP7195转模拟恒流调光芯片在机器视觉光源的应用优势
Daily - Notes
FP7122降压恒流内置MOS耐压100V共正极阳极PWM调光方案原理图
基于无监督医学图像配准论文(1)
Golang 垃圾回收机制详解
PyTorch②---transforms结构及用法
CS4398音频解码替代芯片DP4398完全兼容DAC解码
Actual combat Meituan Nuxt +Vue family bucket, server-side rendering, mailbox verification, passport authentication service, map API reference, mongodb, redis and other technical points
使用 腾讯云搭建一个个人博客
基于最小二乘法的线性回归分析方程中系数的估计
Binder机制(下篇)
How to set the win10 taskbar does not merge icons
MATLAB绘制平面填充图入门详解









