【问题标题】:Can I use the .format feature when using screen.blit in Pygame?在 Pygame 中使用 screen.blit 时可以使用 .format 功能吗?
【发布时间】:2018-12-18 20:23:31
【问题描述】:

嘿,我最近不知道如何使用某些 x 和 y 值将列表 blit 到我的 pygame 屏幕上,允许文本 blit 说 x += 20,每次 blit 时将列表中的每个其他字符串移动超过 20 个单位。我最近做了一些 .format 的东西,只在控制台中打印,screen.blit 是否有这样的功能,所以我可以在 pygame 窗口中使用相同的格式?在下面包含我的代码。在此先感谢:D

import pygame


NAMES = ['Deanerys T.', 'Jon S.', 'Gregor C.', 'Khal D.', 'Cersei L.', 'Jamie L.', 
         'Tyrion L.', 'Sansa S.', 'Ayra S.', 'Ned S.']

DATE_OF_BIRTH = ['6/10/1996', '6/12/1984', '3/12/1980', '8/4/1986', '7/2/1970', 
                 '7/2/1975', '12/24/1980', '11/30/1993', '5/18/1999', '6/27/1984']

AGE = [22, 34, 38, 32, 48, 43, 38, 25, 19, 34]

MARITAL_STATUS = ['Not Married', 'Not Married', 'Not Married', 'Not Married', 
                  'Married', 'Not Married', 'Married', 'Married', 'Not Married', 'Married']

NUM_OF_CHILDREN = [3, 0, 0, 4, 2, 0, 1, 1, 0, 5]




for i in range(10):
    print("{:>12} was born {:>10}, is age {:>2}.  They have {:>1} children, and are {:>4}".format(NAMES[i], DATE_OF_BIRTH[i], AGE[i], NUM_OF_CHILDREN[i], MARITAL_STATUS[i]))
print("\n")

for i in range(10):
    print("NAME: {:>12} DATE OF BIRTH: {}".format(NAMES[i], DATE_OF_BIRTH[i], ))
print("\n")

for i in range(10):
    print("NAME: {:>12}  AGE: {}".format(NAMES[i], AGE[i], ))
print("\n")

for i in range(10):
    print("NAME: {:>12} MARRIAGE STATUS: {}".format(NAMES[i], MARITAL_STATUS[i], ))
print("\n")

for i in range(10):
    print("NAME: {:>12} NUMBER OF CHILDREN: {}".format(NAMES[i], NUM_OF_CHILDREN[i], ))
print("\n")

【问题讨论】:

    标签: python printing pygame format blit


    【解决方案1】:

    format是字符串上的操作符,所以在pygame中使用是没有问题的。

    例如:

    hp = 56
    player_hp_text = "Hit Points: {:>3}".format( hp )
    
    player_hp_bitmap = myfont.render( player_hp_text, 16, (255, 255,0) )
    screen.blit( player_hp_bitmap, ( 10, 10 ) )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      • 2021-03-01
      • 2013-10-18
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      相关资源
      最近更新 更多