Menu
Coddy logo textTech

사용자 입력 처리하기

Coddy Lua 여정의 로직 및 흐름 제어 (Logic & Flow) 섹션에 포함된 레슨. 54개 중 28번째.

challenge icon

챌린지

쉬움

io.read()를 사용하여 사용자 명령을 읽고 표시하도록 게임 루프를 확장하세요.

이전 과제에서 startingRoom, gardenRoom, player 변수가 제공됩니다.

게임 루프를 다음과 같이 수정하세요:

  • 플레이어의 현재 방에 대한 설명을 출력합니다.
  • 줄 바꿈 없이 프롬프트 "> "(보다 큼 기호와 공백)를 출력합니다. 끝에 줄 바꿈을 추가하지 않고 텍스트를 출력하는 io.write()print() 대신 사용하세요.
  • 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

로직 및 흐름 제어 (Logic & Flow)의 모든 레슨

직접 연습해 보세요: 온라인 Lua 컴파일러