Pygame を使用したゲーム開発の実践ガイド ---Pygame の概要

王林
リリース: 2024-08-11 12:43:37
オリジナル
416 人が閲覧しました

この記事では、ゲーム開発ライブラリである Pygame をダウンロードしてインストールする方法を学び、Pygame に慣れるために簡単なサンプル プロジェクトを実行します。

プロジェクト コードのダウンロード リンク: https://github.com/la-vie-est-belle/pygame_codes


Pygame のインストール

Windows への Pygame のインストール
コマンド ライン ウィンドウを開き、コマンド pip install pygame を入力して Enter キーを押します。最後に「pygame が正常にインストールされました」というメッセージが表示された場合は、Pygame が正常にインストールされたことを意味します。
The Practical Guide to Game Development with Pygame---Introduction to Pygame

注: このチュートリアルで使用される Pygame のバージョンは 2.6.0 です。

もちろん、Pygame が正しく動作しているかどうかを確認する必要もあります。コマンド ライン ウィンドウを開き、「python」と入力し、Enter キーを押して Python コマンド ライン インターフェイスに入ります。次に「import pygame」と入力します。 Pygame のウェルカム メッセージが表示された場合は、インストールが成功し、Pygame が正常に使用できることを意味します。
The Practical Guide to Game Development with Pygame---Introduction to Pygame

MacOS への Pygame のインストール
同様に、ターミナルを開き、「pip3 install pygame」と入力し、Enter キーを押して Pygame をインストールします。確認方法は上記と同じなので、ここでは繰り返しません。


Pygame サンプル プロジェクトの実行

コマンド ライン ウィンドウを開き、コマンド python -m pygame.examples.aliens を実行して、Pygame に付属する組み込みのエイリアン ゲームを開始します。コントロールは次のとおりです:

  • 矢印キーでプレーヤーを移動します
  • スペースバーで撮影 The Practical Guide to Game Development with Pygame---Introduction to Pygame

The Practical Guide to Game Development with Pygame---Introduction to Pygame

今後の実践的な記事では、このエイリアン ゲームを一緒に開発して実装します。ここでは、この簡単な Pygame サンプル コード 1-1 を見てみましょう。

import sys
import pygame

pygame.init()                                       # 1
screen = pygame.display.set_mode((1100, 600))       # 2
pygame.display.set_caption('Dino Runner')           # 3

icon = pygame.image.load('icon.png')                # 4
pygame.display.set_icon(icon)

dino = pygame.image.load('dino_start.png')          # 5
dino_rect = dino.get_rect()
dino_rect.topleft = (80, 300)

while True:                                         # 6
    for event in pygame.event.get():                # 7
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    screen.fill((255, 255, 255))                    # 8
    screen.blit(dino, dino_rect)                    # 9
    pygame.display.flip()                           # 10
ログイン後にコピー

結果は次のようになります:
The Practical Guide to Game Development with Pygame---Introduction to Pygame

コードの説明:
#1 pygame.init() は、Pygame ライブラリ内のすべてのモジュールを初期化します。この行は、Pygame 関数またはクラスを使用する前に含める必要があります。

#2 pygame.display.set_mode() 関数を呼び出して、ゲーム ウィンドウのサイズを設定します (サイズはタプルとして渡されます)。この関数は、変数 screen に格納されているウィンドウ オブジェクトを返します。このオブジェクトを使用すると、ウィンドウにグラフィックを描画したり、画像やテキストを追加したりできます。 pygame.display モジュールは、ウィンドウ管理と画面表示専用です。

#3 pygame.display.set_caption() 関数を使用してウィンドウのタイトルを設定します。

#4 pygame.image.load() 関数を使用して画像 (この場合はウィンドウ アイコン) を読み込みます。この関数は、icon 変数に格納される画像オブジェクトを返します。次に、pygame.display.set_icon() 関数を使用してウィンドウ アイコンを設定します。

#5 主人公の画像を読み込み、get_rect()を呼び出してキャラクター画像の四角形領域(Rectオブジェクト)を取得し、この四角形の左上隅topleftを画面の位置

注: 画面座標の原点は左上隅にあり、X 軸は右に伸び、Y 軸は下に伸びます。 Pygame の座標系については後の章で詳しく説明します。

#6 Pygame が継続的にユーザー入力を検出して処理し、ゲームの状態を更新したり、画面のコンテンツを更新したりするゲーム ループに入ります。

#7 pygame.event.get() を使用してイベント キューを取得します。 for ループでは、各イベントを読み取り、処理します。イベントタイプevent.typeがpygame.QUIT(つまり、ウィンドウを閉じる)の場合、pygame.quit()を呼び出してゲームを終了します。 sys.exit() は、現在の Python プログラムを終了し、クリーンアップして、Pygame プログラムを実行しているスレッドを終了します。

#8 ウィンドウオブジェクト画面のfill()関数を呼び出して、ウィンドウを色で塗りつぶします。色の RGB 値、この場合は白を表すタプルを渡します。

#9 Call the blit() function of the window object screen to display the character image on the screen, with the position defined by the previously set rectangle dino_rect. You can also pass a coordinate tuple (x, y) to blit() to set the character's position on the screen, such as:

screen.blit(dino, (80, 300))
ログイン後にコピー

#10 Call the pygame.display.flip() function to refresh the screen content. You can also use pygame.display.update() to achieve the same effect. The latter can also accept a rectangle area to update only that portion of the screen. For example, the following line will update only a rectangle with the top-left corner at (0, 0) and a width and height of 350 pixels.

pygame.display.update((0, 0, 350, 350))
ログイン後にコピー

The Practical Guide to Game Development with Pygame---Introduction to Pygame


Summary

In this article, we learned how to install Pygame on Windows and MacOS, and explored a simple example code to understand the basic structure, operation principles, and some common functions of Pygame. If there are any parts that you do not fully understand, you can leave them for now. Later chapters may help clarify these concepts.

Buy author a cup of coffee if you enjoyed this tutorial. :)

  • The Practical Guide to Game Development with Pygame---Introduction to Pygame

  • The Practical Guide to Game Development with Pygame---Introduction to Pygame

  • The Practical Guide to Game Development with Pygame---Introduction to Pygame

以上がPygame を使用したゲーム開発の実践ガイド ---Pygame の概要の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ソース:dev.to
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!