LoginSignup
1
0

pythonでゲームを作る

Last updated at Posted at 2024-01-11

pythonでゲームを作っていきたいと思います。
今回使うのは”pygame”を使っていきたいと思います

pygameの使い方

1.install

まずインストールしなければなりません。ダウンロードするにはpip install pygame
を使います。

2.とりあいずウィンドウを作る

ウィンドウを作る方法を教えます

system.py
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を起動するとこうなるはずです。スクリーンショット 2024-01-11 163521.png
では説明をしていきます
import pygameはpygameをインポートします。これをしないとだめです。
pygame.init()で初期化をしますこれもしないとエラーが出ます
screen = pygame.display.set_mode((800,600))はウィンドウをピクセル数((中))この中に入れた数字を作ります
pygame.display.set_caption("game")はウィンドウの名前です("ここに書いて")

while文.py
while run: 
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

これで×マークを押したら消えるプログラムです。消えさせるだけでもたいへんですね

次は背景・文字を書いていきます

文字の書き方

文字の書き方はこうなります

system.py
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を使います。ソースコード☟

system.py
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

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0