Python 如何在Pygame中插入滑块?

Python 如何在Pygame中插入滑块?,python,slider,pygame,Python,Slider,Pygame,我现在正在Python上进行物理模拟,用Pygame模拟一个房间里的气体云。 我的问题是,我无法在代码中插入工作滑块来更改参数的值。 我有一个运行模拟的“whilerunning”循环,当我想在其中插入一个工作滑块时,模拟停止。我不能让模拟和滑块都工作 下面是滑块的代码: #!/usr/bin/python import os import pygame, sys from pygame.locals import * # set window size width = 640 height

我现在正在Python上进行物理模拟,用Pygame模拟一个房间里的气体云。 我的问题是,我无法在代码中插入工作滑块来更改参数的值。 我有一个运行模拟的“whilerunning”循环,当我想在其中插入一个工作滑块时,模拟停止。我不能让模拟和滑块都工作

下面是滑块的代码:

#!/usr/bin/python
import os
import pygame, sys
from pygame.locals import *

# set window size
width = 640
height = 100

# initilaise pygame
pygame.init()
windowSurfaceObj = pygame.display.set_mode((width,height),1,16)
redColor = pygame.Color(255,0,0)
blackColor = pygame.Color(0,0,0)

#starting position
x = 100
pygame.draw.rect(windowSurfaceObj,redColor,Rect(x,5,10,90))
pygame.display.update(pygame.Rect(0,0,width,height))

s = 0
while s == 0:
    button = pygame.mouse.get_pressed()
    if button[0] != 0:
       pos = pygame.mouse.get_pos()
       x = pos[0]
       y = pos[1]
       a = x - 5
       if a < 0:
          a = 0
       pygame.draw.rect(windowSurfaceObj,blackColor,Rect(0,0,width,height))
       #pygame.display.update(pygame.Rect(0,0,width,height))
       pygame.draw.rect(windowSurfaceObj,redColor,Rect(a,5,10,90))
       pygame.display.update(pygame.Rect(0,0,width,height))


   # check for ESC key pressed, or pygame window closed, to quit
    for event in pygame.event.get():
       if event.type == QUIT:
          pygame.quit()
          sys.exit()
       elif event.type == KEYDOWN:
          if event.key == K_ESCAPE:
             pygame.quit()
             sys.exit()
#/usr/bin/python
导入操作系统
导入pygame,sys
从pygame.locals导入*
#设置窗口大小
宽度=640
高度=100
#初始游戏
pygame.init()
windowSurfaceObj=pygame.display.set_模式((宽度、高度),1,16)
redColor=pygame.Color(255,0,0)
blackColor=pygame.Color(0,0,0)
#起始位置
x=100
pygame.draw.rect(windowSurfaceObj,redColor,rect(x,5,10,90))
pygame.display.update(pygame.Rect(0,0,宽度,高度))
s=0
当s==0时:
button=pygame.mouse.get_pressed()
如果按钮[0]!=0:
pos=pygame.mouse.get_pos()
x=位置[0]
y=位置[1]
a=x-5
如果a<0:
a=0
pygame.draw.rect(windowSurfaceObj,黑色,rect(0,0,宽度,高度))
#pygame.display.update(pygame.Rect(0,0,宽度,高度))
pygame.draw.rect(windowSurfaceObj,redColor,rect(a,5,10,90))
pygame.display.update(pygame.Rect(0,0,宽度,高度))
#检查是否按下ESC键或pygame窗口是否关闭以退出
对于pygame.event.get()中的事件:
如果event.type==退出:
pygame.quit()
sys.exit()
elif event.type==KEYDOWN:
如果event.key==K_转义:
pygame.quit()
sys.exit()

您可以用一个矩形和一个圆形复杂地创建自己的滑块,以响应鼠标事件,但我建议您改为查看Kivy滑块。

滑块代码对我来说很好,它在我的计算机上工作。模拟代码是否在单独的while循环中?如果是这样,您必须将两个循环合并为一个,因为滑块循环永远不会中断

