Menu
Coddy logo textTech

Afficher les sorties

Fait partie de la section Logic & Flow du Journey Lua de Coddy — leçon 31 sur 54.

challenge icon

Défi

Facile

Améliorez l'expérience utilisateur du jeu en affichant toutes les sorties disponibles pour le joueur après avoir montré la description de la pièce.

Vous disposez des variables startingRoom, gardenRoom et player du défi précédent.

Modifiez la boucle de jeu pour afficher les sorties disponibles :

  • Affichez la description de la pièce actuelle du joueur
  • Affichez "Exits:" sur une nouvelle ligne
  • Utilisez pairs() pour itérer à travers la table exits de la pièce actuelle
  • Pour chaque sortie, affichez " - " (deux espaces, un tiret et un espace) suivi du nom de la direction
  • Affichez l'invite "> " (signe supérieur suivi d'un espace) sans saut de ligne
  • Lisez la commande de l'utilisateur en utilisant io.read()
  • Vérifiez si la commande correspond à une clé dans la table exits de la pièce actuelle
  • Si elle correspond, mettez à jour player.currentRoom avec la pièce stockée dans cette sortie
  • Si la commande ne correspond à aucune sortie et n'est pas "quit", affichez "You can't go that way."
  • Si la commande est "quit", définissez gameRunning sur false pour quitter la boucle

La boucle doit continuer jusqu'à ce que l'utilisateur tape "quit".

Format de sortie attendu :

You find yourself in a dusty library filled with ancient books and scrolls.
Exits:
  - east
> 
A hidden garden with overgrown plants and a small fountain.
Exits:
  - west
> 
You can't go that way.
A hidden garden with overgrown plants and a small fountain.
Exits:
  - west
> 
You find yourself in a dusty library filled with ancient books and scrolls.
Exits:
  - east
> 

Essayez vous-même

-- Room definitions
local startingRoom = {
    description = "You find yourself in a dusty library filled with ancient books and scrolls.",
    exits = {}
}

local gardenRoom = {
    description = "A hidden garden with overgrown plants and a small fountain.",
    exits = {}
}

-- Connect rooms
startingRoom.exits["north"] = gardenRoom
gardenRoom.exits["south"] = startingRoom

-- Player
local player = {
    currentRoom = startingRoom
}

-- Game loop
local gameRunning = true

while gameRunning do
    print(player.currentRoom.description)
    io.write("> ")
    local command = io.read()
    
    if command == "quit" then
        gameRunning = false
    elseif player.currentRoom.exits[command] then
        player.currentRoom = player.currentRoom.exits[command]
    else
        print("You can't go that way.")
    end
end

Toutes les leçons de Logic & Flow