Pygame入門 文字列を表示する
Pygameでは文字列を直接画面に表示することはできないそうです。
なので、下記のように3つの手順を実行します。
font = pygame.font.Font(None, 55) # フォントを設定する
text = font.render('abc', True, (255, 255, 255)) # 表示する文字列を作る
screen.blit(text, [50, 100]) # 画面に文字列を貼り付ける
renderの2つ目の引数にTrueを指定すると、
文字の角が滑らかに表示されるそうです。
import pygame from pygame.locals import * #pygameで設定された定数を使うため import sys def main(): pygame.init() screen = pygame.display.set_mode((400, 300)) # 画面の大きさを設定する pygame.display.set_caption('text') # 画面のタイトルを設定する font = pygame.font.Font(None, 55) # フォントを設定する while True: screen.fill((0, 0, 0)) # 画面を黒く塗りつぶす text = font.render('abc', True, (255, 255, 255)) # 表示する文字列を作る screen.blit(text, [50, 100]) # 画面に文字列を貼り付ける pygame.display.update() for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() if __name__ == '__main__': main()