例如,如果滑块控制增长率,可以这样编写并将气体云绘制到
windowSurfaceObj
上。要使其正常工作,您需要将
windowSurfaceObj
放大,并为滑块和气体云设置两个单独的曲面。在这种情况下,您可以在单独的曲面上绘制,并将单独的曲面blit到
windowSurfaceObj
。您还必须执行碰撞检测以确定鼠标是否在滑块内单击

# imports and Pygame initialization here

a = 0
size_of_gas_cloud = 30

while True:

    if pygame.mouse.get_pressed()[0] != 0:
        # collision detection also needed here
        a = pygame.mouse.get_pos()[0] - 5
        if a < 0:
            a = 0

    size_of_gas_cloud += a

    pygame.draw.rect(windowSurfaceObj, blackColor, Rect(0, 0, width, height))
    pygame.draw.rect(windowSurfaceObj, redColor, Rect(a, 5, 10, 90))
    pygame.display.update()

    # Pygame event loop here
#在此处导入和Pygame初始化
a=0
气体云的大小=30
尽管如此:
如果pygame.mouse.get_按下()[0]!=0:
#这里还需要碰撞检测
a=pygame.mouse.get_pos()[0]-5
如果a<0:
a=0
气体云的大小+=a
pygame.draw.rect(windowSurfaceObj,黑色,rect(0,0,宽度,高度))
pygame.draw.rect(windowSurfaceObj、redColor、rect(a、5、10、90))
pygame.display.update()
#Pygame事件循环

我制作了一个滑块,用作背景音乐的音量控制器

enter code here
I did a slider that i used as volume controller to the background music
类滑块: definit(自身、x、y、w、h): self.circle_x=x self.volume=0 self.sliderRect=pg.Rect(x,y,w,h)

def绘制(自身,屏幕):
pg.draw.rect(屏幕,(255,255,255),self.sliderRect)
pg.draw.circle(屏幕,(255,240,255),(self.circle_x,(self.sliderRect.h/2+self.sliderRect.y)),self.sliderRect.h*1.5)
def get_卷(自身):
返回自卷
def set_音量(自身,数值):
self.volume=num
def更新_卷(自身,x):
如果xself.sliderRect.x+self.sliderRect.w:
自体积=100
其他:
self.volume=int((x-self.sliderRect.x)/float(self.sliderRect.w)*100)
U滑块上的def(自身、x、y):

