如何让“得分”显示在游戏窗口中?

时间:2011-10-23 22:17:18

标签: python pygame

到目前为止,在我的蛇游戏中,得分是用python shell打印的。我怎样才能在游戏窗口中打印出来呢?那么游戏和得分是在同一个窗口? 谢谢Eric

while running:


    screen.fill((0, 0, 0))
    worm.move()
    worm.draw()
    food.draw()

    if worm.crashed:
        exit();
    elif worm.x <= 0 or worm.x >= w -1:
        running = False
    elif worm.y <= 0 or worm.y >= h-1:
        running = False
    elif worm.position() == food.position():
        score += 1
        worm.eat()
        print " score: %d" % score
        food = Food(screen)
    elif food.check(worm.x, worm.y):
        score += 1
        worm.eat()
        print "score: %d" % score
        food = Food(screen)
    elif running == False:
        exit();

    for event in pygame.event.get():
        if event.type == pygame.quit:
            running = False
        elif event.type == pygame.KEYDOWN:
            worm.event(event)

    pygame.display.flip()
    clock.tick(100)

编辑 -

while running:


screen.fill((0, 0, 0))
worm.move()
worm.draw()
food.draw()
pygame.font.init()
pygame.font.get_fonts() 

if worm.crashed:
    exit();
elif worm.x <= 0 or worm.x >= w -1:
    running = False
elif worm.y <= 0 or worm.y >= h-1:
    running = False

elif food.check(worm.x, worm.y):
    score += 1
    worm.eat()
    food = Food(screen)
    message = 'score: %d' % score
    font = pygame.font.Font(None, 40)
    text = font.render(message, 1, white)
    screen.blit(text, (50, 50))

elif running == False:
    exit();

for event in pygame.event.get():
    if event.type == pygame.quit:
        running = False
    elif event.type == pygame.KEYDOWN:
        worm.event(event)

为什么得分Apearing,我没有得到任何错误?

2 个答案:

答案 0 :(得分:1)

使用font模块在​​屏幕上渲染字体。

来自用户指南:

white = (255, 255, 255)

message = 'your message'
font = pygame.font.Font(None, 40)
text = font.render(message, 1, white)
screen.blit(text, (x_position,y_position))

答案 1 :(得分:1)

响应你的编辑:当你选择特定的elif分支时,你只是将文本blit到屏幕上(即,只有当蠕虫获得食物时)。在下一帧,你清除屏幕,但食物不存在,所以得分不会被绘制。你应该每帧都将分数blit到屏幕上。