사용자 입력 처리하기
Coddy Lua 여정의 논리와 흐름 제어 섹션에 포함된 레슨. 54개 중 28번째.
챌린지
쉬움io.read()를 사용해 사용자 명령을 읽고 표시하도록 게임 루프를 확장하세요.
이전 챌린지에서 제공된 startingRoom, gardenRoom, player 변수를 사용하세요.
게임 루프를 수정하여 다음을 수행하세요.
- 플레이어가 있는 현재 방의 설명을 출력합니다.
- 줄바꿈 없이 프롬프트
"> "(보다 큼 기호 뒤에 공백)을 출력합니다.io.write()는 끝에 줄바꿈을 추가하지 않고 텍스트를 출력하므로print()대신io.write()를 사용하세요. 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직접 해보기
-- 게임 방
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."
}
-- 플레이어
local player = {
currentRoom = startingRoom
}
-- 게임 루프
local gameRunning = true
-- TODO: 여기에 while 루프를 작성하세요
논리와 흐름 제어의 모든 레슨
직접 연습해 보세요: 온라인 Lua 컴파일러