Thanos
Thanos

Reputation: 386

How would I display an image in the background while the loading bar progresses?

So i made a loading bar and managed to get it working successfully. However, I want to display an image in the background while the progress bar is progressing and my actions have gone to dust whenever I try.

I tried to add a new line which would constantly "blit" the image. screen.blit(loadingimg) but it ended up giving this error:

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)

What's supposed to happen is that a loading bar appears and while the progress bar is loading, there's an image in the background. After the progress bar reaches 100%, it will move on to the next thing but in this case, I just want both of them to disappear after reaching 100%

My actual output is just this error:

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

Upvotes: 0

Views: 161

Answers (1)

furas
furas

Reputation: 142671

See documentation for blit(). It needs also position - blit(image, (x,y)) or blit(image, rect)

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

blit() is used to display not only background but also player's image, enemies' images - and they can be displayed in any place on screen.

Upvotes: 1

Related Questions