如果self.on\u slider\u hold(x,y)或self.sliderRect.x我以自己的方式改进了原始代码。给你。它的构建使得您只需使用两行代码来定义循环中的滑块(一行用于初始化它(sliderInit()),另一行用于执行它(slier())

导入pygame
导入系统
从pygame.locals导入*
pygame.init()
screen=pygame.display.set_模式((19201080))
#screen=pygame.display.set_模式((0,0),pygame.FULLSCREEN)
屏幕颜色=(255255)
屏幕填充(屏幕颜色)
pygame.display.update(pygame.Rect(0,019201080))
def滑块(sx、sy、动作、宽度、高度、按钮颜色、滑块颜色、按钮顺序厚度、滑块顺序厚度、按钮宽度、滑块右侧、最大值):
如果鼠标[0]>sx和鼠标[0]sy和鼠标[1]sx+宽度按钮宽度:
mouseX=sx+宽度按钮宽度
pygame.draw.rect(屏幕,屏幕颜色,(sx,sy,宽度,高度))
pygame.draw.rect(屏幕,幻灯片颜色,(sx,sy+高度/2-幻灯片白光/2,宽度,幻灯片白光))
pygame.draw.rect(屏幕,按钮颜色,(鼠标,sy,按钮宽度,高度))
如果按钮顺序厚度!=0:
pygame.draw.rect(屏幕,(0,0,0),(鼠标,sy,按钮宽度,高度),按钮顺序厚度)
如果滑块顺序厚度!=0:
pygame.draw.rect(屏幕,(0,0,0),(sx,sy+高度/2-滑块白光/2,宽度,滑块白光),滑块顺序厚度)
pygame.display.update(pygame.Rect(sx,sy,width,height))
如果操作==“test1”:
sliderValue=(mouseX sx)/((宽度按钮宽度)/maxValue)
打印(“test1:”)
打印(滑动值)
elif操作==“test2”:
sliderValue=(mouseX sx)/((宽度按钮宽度)/maxValue)
打印(“test2:”)
打印(滑动值)
def sliderInit(sx、sy、宽度、高度、按钮颜色、滑块颜色、按钮顺序厚度、滑块顺序厚度、按钮宽度、滑块右侧
def draw(self, screen):
    pg.draw.rect(screen, (255, 255, 255), self.sliderRect)
    pg.draw.circle(screen, (255, 240, 255), (self.circle_x, (self.sliderRect.h / 2 + self.sliderRect.y)), self.sliderRect.h * 1.5)

def get_volume(self):
    return self.volume

def set_volume(self, num):
    self.volume = num

def update_volume(self, x):
    if x < self.sliderRect.x:
        self.volume = 0
    elif x > self.sliderRect.x + self.sliderRect.w:
        self.volume = 100
    else:
        self.volume = int((x - self.sliderRect.x) / float(self.sliderRect.w) * 100)

def on_slider(self, x, y):
    if self.on_slider_hold(x, y) or self.sliderRect.x <= x <= self.sliderRect.x + self.sliderRect.w and self.sliderRect.y <= y <= self.sliderRect.y + self.sliderRect.h:
        return True
    else:
        return False

def on_slider_hold(self, x, y):
    if ((x - self.circle_x) * (x - self.circle_x) + (y - (self.sliderRect.y + self.sliderRect.h / 2)) * (y - (self.sliderRect.y + self.sliderRect.h / 2)))\
           <= (self.sliderRect.h * 1.5) * (self.sliderRect.h * 1.5):
        return True
    else:
        return False

def handle_event(self, screen, x):
    if x < self.sliderRect.x:
        self.circle_x = self.sliderRect.x
    elif x > self.sliderRect.x + self.sliderRect.w:
        self.circle_x = self.sliderRect.x + self.sliderRect.w
    else:
        self.circle_x = x
    self.draw(screen)
    self.update_volume(x)
    print(self.volume)
import pygame
import sys
from pygame.locals import *

pygame.init()
screen = pygame.display.set_mode((1920, 1080))
# screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
screenColor = (255,255,255)
screen.fill(screenColor)
pygame.display.update(pygame.Rect(0,0,1920,1080))

def slider(sx,sy,action,width,height,buttonColor,sliderColor,buttonBorderThickness,sliderBorderThickness,buttonWidth,sliderBarHeight,maxValue):
    if mouse[0] > sx and mouse[0] < sx+width and mouse[1] > sy and mouse[1] < sy+height:
        if click[0] == 1:
            mouseX = mouse[0] - 5
            if mouseX < sx:
                mouseX = sx
            elif mouseX > sx+width-buttonWidth:
                mouseX = sx+width-buttonWidth
            pygame.draw.rect(screen,screenColor,(sx,sy,width,height))
            pygame.draw.rect(screen,sliderColor,(sx,sy+height/2-sliderBarHeight/2,width,sliderBarHeight))
            pygame.draw.rect(screen,buttonColor,(mouseX,sy,buttonWidth,height))
            if buttonBorderThickness != 0:
                pygame.draw.rect(screen,(0,0,0),(mouseX,sy,buttonWidth,height),buttonBorderThickness)
            if sliderBorderThickness != 0:
                pygame.draw.rect(screen,(0,0,0),(sx,sy+height/2-sliderBarHeight/2,width,sliderBarHeight),sliderBorderThickness)
            pygame.display.update(pygame.Rect(sx,sy,width,height))
            
            if action == "test1":
                sliderValue = (mouseX-sx)/((width-buttonWidth)/maxValue)
                print("test1:")
                print(sliderValue)
            elif action == "test2":
                sliderValue = (mouseX-sx)/((width-buttonWidth)/maxValue)
                print("test2:")
                print(sliderValue)

def sliderInit(sx,sy,width,height,buttonColor,sliderColor,buttonBorderThickness,sliderBorderThickness,buttonWidth,sliderBarHeight,maxValue,startValue):
        pygame.draw.rect(screen,screenColor,(sx,sy,width,height))
        pygame.draw.rect(screen,sliderColor,(sx,sy+height/2-sliderBarHeight/2,width,sliderBarHeight))
        pygame.draw.rect(screen,buttonColor,(sx+startValue*((width-buttonWidth)/maxValue),sy,buttonWidth,height))
        if buttonBorderThickness != 0:
            pygame.draw.rect(screen,(0,0,0),(sx+startValue*((width-buttonWidth)/maxValue),sy,buttonWidth,height),buttonBorderThickness)
        if sliderBorderThickness != 0:
            pygame.draw.rect(screen,(0,0,0),(sx,sy+height/2-sliderBarHeight/2,width,sliderBarHeight),sliderBorderThickness)
        pygame.display.update(pygame.Rect(sx,sy,width,height))

firstLoop = True

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
    elif event.type == KEYDOWN:
        if event.key == K_ESCAPE:
            pygame.quit()
            sys.exit()
    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()
    
    if firstLoop == True:
        sliderInit(100,50,1000,50,(255,255,255),(220,220,220),1,0,25,10,100,50)
        sliderInit(100,120,1000,50,(255,255,255),(220,220,220),1,0,25,10,100,50)
        firstLoop = False
    
    slider(100,50,"test1",1000,50,(255,255,255),(220,220,220),1,0,25,10,100)
    slider(100,120,"test2",1000,50,(255,255,255),(220,220,220),1,0,25,10,100)
import pygame
import sys
from pygame.locals import *

pygame.init()
screen = pygame.display.set_mode((1920, 1080))
# screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
screenColor = (255,255,255)
screen.fill(screenColor)
pygame.display.update(pygame.Rect(0,0,1920,1080))

def slider(sx,sy,width,height,action,
               buttonColor,buttonBorderThickness,buttonWidth,
               sliderBarColor,sliderBarBorderThickness,sliderBarHeight,
               sliderStepColor,sliderStepBorderThickness,sliderStepWidth,sliderStepHeight,sliderStepSnap,
               maxValue):
    
    if mouse[0] > sx and mouse[0] < sx+width and mouse[1] > sy and mouse[1] < sy+height:
        if click[0] == 1:
            mouseX = mouse[0] - 5
            if mouseX < sx+sliderStepSnap:
                mouseX = sx
            
            elif mouseX > (round(sx+10*((width)/100))-sliderStepSnap) and mouseX < (round(sx+10*((width)/100))+sliderStepSnap):
                mouseX = round(sx+10*((width)/100)-buttonWidth/2)
            elif mouseX > (round(sx+20*((width)/100))-sliderStepSnap) and mouseX < (round(sx+20*((width)/100))+sliderStepSnap):
                mouseX = round(sx+20*((width)/100)-buttonWidth/2) 
            elif mouseX > (round(sx+30*((width)/100))-sliderStepSnap) and mouseX < (round(sx+30*((width)/100))+sliderStepSnap):
                mouseX = round(sx+30*((width)/100)-buttonWidth/2)
            elif mouseX > (round(sx+40*((width)/100))-sliderStepSnap) and mouseX < (round(sx+40*((width)/100))+sliderStepSnap):
                mouseX = round(sx+40*((width)/100)-buttonWidth/2)
            elif mouseX > (round(sx+50*((width)/100))-sliderStepSnap) and mouseX < (round(sx+50*((width)/100))+sliderStepSnap):
                mouseX = round(sx+50*((width)/100)-buttonWidth/2)
            elif mouseX > (round(sx+60*((width)/100))-sliderStepSnap) and mouseX < (round(sx+60*((width)/100))+sliderStepSnap):
                mouseX = round(sx+60*((width)/100)-buttonWidth/2)
            elif mouseX > (round(sx+70*((width)/100))-sliderStepSnap) and mouseX < (round(sx+70*((width)/100))+sliderStepSnap):
                mouseX = round(sx+70*((width)/100)-buttonWidth/2)
            elif mouseX > (round(sx+80*((width)/100))-sliderStepSnap) and mouseX < (round(sx+80*((width)/100))+sliderStepSnap):
                mouseX = round(sx+80*((width)/100)-buttonWidth/2)
            elif mouseX > (round(sx+90*((width)/100))-sliderStepSnap) and mouseX < (round(sx+90*((width)/100))+sliderStepSnap):
                mouseX = round(sx+90*((width)/100)-buttonWidth/2)
            
            elif mouseX > sx+width-buttonWidth-sliderStepSnap:
                mouseX = sx+width-buttonWidth
            
            pygame.draw.rect(screen,screenColor,(sx,sy,width,height))
            
            for i in range(1,10):
                pygame.draw.rect(screen,sliderStepColor,(round(sx+i*10*((width)/100)),sy+(height-sliderStepHeight)/2,sliderStepWidth,sliderStepHeight))
                if sliderStepBorderThickness != 0:
                    pygame.draw.rect(screen,(0,0,0),(round(sx+i*10*((width)/100)),sy+(height-sliderStepHeight)/2,sliderStepWidth,sliderStepHeight),sliderStepBorderThickness)
            pygame.draw.rect(screen,sliderBarColor,(sx,sy+height/2-sliderBarHeight/2,width,sliderBarHeight))
            
            pygame.draw.rect(screen,buttonColor,(mouseX,sy,buttonWidth,height))
            if buttonBorderThickness != 0:
                pygame.draw.rect(screen,(0,0,0),(mouseX,sy,buttonWidth,height),buttonBorderThickness)
            if sliderBarBorderThickness != 0:
                pygame.draw.rect(screen,(0,0,0),(sx,sy+height/2-sliderBarHeight/2,width,sliderBarHeight),sliderBarBorderThickness)
            pygame.display.update(pygame.Rect(sx,sy,width,height))
            
            if action == "test1":
                sliderValue = (mouseX-sx)/((width-buttonWidth)/maxValue)
                print("test1:")
                print(sliderValue)
            elif action == "test2":
                sliderValue = (mouseX-sx)/((width-buttonWidth)/maxValue)
                print("test2:")
                print(sliderValue)

def sliderInit(sx,sy,width,height,
               buttonColor,buttonBorderThickness,buttonWidth,
               sliderBarColor,sliderBarBorderThickness,sliderBarHeight,
               sliderStepColor,sliderStepBorderThickness,sliderStepWidth,sliderStepHeight,
               maxValue,startValue):
        
        pygame.draw.rect(screen,screenColor,(sx,sy,width,height))
        
        for i in range(1,10):
            pygame.draw.rect(screen,sliderStepColor,(round(sx+i*10*((width)/100)),sy+(height-sliderStepHeight)/2,sliderStepWidth,sliderStepHeight))
            if sliderStepBorderThickness != 0:
                pygame.draw.rect(screen,(0,0,0),(round(sx+i*10*((width)/100)),sy+(height-sliderStepHeight)/2,sliderStepWidth,sliderStepHeight),sliderStepBorderThickness)
        pygame.draw.rect(screen,sliderBarColor,(sx,sy+height/2-sliderBarHeight/2,width,sliderBarHeight))
    
        pygame.draw.rect(screen,buttonColor,(sx+startValue*((width-buttonWidth)/maxValue),sy,buttonWidth,height))
        if buttonBorderThickness != 0:
            pygame.draw.rect(screen,(0,0,0),(sx+startValue*((width-buttonWidth)/maxValue),sy,buttonWidth,height),buttonBorderThickness)
        if sliderBarBorderThickness != 0:
            pygame.draw.rect(screen,(0,0,0),(sx,sy+height/2-sliderBarHeight/2,width,sliderBarHeight),sliderBorderThickness)
        pygame.display.update(pygame.Rect(sx,sy,width,height))

firstLoop = True

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                turnOffMotors()
                pygame.quit()
                sys.exit()
    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()
    
    if firstLoop == True:
        sliderInit(100,50,1000,50,
               (255,255,255),1,25,
               (220,220,220),0,10,
               (0,0,0),0,5,40,
               100,50)
        sliderInit(100,120,1000,50,
               (255,255,255),1,25,
               (220,220,220),0,10,
               (0,0,0),0,5,40,
               100,50)
        firstLoop = False
    
    slider(100,50,1000,50,"test1",
           (255,255,255),1,25,
           (220,220,220),0,10,
           (0,0,0),0,5,40,30,
           100)
    slider(100,120,1000,50,"test2",
           (255,255,255),1,25,
           (220,220,220),0,10,
           (0,0,0),0,5,40,30,
           100)