Menu
Coddy logo textTech

Manejo de la entrada del usuario

Parte de la sección Logic & Flow del Journey de Lua de Coddy — lección 28 de 54.

challenge icon

Desafío

Fácil

Extiende el bucle del juego para leer y mostrar los comandos del usuario utilizando io.read().

Se te proporcionan las variables startingRoom, gardenRoom y player del desafío anterior.

Modifica el bucle del juego para:

  • Imprimir la descripción de la habitación actual del jugador
  • Imprimir el prompt "> " (signo de mayor que seguido de un espacio) sin un salto de línea
  • Leer el comando del usuario utilizando io.read()
  • Imprimir "You entered: " seguido del comando que el usuario escribió
  • Verificar si el comando es "quit"; si es así, establece gameRunning en false para salir del bucle

El bucle debe continuar hasta que el usuario escriba "quit".

Formato de salida esperado:

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

Pruébalo tú mismo

-- 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

Todas las lecciones de Logic & Flow