1

So when I and my acquaintance were working in pygame, we came across a problem:Every time the progress bar reached over 100, it would crash and show us an error. The error code reads:

 "Traceback (most recent call last):
  File "C:/Users/Tip/Documents/loading bar/load.py", line 150, in <module>
    loading(progress/2)
  File "C:/Users/Tip/Documents/loading bar/load.py", line 60, in loading
    screen.blit(text, (300, 100))
TypeError: argument 1 must be pygame.Surface, not str
libpng warning: iCCP: known incorrect sRGB profile". 

And to help, here is the code:

import pygame
import random

pygame.init()

progress = 0

into = True

black = [0, 0, 0]
white = [255, 255, 255]
intoWhite = [249, 244, 244]
green = [0, 255, 0]

screenWidth = 600
screenHeigth = 450
size = [screenWidth, screenHeigth]

font = pygame.font.SysFont("Comic Sans MS", 25)

intoPic = pygame.image.load("images/Can You Surive Pic.png")
cory = "audio/Cory in the House - Opening Sequence.mp3"
coryPic = pygame.image.load("images/CoryBaxterFull.png")
coryMusicBool = False
coryPicBool = False
basic = "audio/basic song.mp3"
shrekPicBool = False
shrekMusicBool = False
shrek = "audio/Shrek's Fairytale Freestyle.mp3"
shrekPic = pygame.image.load("images/shrek.png")

clock = pygame.time.Clock()
clock.tick(10)

count = 0
escPressed = False

screen = pygame.display.set_mode(size)
pygame.display.set_caption('Loading...')

escMessageText = "Press Esc to cancel"
escMessage = font.render(escMessageText, False, white)
escMessageText_2 = "You Can't Leave"
escMessage_2 = font.render(escMessageText_2, False, white)


def textobjecte(text, color, size):
    if size == "small":
        textsuraface = font.render(text, True, color)

        return textsuraface, textsuraface.get_rect()


def loading(progress):
    text = ""

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

    screen.blit(text, (300, 100))


def message_to_screen(msg, color, y_displace, size="small"):
    textsurf, textrect = textobjecte(msg, color, size)
    textrect.center = (screenWidth/2), (screenHeigth/2) + y_displace

    screen.blit(textsurf, textrect)


def playcory():
    pygame.mixer.music.load(cory)
    pygame.mixer.music.play(-1)


def playshrek():
    pygame.mixer.music.load(shrek)
    pygame.mixer.music.play(-1)


def basicsong():
    pygame.mixer.music.load(basic)
    pygame.mixer.music.play(-1)


basicsong()

while into == True:
    pygame.event.pump()
    screen.fill(intoWhite)
    screen.blit(intoPic, (75, 0))
    intoKey = pygame.key.get_pressed()
    pygame.display.flip()
    if intoKey[pygame.K_SPACE]:
        into = False


while progress/2 < 100:
    timeCount = random.randint(1, 10000)
    increase = random.randint(1, 10)
    progress += increase/10000
    screen.fill(black)
    pygame.event.pump()
    pygame.draw.rect(screen, white, [300, 50, 200, 50])
    pygame.draw.rect(screen, black, [301, 51, 198, 48])

    if (progress/2) > 100:
        pygame.draw.rect(screen, white, [302, 52, 196, 46])
    else:
        pygame.draw.rect(screen, white, [302, 52, progress, 46])

    if coryMusicBool == True:
        pygame.mixer.music.stop()
        playcory()
        coryMusicBool = False
    if coryPicBool== True:
        screen.blit(coryPic, (0, 0))

    if shrekMusicBool == True:
        pygame.mixer.music.stop()
        playshrek()
        shrekMusicBool = False
    if shrekPicBool == True:
        screen.blit(shrekPic, (100, 0))

    if escPressed == False:
        screen.blit(escMessage, (50, 50))

    if escPressed == True:
        screen.blit(escMessage_2, (50, 50))

    keys = pygame.key.get_pressed()
    if keys[pygame.K_ESCAPE]:
        pygame.mixer.music.stop()
        progress = 0
        escPressed = True

    keys2 = pygame.key.get_pressed()
    if keys2[pygame.K_c]:
        coryMusicBool = True
        coryPicBool = True

    keys3 = pygame.key.get_pressed()
    if keys3[pygame.K_s]:
        shrekPicBool = True
        shrekMusicBool = True

    if progress/2 >= 100:
        progress = 200

    loading(progress/2)
    pygame.display.flip()

Our code is supposed to be a loading bar. The text that shows the problem of the progression is in the loading x % part. Please help, and thank you.

2 Answers2

1

Try This

import pygame
import random

pygame.init()

progress = 0

into = True

black = [0, 0, 0]
white = [255, 255, 255]
intoWhite = [249, 244, 244]
green = [0, 255, 0]

screenWidth = 600
screenHeigth = 450
size = [screenWidth, screenHeigth]

font = pygame.font.SysFont("Comic Sans MS", 25)

intoPic = pygame.image.load("images/Can You Surive Pic.png")
cory = "audio/Cory in the House - Opening Sequence.mp3"
coryPic = pygame.image.load("images/CoryBaxterFull.png")
coryMusicBool = False
coryPicBool = False
basic = "audio/basic song.mp3"
shrekPicBool = False
shrekMusicBool = False
shrek = "audio/Shrek's Fairytale Freestyle.mp3"
shrekPic = pygame.image.load("images/shrek.png")

