首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何让“得分”显示在游戏窗口中?

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

Stack Overflow用户
提问于 2011-10-24 06:17:18
回答 2查看 3.8K关注 0票数 2

到目前为止,在我的蛇游戏中,分数是在python shell中打印的。怎样才能在游戏窗口中打印出来呢?这样游戏和分数就会出现在同一个窗口中?谢谢埃里克

代码语言:javascript
复制
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)

编辑-

代码语言:javascript
复制
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)

为什么分数不是很接近,我没有得到任何错误?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-10-24 06:24:50

使用font模块在屏幕上呈现字体。

从用户指南中:

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2011-10-24 12:27:43

响应您的编辑:只有当您选择特定的elif分支时(即,仅当蠕虫获取食物时),您才会将文本显示在屏幕上。在下一帧中,你清除了屏幕,但食物不在那里,所以比分不会被抽出。取而代之的是,你应该在每一帧都将分数显示在屏幕上。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7869637

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档