Menu
Coddy logo textTech

ユーザー入力の処理

CoddyのLuaジャーニー「ロジックと制御フロー」セクションの一部。レッスン 28/54。

challenge icon

チャレンジ

簡単

io.read()を使用してユーザーのコマンドを読み取り、表示するようにゲームループを拡張してください。

前のチャレンジで使用したstartingRoomgardenRoomplayer変数が提供されています。

ゲームループを変更して、次の処理を行ってください。

  • プレイヤーの現在の部屋の説明を出力する
  • 改行なしでプロンプト"> "(大なり記号の後にスペース)を出力する:io.write()は末尾に改行を追加せずにテキストを出力するため、print()の代わりにio.write()を使用する
  • io.read()を使用してユーザーのコマンドを読み取る
  • "You entered: "の後に、ユーザーが入力したコマンドを出力する
  • コマンドが"quit"かどうかを確認し、そうであればgameRunningfalseに設定してループを終了する

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

期待される出力形式:

You find yourself in a dusty library filled with ancient books and scrolls.
> 
You entered: look
You find yourself in a dusty library filled with ancient books and scrolls.
> 
You entered: east
You find yourself in a dusty library filled with ancient books and scrolls.
> 
You entered: quit

自分で試してみよう

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

local gardenRoom = {
    description = "You are in a beautiful garden with blooming flowers and a fountain."
}

-- Player definition
local player = {
    currentRoom = startingRoom
}

-- Create game loop
local gameRunning = true

while gameRunning do
    print(player.currentRoom.description)
    gameRunning = false
end

ロジックと制御フローのすべてのレッスン

自分で練習してみよう: Luaオンラインコンパイラ