Hello world.のPygame版です。
ソースです。
import pygame
import sys
def main():
pygame.init()
pygame.display.set_caption("Pygame 文字の描画")
screen = pygame.display.set_mode((640, 480))
clock = pygame.time.Clock()
font = pygame.font.Font(None, 40)
tm = 0
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
# 終了ボタンで終了
pygame.quit()
sys.exit()
# 以下に処理を書く
screen.fill((0,0,0))
txt = font.render('Hello world. ' + str(tm), True, (255,255,255))
screen.blit(txt, [250, 200])
pygame.display.update()
clock.tick(1)#1秒間のフレーム数 1sec
tm += 1
if __name__ == '__main__':
main()
1秒ごとにHello world. とカウンタ値を表示します。
screen.fill((0,0,0))をやらないとカウンタ値が残っているので、それを上書きして何だか分からない表示になってしまいます。これは画面のオールクリアです。
fontオブジェクトを取得してHoeelo workd とカウント値(tm)をセットしてscreenに貼り付けます。
|