Menu
Coddy logo textTech

Lidando com a Entrada do Usuário

Parte da seção Lógica e Fluxo do Journey de Lua da Coddy. Lição 28 de 54.

challenge icon

Desafio

Fácil

Estenda o loop do jogo para ler e exibir os comandos do usuário usando io.read().

Você recebeu as variáveis startingRoom, gardenRoom e player do desafio anterior.

Modifique o loop do jogo para:

  • Imprimir a descrição da sala atual do jogador
  • Imprimir o prompt "> " (sinal de maior seguido por um espaço) sem uma nova linha: use io.write() em vez de print(), pois io.write() exibe o texto sem adicionar uma nova linha ao final
  • Ler o comando do usuário usando io.read()
  • Imprimir "You entered: " seguido pelo comando digitado pelo usuário
  • Verificar se o comando é "quit" — se for, definir gameRunning como false para sair do loop

O loop deve continuar até que o usuário digite "quit".

Formato de saída 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

Experimente você mesmo

-- 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 as lições de Lógica e Fluxo

Pratique por conta própria: Compilador de Lua online