当前位置:网站首页>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边栏推荐
- Daily - Notes
- ASR6601牛羊定位器芯片GPS国内首颗支持LoRa的LPWAN SoC
- Win11系统找不到dll文件怎么修复
- HAL框架
- 使用libcurl将Opencv Mat的图像上传到文件服务器,基于post请求和ftp协议两种方法
- 【深度学习中的损失函数整理与总结】
- 编译error D8021 :无效的数值参数“/Wextra” cl command line error d8021 invalid numeric argument ‘/wextra‘
- flink+sklearn——使用jpmml实现flink上的机器学习模型部署
- FP7126降压恒流65536级高辉无频闪调光共阳极舞台灯RGB驱动方案
- Binder机制(下篇)
猜你喜欢

win10系统更新错误代码0x80244022怎么办

Makefile容易犯错的语法

What should I do if Windows 10 cannot connect to the printer?Solutions for not using the printer

基于无监督医学图像配准论文(1)

【我的电赛日记(三)】STM32学习笔记与要点总结

推开机电的大门《电路》(三):说说不一样的电阻与电导

SQL的通用语法和使用说明(图文)

Win10 cannot directly use photo viewer to open the picture

pygame绘制弧线

Win11 system cannot find dll file how to fix
随机推荐
FP7122降压恒流内置MOS耐压100V共正极阳极PWM调光方案原理图
STM32F1和F4的区别
BLE蓝牙5.2-PHY6222系统级芯片(SoC)智能手表/手环
Publish module to NPM should be how to operate?Solutions to problems and mistake
Win11 system cannot find dll file how to fix
Win10上帝模式干嘛的?Win10怎么开启上帝模式?
关于c语言的调试技巧
用U盘怎么重装Win7系统?如何使用u盘重装系统win7?
2020-02-06-快速搭建个人博客
Win11没有本地用户和组怎么解决
CMAKE
2.4G无线小模块CI24R1超低成本
DP1101兼容CC1101是SUB1GHz无线收发芯片应用于智能家居
win10无法直接用照片查看器打开图片怎么办
刷卡芯片CI520可直接PIN对PIN替换CV520支持SPI通讯接口
网络安全抓包
vscode镜像
Win10无法连接打印机怎么办?不能使用打印机的解决方法
pygame绘制弧线
Win10电脑不能读取U盘怎么办?不识别U盘怎么解决?