Menu
Coddy logo textTech

לולאת המשחק

חלק מהיחידה לוגיקה וזרימת התוכנית במסלול ה-Lua של Coddy. שיעור 27 מתוך 54.

challenge icon

אתגר

קל

ממשו את לולאת המשחק הראשית, שמציגה שוב ושוב את תיאור החדר הנוכחי כל עוד המשחק פועל.

המשתנים startingRoom, gardenRoom ו־player מהאתגר הקודם זמינים לכם.

צרו משתנה בשם gameRunning והגדירו את ערכו ל־true.

כתבו לולאת while שנמשכת כל עוד הערך של gameRunning הוא true. בתוך הלולאה:

  • הדפיסו את תיאור החדר הנוכחי של השחקן
  • הגדירו את gameRunning ל־false כדי לצאת מהלולאה (לעת עתה; נוסיף טיפול מתאים בקלט בשיעור הבא)

פורמט הפלט הצפוי:

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

נסו בעצמכם

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

כל השיעורים ביחידה לוגיקה וזרימת התוכנית

תרגלו בעצמכם: קומפיילר Lua אונליין