1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Pyxelを使ってゲームを作ってみる

1
Posted at

目的

Pythonでレトロゲームを作れるゲームエンジン Pyxel を使って簡単なゲームを作ってみたのでメモ。
グラディウスっぽい横スクロールシューティングを作ってみた。
ブラウザ上で実行する

必要なライブラリ

Pyxel

インストール

python venvを使って仮想環境を作ってPyxelをインストールした。

% python -m venv pyxel
% source pyxel/bin/activate
% pip install -U pyxel

キャラクタとサウンドの作成

Pyxelのユーティリティを使って作成する

% mkdir assets
% pyxel edit asstes/test.pyxres

キャラクタを作る
pyxel_chara.png
画面に表示する時はこんなコード

  pyxel.blt(
    self.x,
    self.y,
    self.playerres.res_page[self.player.res_n],
    self.playerres.res_u[self.player.res_n],
    self.playerres.res_v[self.player.res_n],
    self.playerres.res_w[self.player.res_n],
    self.playerres.res_h[self.player.res_n],
    self.playerres.res_col[self.player.res_n],
  )

サウンドを作る
pyxcel_sound.png
音を再生するコードはこんな感じ

  pyxel.play(0, 2, loop=True)

プログラムを作る

ソースはこちら
画面の初期化とリソースファイルの読み込みはこんなコード

  pyxel.init(self._width, self._height, title="test", fps=15)
  pyxel.load("assets/test.pyxres")
  pyxel.run(self.update, self.draw)

キーの入力はこんなコード

  # "Up" key
  if pyxel.btn(pyxel.KEY_UP) or pyxel.btn(pyxel.GAMEPAD1_BUTTON_DPAD_UP):
    if self.player.isAlive:
      if self.player.y >= self.player.move_y:
        self.player.y -= self.player.move_y

アプリ化・Webアプリ化

Pyxelで作ったゲームはパッケージ化後にWeb上で動かせるHTMLに変換できる。

% cd ..
% pyxel package test test/test.py
% pyxel app2html test

これで test.htmlが作成されるので、ブラウザで開くとゲームが遊べる。
携帯からでも動く。
ブラウザ上で実行する

実行画面

敵のキャラクタは1個しか作ってないけど、遊べるようになった。

play1.png
play2.png
pyxel-20260125-155533.gif

その他

Pyxelで実行中のものは "Alt+1" で画面キャプチャが出来る。
動画キャプチャをする時は "Alt+2"でスタート、"Alt+3"で終了。
ファイルはデスクトップ上に保存される。

1
1
1

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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?