ユーザー入力の処理
CoddyのLuaジャーニー「Logic & Flow」セクションの一部 — レッスン 28/54。
チャレンジ
簡単io.read() を使用してユーザーのコマンドを読み取り、表示するようにゲームループを拡張してください。
前のチャレンジで作成した startingRoom、gardenRoom、および player 変数が提供されています。
ゲームループを以下のように修正してください:
- プレイヤーが現在いる部屋の description を表示する
- プロンプト
"> "(不等号の後にスペースが続くもの)を改行なしで表示する io.read()を使用してユーザーのコマンドを読み取る"You entered: "に続けて、ユーザーが入力したコマンドを表示する- コマンドが
"quit"であるか確認し、そうであればgameRunningをfalseに設定してループを終了する
ループは、ユーザーが "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