PythonとPygameで日本語フォントを表示するプログラムを書いてみました。
日本語フォントがないと始まらないので、姫明朝ともえごぜんminiという無料フォントをダウンロード。
ダウンロードしたファイルを解凍し、プログラムと同じディレクトリに置きます。
サンプルプログラムは以下の通り。
■jfont.py
import sys import pygame from pygame.locals import QUIT def main(): pygame.init() surface = pygame.display.set_mode((400, 200)) print(surface.get_rect().width) print(surface.get_rect().height) clock = pygame.time.Clock() # Clockオブジェクト作成 jfont = pygame.font.Font('姫明朝ともえごぜんmini.otf', 20) # フォントファイルのパス # (テキスト, アンチエイリアス, カラー)を指定 text = jfont.render("パイソンで日本語表示", True, (0x88, 0xC0, 0xEF)) textpos = text.get_rect() textpos.centerx = surface.get_rect().centerx # X座標 textpos.centery = surface.get_rect().centery # Y座標 while True: # イベントループ for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() surface.fill((88, 90, 100)) surface.blit(text, textpos) pygame.display.update() clock.tick(10) if __name__ == '__main__': main()
■実行結果
$ python jfont.py
WindowsのPythonでも同じプログラムが動きます(まあ、当たり前か)。
備考
Pygameのインストールが必要です。
$ pip install pygame