问题描述
我目前在 pygame 上开发一款游戏.我想创建一个带有加载栏的加载窗口,如软件 GIMP,但我真的不知道如何优化它,...根本
I currently work on a game on pygame.I want to create a loading window with a loading bar like the software GIMP but I really don't know how I can optimize that,... At all
所以这是我代码的一部分,显然,它运行得不是很好.你能帮我吗?
So this is a part of my code, obviously, it's not working very well.Can you help me?
...
screen = pygame.display.set_mode((640, 480), pygame.NOFRAME)
where = (120,360)
While in progress = true:
If 1/a < 0.25
screen.blit(Load0, where)
If 1/a < 0.50
screen.blit(Load1,where)
#... ( it's the same for <0.75 and = 1 )
pygame.display.update()
game()
但它使继电器坏加载栏...你能帮我让它更优化、更流畅吗?
PS:我需要加载 350 个元素
But it's make a rellay bad loading bar...Can you help me to make it really more optimized and smoother?
PS : I got 350 elements to load
推荐答案
简化事物,用 2 个矩形绘制一个条形.
Simplify things, and draw a bar by 2 rectangles.
创建一个绘制条形的函数.该函数绘制一个薄的外部矩形和填充的内部矩形.内部矩形的长度取决于进度.progress
是 [0, 1] 范围内的值.如果为 0,则不绘制内部条形.如果为 1,则内条完成:
Create a function which draws a bar. The function draws a thin outer rectangle and filled inner rectangle. The length of the inner rectangle depends on the progress. progress
is a value in range [0, 1]. If it is 0, then no inner bar is draw. If it is 1, then the inner bar is complete:
def DrawBar(pos, size, borderC, barC, progress):
pygame.draw.rect(screen, borderC, (*pos, *size), 1)
innerPos = (pos[0]+3, pos[1]+3)
innerSize = ((size[0]-6) * progress, size[1]-6)
pygame.draw.rect(screen, barC, (*innerPos, *innerSize))
定义条的位置和颜色参数:
Define the position and color parameters for the bar:
barPos = (120, 360)
barSize = (200, 20)
borderColor = (0, 0, 0)
barColor = (0, 128, 0)
定义最大项目数:
max_a = 350
绘制条形图时,当前进度为a/max_a
:
When you draw the bar, then the current progress is a/max_a
:
DrawBar(barPos, barSize, borderColor, barColor, a/max_a)
这篇关于如何在 pygame 中显示平滑的加载栏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!