Menu
Coddy logo textTech

Pętla gry

Część sekcji Logika i przepływ programu ścieżki Lua w Coddy. Lekcja 27 z 54.

challenge icon

Wyzwanie

Łatwy

Zaimplementuj główną pętlę gry, która stale wyświetla opis bieżącego pokoju, gdy gra jest uruchomiona.

Masz już zmienne startingRoom, gardenRoom i player z poprzedniego wyzwania.

Utwórz zmienną o nazwie gameRunning i ustaw jej wartość na true.

Napisz pętlę while, która działa tak długo, jak gameRunning ma wartość true. Wewnątrz pętli:

  • Wyświetl opis bieżącego pokoju gracza
  • Ustaw wartość gameRunning na false, aby zakończyć pętlę (na razie dodamy obsługę wprowadzania danych w następnej lekcji)

Oczekiwany format wyjściowy:

You find yourself in a dusty library filled with ancient books and scrolls.

Spróbuj swoich sił

-- Create the starting room
local startingRoom = {
    name = "Ancient Library",
    description = "You find yourself in a dusty library filled with ancient books and scrolls."
}

-- Create the garden room
local gardenRoom = {
    name = "Secret Garden",
    description = "A beautiful garden with exotic flowers and a mysterious fountain."
}

-- Link the rooms
startingRoom.nextRoom = gardenRoom
gardenRoom.previousRoom = startingRoom

-- Create the player table
local player = {
    currentRoom = startingRoom
}

-- Print the player's current room name and description
print(player.currentRoom.name)
print(player.currentRoom.description)

Wszystkie lekcje w sekcji Logika i przepływ programu

Poćwicz samodzielnie: Kompilator Lua online