플레이어 이동시키기
Coddy Lua 여정의 Logic & Flow 섹션에 포함된 레슨 — 54개 중 29번째.
챌린지
쉬움플레이어가 사용 가능한 출구와 입력을 대조하여 방 사이를 이동할 수 있도록 하는 이동 시스템을 구현하세요.
이전 챌린지에서 사용했던 startingRoom, gardenRoom, 그리고 player 변수가 제공됩니다.
이동 명령을 처리하도록 게임 루프를 수정하세요:
- 플레이어의 현재 방에 대한 설명을 출력합니다.
- 줄 바꿈 없이 프롬프트
"> "(부등호와 공백 한 칸)를 출력합니다. io.read()를 사용하여 사용자의 명령을 읽습니다.- 명령이 현재 방의
exits테이블에 있는 키와 일치하는지 확인합니다. - 일치하는 경우,
player.currentRoom을 해당 출구에 저장된 방으로 업데이트합니다. - 명령이
"quit"인 경우,gameRunning을false로 설정하여 루프를 종료합니다.
루프는 사용자가 "quit"을 입력할 때까지 계속되어야 합니다.
참고: io.write("> ")는 줄 바꿈을 추가하지 않기 때문에, 다음 출력 라인(방 설명)은 원시 출력 스트림에서 프롬프트 바로 뒤의 같은 라인에 나타납니다. 각 루프 반복은 다음을 생성합니다: 방 설명이 한 줄에 출력되고, 그 다음 줄 바꿈 없이 > 가 출력되므로, 이어지는 설명은 해당 라인에서 계속됩니다.
예상 출력 형식 (입력값: east, 그 다음 quit):
You find yourself in a dusty library filled with ancient books and scrolls.
> A hidden garden with overgrown plants and a small fountain.
> 직접 해보기
-- Game rooms
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."
}
-- Player
local player = {
currentRoom = startingRoom
}
-- Game loop
local gameRunning = true
while gameRunning do
print(player.currentRoom.description)
io.write("> ")
local command = io.read()
print("You entered: " .. command)
if command == "quit" then
gameRunning = false
end
end