pygameを使う
2次元グラフィクスを扱います。
モジュール pygame を使います。
import pygame import sys from pygame.locals import * pygame.init() #モジュール初期化 screen = pygame.display.set_mode( (400,330) ) #ウィンドウ生成 screen.fill( (0,0,0) ) #画面を黒く塗りつぶす pygame.draw.rect(screen,(255,0,0),(20,70,10,180)) #赤い四角を描く pygame.display.update() #画面表示を更新 while True: for event in pygame.event.get(): if event.type == QUIT: #終了処理 pygame.image.save(screen, "test.png") #ウィンドウイメージをセーブ pygame.quit() #モジュールの終了 sys.exit() #プログラム終了
ウィンドウ生成
screen = pygame.display.set_mode(画面サイズ)
画面サイズ = (横, 縦)
四角を描く
pygame.draw.rect(スクリーンオブジェクト, 色, 四角の形)
色 = (R, G, B) #RGBは0~255 (28。3原色合わせて 224=1670万色)
四角の形 = (左上のx座標, 左上のy座標, 横幅, 縦幅)
円を描く
pygame.draw.circle(スクリーンオブジェクト, 色, 中心座標, 半径)
中心座標 = (x座標, y座標)
楕円を描く
pygame.draw.ellipse(スクリーンオブジェクト, 色, 楕円の形)
楕円の形 = (中心のx座標, 中心のy座標, 横幅, 縦幅)
線を描く
pygame.draw.line(スクリーンオブジェクト, 色, 始点, 終点, 幅)
始点 = (x座標, y座標)
終点 = (x座標, y座標)
多角形を描く
pygame.draw.polygan(スクリーンオブジェクト, 色, 座標のリスト, 幅)
座標のリスト = [(x座標, y座標), (x座標, y座標), (x座標, y座標) …]幅を0にすると塗りつぶし
文字を描く
font1 = pygame.font.SysFont("PlemolJP",150) #フォントの設定 text1 = font1.render("My Name", True, (255,255,255)) #文字の書き出し screen.blit(text1,(100,100)) #文字の描画
SysFont(フォント名, サイズ)
font.render(文字列, アンチエイリアス処理の有無, 色)
screen.blit(テキストオブジェクト, 表示位置)
使用できるフォントは
print(pygame.font.get_fonts())
で取得