사용자 입력 처리하기
Coddy Lua 여정의 Logic & Flow 섹션에 포함된 레슨 — 54개 중 28번째.
챌린지
쉬움io.read()를 사용하여 사용자 명령을 읽고 표시하도록 게임 루프를 확장하세요.
이전 챌린지에서 사용된 startingRoom, gardenRoom, 그리고 player 변수가 제공됩니다.
다음과 같이 게임 루프를 수정하세요:
- 플레이어의 현재 방에 대한 설명을 출력합니다.
- 줄 바꿈 없이 프롬프트
"> "(큼 기호와 공백 하나)를 출력합니다. 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