Menu
Coddy logo textTech

טיפול בקלט המשתמש

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

challenge icon

אתגר

קל

הרחב את לולאת המשחק כדי לקרוא ולהציג פקודות משתמש באמצעות io.read().

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

שנה את לולאת המשחק כך שתבצע את הפעולות הבאות:

  • תדפיס את התיאור של החדר הנוכחי של השחקן
  • תדפיס את ההנחיה "> " (סימן גדול מ- ואחריו רווח) ללא ירידת שורה: השתמש ב-io.write() במקום ב-print(), משום ש-io.write() מציגה טקסט בלי להוסיף ירידת שורה בסוף
  • תקרא את הפקודה של המשתמש באמצעות io.read()
  • תדפיס את "You entered: " ואחריו את הפקודה שהמשתמש הקליד
  • תבדוק אם הפקודה היא "quit" — אם כן, תקבע את gameRunning ל-false כדי לצאת מהלולאה

הלולאה צריכה להימשך עד שהמשתמש יקליד "quit".

תבנית הפלט הצפויה:

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

נסו בעצמכם

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

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

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