如何在加载栏进行时在背景中显示图像?

2024-10-01 17:32:52 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我做了一个加载条,并成功地让它工作。但是,我希望在进度条进行时在背景中显示一个图像,并且每当我尝试时,我的操作都会付诸东流

我试图添加一个新的行,它会不断“blit”的形象。 screen.blit(loadingimg),但它最终给出了以下错误:

DS.blit(loadingimg)
TypeError: function missing required argument 'dest' (pos 2)
smallfont = pygame.font.SysFont("comicsansms",25)
DS = pygame.display.set_mode((W, H))

def text_objects(text, color, size):
    if size == "small":
        textSurface = smallfont.render(text, True, color)

    return textSurface, textSurface.get_rect()

def loading(progress):
    if progress < 100:
        text = smallfont.render("Loading: " + str(int(progress)) + "%", True, green)
    else:
        text = smallfont.render("Loading: " + str(100) + "%", True, green)

    DS.blit(text, [453, 273])

def message_to_screen(msh, color, y_displace = 0, size = "small"):
    textSurf, textRect = text_objects(msg, color, size)
    textRect.center = HW, HH + y_displace
    DS.blit(textSurf, textRect)

while (progress/2) < 100:
    event_handler()
    DS.fill(WHITE)
    DS.blit(loadingimg)
    time_count = (random.randint(1,1))
    increase = random.randint(1,20)
    progress += increase
    pygame.draw.rect(DS, green, [423, 223, 204, 49])
    pygame.draw.rect(DS, BLACK, [424, 224, 202, 47])
    if (progress/2) > 100:
        pygame.draw.rect(DS, green, [425, 225, 200, 45])
    else:
        pygame.draw.rect(DS, green, [425, 225, progress, 45])
    loading(progress/2)
    pygame.display.flip()

    time.sleep(time_count)

应该发生的是一个加载条出现,当进度条加载时,背景中有一个图像。当进度条达到100%后,它将继续进行下一个操作,但在这种情况下,我只希望它们在达到100%后都消失

我的实际输出就是这个错误:

DS.blit(loadingimg)
TypeError: function missing required argument 'dest' (pos 2)

Tags: 进度条textrectsizeifdefdsgreen
1条回答
网友
1楼 · 发布于 2024-10-01 17:32:52

有关blit(),请参阅文档。它还需要位置-blit(image, (x,y))blit(image, rect)

DS.blit(loadingimg, (0, 0))

blit()不仅用来显示背景,还可以显示玩家的图像,敌人的图像-它们可以显示在屏幕上的任何地方

相关问题 更多 >

    热门问题