0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ゲーム開発で使用したテクニック~画像データの取得~

Posted at

個人でゲーム開発をしている中で、実装に苦労した部分が多かったので、テクニックとして紹介していこうと思います。

今回は「画像データの取得」について書きます。

画像データの配置場所

「game.py」が起動ファイルだったとして、同階層に「img」フォルダを作ります。
必要な画像データは「img」フォルダに入れます。

画像データの取得

ただロードするだけなら1行です。
「img_path」には「img\XXXXX.png」という感じでPATHが入ります。

self.IMG = pg3.image.load(img_path)

問題点

pyinstallerを使用してexeファイルを作成しました。
すると、画像参照ができないというエラーに遭遇しました。
exeを起動すると必要なファイルが一時領域に展開されるのですが、それがフォルダ構成を維持してくれませんでした。
ということで、コードは以下のようになりました。

img_path = self.get_image_path(self.IMG_NAME)
self.IMG = pg3.image.load(img_path)

def get_image_path(self, filename):
    """
    画像ファイルのパスを返す
    """
    if hasattr(sys, '_MEIPASS'):
        return os.path.join(sys._MEIPASS, filename)
    return os.path.join(os.path.abspath("."), "img\\" + filename)

exeファイルを実行しt時に一時領域が追加されている場合は、一時領域の直下にあるファイルをpathに追加しています。
普通にpyファイルを実行する場合は、一時領域がありませんので、imgフォルダ直下にあるファイルをpathに追加しています。

「画像データの取得」に関しては以上となります。

リリース済みのゲームはこちら

・PC向け無料ゲーム
『LAbyrinth』(2Dの迷路探索ゲーム)
●Freem!
https://www.freem.ne.jp/win/game/33791
●Unityroom
https://unityroom.com/games/2025-labyrinth-isukaka

0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?