当前位置:网站首页>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
边栏推荐
- General code for pytorch model to libtorch and onnx format
- CI24R1小模块2.4G收发模块无线通信低成本兼容si24r1/XN297超低功耗
- PyTorch②---transforms结构及用法、常见的Transforms
- IPV4和IPV6是什么?
- 2.4G无线小模块CI24R1超低成本
- PHY6222蓝牙5.2支持MESH组网M0内核超低功耗
- 13.56MHZ刷卡芯片CI521兼容cv520/ci520支持A卡B卡MIFARE协议
- 图像配置分类及名词解释
- Letter combination of LeetCode2 phone number
- ARMv8虚拟化
猜你喜欢
Win10电脑不能读取U盘怎么办?不识别U盘怎么解决?
Win10 Settings screen out from lack of sleep?Win10 set the method that never sleep
【我的电赛日记(三)】STM32学习笔记与要点总结
STM32LL库使用——SPI通信
FP7195转模拟调光技术解决智能家居调光频闪和电感噪音的原理
Win10系统设置application identity自动提示拒绝访问怎么办
Configure clangd for vscode
ARMv8虚拟化
FP6195耐压60V电流降压3.3V5V模块供电方案
Win10安装了固态硬盘还是有明显卡顿怎么办?
随机推荐
IPV4和IPV6是什么?
神经网络的设计过程
Mysql connection error solution
镜像法求解接地导体空腔电势分布问题
Fast advanced TypeScript
【我的电赛日记(一)】HMI USART串口屏
Actual combat Meituan Nuxt +Vue family bucket, server-side rendering, mailbox verification, passport authentication service, map API reference, mongodb, redis and other technical points
2021-10-14
轻量化AlphaPose
mysql的索引结构为什么选用B+树?
实战美团Nuxt +Vue全家桶,服务端渲染,邮箱验证,passport鉴权服务,地图API引用,mongodb,redis等技术点
Letter combination of LeetCode2 phone number
推开机电的大门《电路》(二):功率计算与判断
How to set the win10 taskbar does not merge icons
STM32LL库使用——SPI通信
Redis的线程模型
5. Use RecyclerView to elegantly achieve waterfall effect
FP7128内置MOS降压恒流调光深度0.01%高辉共阳调光方案
Win7遇到错误无法正常开机进桌面怎么解决?
基于无监督医学图像配准论文(1)