clock = pygame.time.Clock()
clock.tick(10)

count = 0
escPressed = False

screen = pygame.display.set_mode(size)
pygame.display.set_caption('Loading...')

escMessageText = "Press Esc to cancel"
escMessage = font.render(escMessageText, False, white)
escMessageText_2 = "You Can't Leave"
escMessage_2 = font.render(escMessageText_2, False, white)


def textobjecte(text, color, size):
    if size == "small":
        textsuraface = font.render(text, True, color)

        return textsuraface, textsuraface.get_rect()


def loading(progress):
    text = ""

    if progress < 101:
        text = font.render("loading: " + str(int(progress)) + "%", True, green)

    screen.blit(text, (300, 100))


def message_to_screen(msg, color, y_displace, size="small"):
    textsurf, textrect = textobjecte(msg, color, size)
    textrect.center = (screenWidth/2), (screenHeigth/2) + y_displace

    screen.blit(textsurf, textrect)


def playcory():
    pygame.mixer.music.load(cory)
    pygame.mixer.music.play(-1)


def playshrek():
    pygame.mixer.music.load(shrek)
    pygame.mixer.music.play(-1)


def basicsong():
    pygame.mixer.music.load(basic)
    pygame.mixer.music.play(-1)


basicsong()

while into == True:
    pygame.event.pump()
    screen.fill(intoWhite)
    screen.blit(intoPic, (75, 0))
    intoKey = pygame.key.get_pressed()
    pygame.display.flip()
    if intoKey[pygame.K_SPACE]:
        into = False


while progress/2 < 100:
    timeCount = random.randint(1, 10000)
    increase = random.randint(1, 10)
    progress += increase/10000
    screen.fill(black)
    pygame.event.pump()
    pygame.draw.rect(screen, white, [300, 50, 200, 50])
    pygame.draw.rect(screen, black, [301, 51, 198, 48])

    if (progress/2) > 100:
        pygame.draw.rect(screen, white, [302, 52, 196, 46])
    else:
        pygame.draw.rect(screen, white, [302, 52, progress, 46])

    if coryMusicBool == True:
        pygame.mixer.music.stop()
        playcory()
        coryMusicBool = False
    if coryPicBool== True:
        screen.blit(coryPic, (0, 0))

    if shrekMusicBool == True:
        pygame.mixer.music.stop()
        playshrek()
        shrekMusicBool = False
    if shrekPicBool == True:
        screen.blit(shrekPic, (100, 0))

    if escPressed == False:
        screen.blit(escMessage, (50, 50))

    if escPressed == True:
        screen.blit(escMessage_2, (50, 50))

    keys = pygame.key.get_pressed()
    if keys[pygame.K_ESCAPE]:
        pygame.mixer.music.stop()
        progress = 0
        escPressed = True

    keys2 = pygame.key.get_pressed()
    if keys2[pygame.K_c]:
        coryMusicBool = True
        coryPicBool = True

    keys3 = pygame.key.get_pressed()
    if keys3[pygame.K_s]:
        shrekPicBool = True
        shrekMusicBool = True

    if progress/2 >= 100:
        progress = 200

    loading(progress/2)
    pygame.display.flip()

For the SRGb error add this to your code pngcrush -ow -rem allb -reduce file.png. its from this link. libpng warning: iCCP: known incorrect sRGB profile For your game quitting at 100% its because your while loop ends when you reach 100% so you should make it so the draw screen statement is in a separate loop from the draw the loading bar one

Sam Green
  • 48
  • 6
  • How? One does not simply make pygame.surface. – ALEXANDER CARROLL Jul 18 '18 at 19:38
  • Please explain how your solution works and don't post only code. This should work as well, if the `progress` always stays below 101, otherwise the `text = ""` will still raise an error. – skrx Jul 18 '18 at 21:23
  • sorry I'm still new to this. the reason this works is because at 100% it causes something else to happen which stops it reaching 101% and causing an error. – Sam Green Jul 18 '18 at 22:17
0

In the loading function, text is either a string if the progress is >= 100 or a pygame.Surface if the progress is < 100.

Since the pygame.Surface.blit method only takes a pygame.Surface as the first argument, a TypeError will be raised when you pass a string.

A simple fix would be to blit the text surface in the if clause:

def loading(progress):
    if progress < 100:
        text = font.render("loading: " + str(int(progress)) + "%", True, green)
        screen.blit(text, (300, 100))

You could also blit a transparent surface or do text = font.render("", True, green), but that would be pointless. Just don't blit anything when the progress is >= 100.

skrx
  • 19,980
  • 5
  • 34
  • 48
  • Ok. I will attempt it. – ALEXANDER CARROLL Jul 19 '18 at 13:19
  • We did this and our program still crashes with the incorrect sRGB profile error – ALEXANDER CARROLL Jul 19 '18 at 13:26
  • `libpng warning: iCCP: known incorrect sRGB profile` that's just a warning and can be ignored, since it doesn't affect the program. The `TypeError` was responsible for the crash. -- The solution in my answer works fine for me, so something else must be wrong. As I explained, the reason for the error was that an incorrect type (str) was passed to `screen.blit`, but that can't happen anymore in my example. – skrx Jul 19 '18 at 16:22