首页 > 解决方案 > 加载栏进行时如何在背景中显示图像?

问题描述

所以我做了一个加载栏并设法让它成功工作。但是,我想在进度条正在进行时在背景中显示图像,并且每当我尝试时我的动作都会化为乌有。

我试图添加一个新行,它会不断地“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)

标签: pythonpython-3.xpygameloading

解决方案


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

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

blit()不仅用于显示背景,还用于显示玩家的图像、敌人的图像——它们可以显示在屏幕上的任何位置。


推荐阅读