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?

【renpy】アイテムの合成

Last updated at Posted at 2025-07-05

雑記

餅は餅屋ということで、ゲームプログラミングであればUnity!というノリで学習を始めたのだけれど、ハードルが高すぎて挫折。

でも今年はアプリ作ってみたいという欲求もあり、pythonで作れるものはないかと探したところ、renPyに行き着く。
(pyGameやpyxelも候補に入れてたんだけど、ニコニコ動画で「ドキドキ文芸部」の実況にハマった時期もあり、これが決め手となりました。)

といっても、一朝一夕で出来るものでもなく、覚えた知識もジャンジャカ忘れていくので、Qiita記事でまとめようかな?と思いました。

AtCoderをベースとしたゲームアプリなんか作ってみたいのです。

※ 記事の順序は覚書の順序になってしまいますが、目次を作って読みやすい順序にする予定です。

動作確認

Ren'Py 8.3.1

サンプルコード

renpy_sample.py
init python:
    # インベントリリスト
    inventory = ["red", "blue"]  # 最初は赤と青

    # 合成ルール: 赤+青=紫
    def combine_items(item1, item2):
        if (item1 == "red" and item2 == "blue") or (item1 == "blue" and item2 == "red"):
            inventory.remove(item1)
            inventory.remove(item2)
            inventory.append("purple")
            return "purple"
        return None

# Solid色画像を定義
image item_red = Solid("#f44", xysize=(64,64))
image item_blue = Solid("#44f", xysize=(64,64))
image item_purple = Solid("#a3f", xysize=(64,64))

screen inventory_screen():
    vbox:
        text "インベントリ:"
        hbox:
            # 赤アイテム
            if "red" in inventory:
                if "blue" in inventory:
                    imagebutton:
                        idle "item_red"
                        action [Function(combine_items, "red", "blue"), Hide("inventory_screen"), Show("inventory_screen")]
                        tooltip "赤+青を合成"
                else:
                    add "item_red"
            # 青アイテム
            if "blue" in inventory:
                if "red" in inventory:
                    imagebutton:
                        idle "item_blue"
                        action [Function(combine_items, "red", "blue"), Hide("inventory_screen"), Show("inventory_screen")]
                        tooltip "赤+青を合成"
                else:
                    add "item_blue"
            # 紫アイテム
            if "purple" in inventory:
                add "item_purple"

        if "purple" in inventory:
            text "紫ができた!"

label start:
    show screen inventory_screen
    "赤と青のアイテムがあります。"
    "ボタンを押して合成してください。"
    "合成後、紫アイテムが表示されます。"
    "終了です。"
  1. インベントリと合成ルールの定義(Python)
  2. アイテム画像の定義
  3. インベントリ画面の実装(screen)
  4. ゲームの進行(label start)

1. インベントリと合成ルールの定義

init python:
    # インベントリリスト
    inventory = ["red", "blue"]  # 最初は赤と青

    # 合成ルール: 赤+青=紫
    def combine_items(item1, item2):
        if (item1 == "red" and item2 == "blue") or (item1 == "blue" and item2 == "red"):
            inventory.remove(item1)
            inventory.remove(item2)
            inventory.append("purple")
            return "purple"
        return None

解説

  • inventory:現在持っているアイテムをリストで管理。最初は「red(赤)」と「blue(青)」が入っています。
  • combine_items関数
    • 「赤」と「青」が両方ある場合、両方をインベントリから削除し、「紫(purple)」を追加します。
    • 合成が成立しない場合はNoneを返します。

2. アイテム画像の定義

image item_red = Solid("#f44", xysize=(64,64))
image item_blue = Solid("#44f", xysize=(64,64))
image item_purple = Solid("#a3f", xysize=(64,64))

解説

  • Solidを使って、シンプルな64x64ピクセルの色付き画像を作成しています。
  • それぞれ「赤」「青」「紫」を表現。

3. インベントリ画面の実装

screen inventory_screen():
    vbox:
        text "インベントリ:"
        hbox:
            # 赤アイテム
            if "red" in inventory:
                if "blue" in inventory:
                    imagebutton:
                        idle "item_red"
                        action [Function(combine_items, "red", "blue"), Hide("inventory_screen"), Show("inventory_screen")]
                        tooltip "赤+青を合成"
                else:
                    add "item_red"
            # 青アイテム
            if "blue" in inventory:
                if "red" in inventory:
                    imagebutton:
                        idle "item_blue"
                        action [Function(combine_items, "red", "blue"), Hide("inventory_screen"), Show("inventory_screen")]
                        tooltip "赤+青を合成"
                else:
                    add "item_blue"
            # 紫アイテム
            if "purple" in inventory:
                add "item_purple"

        if "purple" in inventory:
            text "紫ができた!"

解説

  • vbox/hbox:インベントリのレイアウトを整えます。
  • imagebutton
    • 「赤」と「青」が両方あるときだけ、ボタンが表示されます。
    • ボタンを押すとcombine_items関数が呼ばれ、合成が実行されます。
    • 合成後、画面を一度隠して再表示することで、インベントリの内容を更新します。
  • add:ボタンではなく、ただアイテム画像を表示。
  • 紫ができた!:紫がインベントリにあれば、テキストで合成成功を表示。

4. ゲームの進行

label start:
    show screen inventory_screen
    "赤と青のアイテムがあります。"
    "ボタンを押して合成してください。"
    "合成後、紫アイテムが表示されます。"
    "終了です。"

解説

  • ゲーム開始時にインベントリ画面が表示され、合成を促すメッセージが順に表示されます。
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?