python - 打印乐谱 - Pygame

标签 python text printing pygame

有谁知道我怎样才能让分数显示在我的游戏屏幕上?

到目前为止我有这段代码:

for bullet in bullet_list:

        block_hit_list = pygame.sprite.spritecollide(bullet, block_list, True)

        for block in block_hit_list:
        explosion.play()
        bullet_list.remove(bullet)
        all_sprites_list.remove(bullet)
        score += 10
        font = pygame.font.Font(None, 36)
        text = font.render(score, 1, (WHITE))
        textpos = text.get_rect(centerx=background.get_width()/2)
        background.blit(text, textpos)

    if bullet.rect.y < -10:
        bullet_list.remove(bullet)
        all_sprites_list.remove(bullet)

但是,一旦我启动游戏并发射子弹,我就会收到此错误:

"text = font.render(score, 1, (WHITE))

TypeError: text must be a unicode or bytes"

有谁知道我该如何解决这个问题?

谢谢

最佳答案

好的,所以首先您问的是如何正确绘制站点,这就像之前的答案一样,但是您将其 blit 到屏幕的方式是错误的。

您目前每次通过 for 循环时都会将分数 blit 到屏幕,这会导致它产生您正在体验的效果。这应该就在 for 循环之后。 示例:

for bullet in bullet_list: 
    block_hit_list = pygame.sprite.spritecollide(bullet, block_list, True) 
    for block in block_hit_list:
        explosion.play()
        bullet_list.remove(bullet)
        all_sprites_list.remove(bullet) 
        score += 10
        #removed this line!!!! ~ font = pygame.font.Font(None, 36) 
        #removed this line to!!!! ~ text = font.render(score, 1, (WHITE)) 
        #remove this line also!!! ~ textpos = text.get_rect(centerx=background.get_width()/2) 
        #finally remove this line!!!! ~ background.blit(text, textpos) 
    if bullet.rect.y < -10:
        bullet_list.remove(bullet) 
        all_sprites_list.remove(bullet)
#add in those removed lines after your for loop.
font = pygame.font.Font(None, 36)
text = font.render(score, 1, (WHITE))
textpos = text.get_rect(centerx=background.get_width()/2)
background.blit(text, textpos)

这应该有效。如果您需要任何进一步的帮助,请告诉我。

关于python - 打印乐谱 - Pygame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22623139/

相关文章:

r - 从作者单位中提取国家名称

c - 打印机模块的ESC/POS命令

html - 打印分页符问题 - 在浏览器中打印预览

java - 我需要在同一页上打印 JTextArea 和 JTable。

python - 将元素分配给字典列表中的一个数组

python - 图像 Python3 的中值滤波器

python - Flask-SQLAlchemy - 按上次观看排序的系列中第一集未观看的剧集

java - 如何获得一个实际的单词迭代器包装breakIterator?

text - 如何从头开始创建一个非常简单的文本编辑器?

python - 如何在 Lambda 中阅读或解释 Lambda