当前位置:网站首页>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 syntax and usage instructions of SQL (picture and text)
小T成长记-网络篇-1-什么是网络?
MATLAB绘制平面填充图入门详解
Win10无法连接打印机怎么办?不能使用打印机的解决方法
IPV4和IPV6是什么?
13.56MHZ刷卡芯片CI521兼容cv520/ci520支持A卡B卡MIFARE协议
Win10上帝模式干嘛的?Win10怎么开启上帝模式?
ARMv8虚拟化
Win10电脑需要安装杀毒软件吗?
Win7 encounters an error and cannot boot into the desktop normally, how to solve it?
随机推荐
Win10系统设置application identity自动提示拒绝访问怎么办
PyTorch⑦---卷积神经网络_非线性激活
【我的电赛日记(完结)---2021全国大学生电子设计竞赛全国一等奖】A题:信号失真度测量装置
用U盘怎么重装Win7系统?如何使用u盘重装系统win7?
Failed to install using npx -p @storybook/cli sb init, build a dedicated storybook by hand
define #使用
SQL的通用语法和使用说明(图文)
推开机电的大门《电路》(二):功率计算与判断
What is Win10 God Mode for?How to enable God Mode in Windows 10?
win10无法直接用照片查看器打开图片怎么办
KiCad常用快捷键
PyTorch⑥---卷积神经网络_池化层
编译error D8021 :无效的数值参数“/Wextra” cl command line error d8021 invalid numeric argument ‘/wextra‘
IPV4和IPV6是什么?
基于深度学习的配准框架
2022TI杯D题混沌信号产生实验装置
General syntax and usage instructions of SQL (picture and text)
Win10电脑不能读取U盘怎么办?不识别U盘怎么解决?
Detailed explanation of RecyclerView series article directory
cmake配置libtorch报错Failed to compute shorthash for libnvrtc.so