问题
i'm trying to make a loading bar but this is what came up HELP MEE
from time import sleep
def fill_rect():
global fill_r
global fill_v
global rect_x
global speed_fill
fill(fill_r,fill_v,0)
rect(width/2 - 100, height/2 - 12.5,rect_x,25)
if rect_x <= 200 - speed_fill :
rect_x = rect_x + speed_fill
fill_r = fill_r + 5
fill_v = fill_v -2
speed_fill = speed_fill + 1
def setup():
global fill_r
global fill_v
global rect_x
global speed_fill
background(0,100,255)
size(500,500)
speed_fill = 1
fill(0)
rect(width/2 - 100, height/2 - 12.5,200,25)
rect_x = 1
fill_r = 25
fill_v = 100
def draw():
global fill_r
global fill_v
global rect_x
fill_rect()
the loading bar either doesn't go all the way the sleep import is useless in this code if i change the parameters of the if statement in the fill_rect() function the loading bar goes beyond the limit
回答1:
Use min to limit rect_x
to the end of the bar:
rect_x = min(200, rect_x + speed_fill)
The bar fills up rapidly. The issue is, that the acceleration is to strong:
speed_fill = speed_fill + 1
Decrease the acceleration (e.g. 0.1):
def fill_rect():
global fill_r, fill_v, rect_x, speed_fill
fill(fill_r, fill_v, 0)
rect(width/2 - 100, height/2 - 12.5, rect_x, 25)
if rect_x <= 200:
rect_x = min(200, rect_x + speed_fill)
speed_fill += 0.1
fill_r += 5
fill_v -= 2
Note, the frame rate can be controlled by frameRate().
回答2:
i found a way to make it stop at the end of the rectangle with any acceleration
def fill_rect():
global fill_r
global fill_v
global rect_x
global speed_fill
fill(fill_r,fill_v,0)
rect(width/2 - 100, height/2 - 12.5,rect_x,25)
if rect_x <= 200 :
if rect_x > 200 - speed_fill:
rect_x = 200 - speed_fill
rect_x = rect_x + speed_fill
fill_r = fill_r + 5
fill_v = fill_v - 2
speed_fill = speed_fill + 1
来源:https://stackoverflow.com/questions/60082221/how-to-fix-loading-bar-stopping-before-the-end