Pygame中的进度条损坏

时间:2018-07-18 19:28:59

标签: python-3.x pygame

因此,当我和我的熟人在pygame中工作时,我们遇到了一个问题:每次进度条超过100时,它就会崩溃并向我们显示错误。错误代码为:

 "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". 

还有帮助,这是代码:

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()

我们的代码应该是一个加载栏。显示进度问题的文本在加载x%部分中。 请帮忙,谢谢。

2 个答案:

答案 0 :(得分:1)

尝试一下

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()

对于SRGb错误,请将其添加到代码pngcrush -ow -rem allb -reduce file.png中。从此链接。 libpng warning: iCCP: known incorrect sRGB profile 对于您的游戏退出率是100%的情况,是因为您的while循环在达到100%时结束,因此您应该这样做,以便绘制屏幕语句与绘制加载条处于一个单独的循环中

答案 1 :(得分:0)

loading函数中,如果进度为> = 100,则text是字符串,如果进度为<100,则pygame.Surface是字符串。

由于pygame.Surface.blit方法仅将pygame.Surface作为第一个参数,因此在传递字符串时将引发TypeError

一个简单的解决方法是在if子句中屏蔽text表面:

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

您还可以为透明表面涂色或进行text = font.render("", True, green),但这将毫无意义。只要进度> = 100,就不要涂任何东西。