Menu
Coddy logo textTech

プレイヤーの移動

CoddyのLuaジャーニー「Logic & Flow」セクションの一部 — レッスン 29/54。

challenge icon

チャレンジ

簡単

プレイヤーの入力を利用可能な出口と照らし合わせてチェックすることで、部屋の間を移動できるようにする移動システムを実装してください。

前のチャレンジで作成した startingRoomgardenRoom、および player 変数が提供されています。

移動コマンドを処理するようにゲームループを修正してください:

  • プレイヤーが現在いる部屋の description を表示する
  • プロンプト "> " (不等号の後にスペースを1つ)を改行なしで表示する
  • io.read() を使用してユーザーのコマンドを読み取る
  • コマンドが現在の部屋の exits テーブルのキーと一致するかどうかをチェックする
  • 一致した場合は、player.currentRoom をその出口に保存されている部屋に更新する
  • コマンドが "quit" の場合は、gameRunningfalse に設定してループを終了する

ループは、ユーザーが "quit" と入力するまで継続する必要があります。

注意: io.write("> ") は改行を追加しないため、次の出力行(部屋の説明)は、生の出力ストリームにおいてプロンプトの直後、同じ行に表示されます。各ループの反復では、部屋の説明が独自の行に表示され、その後に改行なしで > が表示されるため、次の説明はその同じ行に続いて表示されます。

期待される出力形式(入力が east、その後に quit の場合):

You find yourself in a dusty library filled with ancient books and scrolls.
> A hidden garden with overgrown plants and a small fountain.
> 

自分で試してみよう

-- Game rooms
local startingRoom = {
    description = "You find yourself in a dusty library filled with ancient books and scrolls."
}

local gardenRoom = {
    description = "You step into a beautiful garden with blooming flowers and a gentle breeze."
}

-- Player
local player = {
    currentRoom = startingRoom
}

-- Game loop
local gameRunning = true

while gameRunning do
    print(player.currentRoom.description)
    io.write("> ")
    local command = io.read()
    print("You entered: " .. command)
    
    if command == "quit" then
        gameRunning = false
    end
end

Logic & Flowのすべてのレッスン