Pygame 屏幕上的可变消息

Pygame Variable message to screen

所以我正在为某人做一个项目,我不知道为什么,但为什么我不能用变量代替一些文本?这让我有点恼火,因为我看不出它不起作用的原因。

#!/usr/bin/python
import pygame
pygame.init()
Surface = pygame.display.set_mode((1200,750))
pygame.display.set_caption('Blinking Lights - Avi Schiffmann')
clock = pygame.time.Clock()
blue = (51,51,255)
black = (0,0,0)
white = (255,255,255)
firebrick = (178,34,34)
crimson = (220,20,60)
pE = True
def mst(text,textcolor,x,y,fontsize):
    font = pygame.font.Font(None,fontsize)
    text = font.render(text, True, textcolor)
    Surface.blit(text, [x,y])
def button(x,y,w,h,ic,ac):
    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()

    if x+w > mouse[0] > x and y+h > mouse[1] > y:
        pygame.draw.rect(Surface, ac,(x,y,w,h))

        if click[0] == 1:
            asktime()   
    else:
        pygame.draw.rect(Surface, ic,(x,y,w,h))
def button2(x,y,w,h,ic,ac):
        mouse = pygame.mouse.get_pos()
        click = pygame.mouse.get_pressed()

        if x+w > mouse[0] > x and y+h > mouse[1] > y:
            pygame.draw.rect(Surface, ac,(x,y,w,h))

            if click[0] == 1:
                pygame.quit()
                quit()  
        else:
            pygame.draw.rect(Surface, ic,(x,y,w,h))

def asktime():
    timerr = 0
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_UP:
                    timerr += 600



        Surface.fill(blue)
        mst("Press Up arrow key to add 10 minutes to the timer.", white, 50, 50, 40)
        pygame.draw.rect(Surface, white, (100,100,300,300))
        mst(timerr, black, 50, 90, 45)


        pygame.display.update()
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
    Surface.fill(blue)
    button(350, 400, 100, 150, firebrick, crimson)
    button2(700, 400, 100, 150, firebrick, crimson)
    mst("On", black, 370, 460, 40)
    mst("Quit", black, 720, 460, 40)
    mst("BLINKING LIGHTS", white, 310, 50, 90)
    pygame.display.update()

是的,也是这个错误。

Traceback (most recent call last):
  File "BlinkingLights.py", line 67, in <module>
    button(350, 400, 100, 150, firebrick, crimson)
  File "BlinkingLights.py", line 25, in button
    asktime()   
  File "BlinkingLights.py", line 57, in asktime
    mst(timerr, black, 50, 90, 45)
  File "BlinkingLights.py", line 15, in mst
    text = font.render(text, True, textcolor)
TypeError: text must be a string or unicode

我 运行 Mac OSX 10.7.5 我知道它说文本必须是字符串或 unicode 但 timerr 是一个数字,为​​什么它不能打印出数字?

timerr 转换为 str 当您将它传递给 mst 时,您可以在 mst 中进行转换,但随后您将传递给字符串的所有数据都转换为字符串禁止 timerrr 这样会有点多余:

mst(str(timerr), black, 50, 90, 45)

如您所知,timerr 是引发错误的 int。如果您不想被转换 ints 所困扰,请更改您的函数来为您完成:

def mst(text,textcolor,x,y,fontsize):
    font = pygame.font.Font(None,fontsize)
    text = font.render(str(text), True, textcolor)
    Surface.blit(text, [x,y])