Skip to content

Instantly share code, notes, and snippets.

@vestige
Last active October 23, 2022 08:10
Show Gist options
  • Save vestige/e9435e5442e919b2fc4540b3d24609ce to your computer and use it in GitHub Desktop.
Save vestige/e9435e5442e919b2fc4540b3d24609ce to your computer and use it in GitHub Desktop.
import pyxel
class App:
def __init__(self):
pyxel.init(160, 120, title="hello")
pyxel.run(self.update, self.draw)
def update(self):
pass
def draw(self):
pyxel.cls(0)
if __name__ == "__main__":
App()
@kotaproj
Copy link

import pyxel


class App:
    def __init__(self):
        # pyxelの初期化
        pyxel.init(160, 120, title="パックマン")

        # 各キャラクタの準備
        # リソースファイルのキャラクタ読み込み
        pyxel.load("my_resource.pyxres")

        self.x, self.y = 80, 60
        self.move_size = 1
        self.dir = 1		# 1:左  2:右   3:上  4:下
        self.type = 0		# 0:口が小さい、 1:口が大きい

        # pyxelの実行
        # (フレーム更新時、描画時に呼ぶ関数の登録)
        pyxel.run(self.update, self.draw)

    def update(self):
        """フレーム更新時の処理"""

        # キー入力チェック
        if pyxel.btn(pyxel.KEY_LEFT):
            dx, dy = -1, 0
            self.dir = 1
        elif pyxel.btn(pyxel.KEY_RIGHT):
            dx, dy = 1, 0
            self.dir = 2
        elif pyxel.btn(pyxel.KEY_UP):
            dx, dy = 0, -1
            self.dir = 3
        elif pyxel.btn(pyxel.KEY_DOWN):
            dx, dy = 0, 1
            self.dir = 4
        else:
            return

        if pyxel.frame_count % 2 == 0:
            self.type = 1 - self.type

        # 各キャラクタの移動
        self.x += dx * self.move_size
        self.y += dy * self.move_size

        # 衝突判定
        if self.x < 0:
            self.x = 0
        if self.x + 8 > pyxel.width:
            self.x = pyxel.width - 8
        if self.y < 0:
            self.y = 0
        if self.y + 8 > pyxel.height:
            self.y = pyxel.height - 8

    def draw(self):
        """描画処理"""

        # 画面クリア
        pyxel.cls(0)

        # 各キャラクタの描画処理
        if self.dir == 1:
            pyxel.blt(self.x, self.y, 0, self.type * 8, 0, -8, 8)
        elif self.dir == 2:
            pyxel.blt(self.x, self.y, 0, self.type * 8, 0,  8, 8)
        elif self.dir == 3:
            pyxel.blt(self.x, self.y, 0, self.type * 8, 8,  8, -8)
        elif self.dir == 4:
            pyxel.blt(self.x, self.y, 0, self.type * 8, 8,  8, 8)


if __name__ == "__main__":
    App()

@vestige
Copy link
Author

vestige commented Oct 23, 2022

import pyxel
      
def sound_set():
   pyxel.sound(0).set(
      "C2D2E2R",
      "t",
      "7",
      "n",
      60,
   )

class App:
   def __init__(self):
      pyxel.init(160, 120, title="music")

      sound_set()
      pyxel.play(0, 0)
      pyxel.run(self.update, self.draw)
   
   def update(self):
      if pyxel.play_pos(0) is None:
         pyxel.quit()

   def draw(self):
      pyxel.cls(0)

if __name__ == "__main__":
   App()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment