pythonでゲームを作っていきたいと思います。
今回使うのは”pygame”を使っていきたいと思います
pygameの使い方
1.install
まずインストールしなければなりません。ダウンロードするにはpip install pygame
を使います。
2.とりあいずウィンドウを作る
ウィンドウを作る方法を教えます
import pygame
pygame.init()
screen = pygame.display.set_mode((800,600))
pygame.display.set_caption("game")
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
この.pyを起動するとこうなるはずです。
では説明をしていきます
import pygame
はpygameをインポートします。これをしないとだめです。
pygame.init()
で初期化をしますこれもしないとエラーが出ます
screen = pygame.display.set_mode((800,600))
はウィンドウをピクセル数((中))この中に入れた数字を作ります
pygame.display.set_caption("game")
はウィンドウの名前です("ここに書いて")
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
これで×マークを押したら消えるプログラムです。消えさせるだけでもたいへんですね
次は背景・文字を書いていきます
文字の書き方
文字の書き方はこうなります
import pygame
pygame.init()
screen = pygame.display.set_mode((800,600))
#フォントを設定する
font = pygame.font.Font(None, 36)
text = "Debug test"
color = (255, 255, 255)
antialias = True
#フォントを使いタイトルテキストを作る
tital = font.render(text, antialias, color)
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
#タイトルテキストを表示する
screen.blit(tital, (100, 100))
pygame.display.update()
となりますこれを起動すると
先ほどの画面に debug test と出るはずです
背景
背景を変えるにfill
を使います。ソースコード☟
import pygame
pygame.init()
screen = pygame.display.set_mode((800,600))
font = pygame.font.Font(None, 36)
text = "Debug test"
color = (255, 255, 255)
antialias = True
tital = font.render(text, antialias, color)
screen.fill((0,250,0))
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
screen.blit(tital, (100, 100))
pygame.display.update()
これを実行すると背景が緑になるはずです。
次の記事は投稿2か月後までフォローが1でも増えたら出します
Xやっていますフォローお願いします
X_kuma