Menu
Coddy logo textTech
flag Ar iconالعربيةdown icon

معالجة مدخلات المستخدم

جزء من قسم المنطق والتحكم في التدفق في رحلة Lua على Coddy. الدرس 28 من 54.

challenge icon

التحدي

سهل

قم بتوسيع حلقة اللعبة لقراءة أوامر المستخدم وعرضها باستخدام io.read().

تم تزويدك بالمتغيرات startingRoom و gardenRoom و player من التحدي السابق.

قم بتعديل حلقة اللعبة لـ:

  • طباعة description الخاصة بـ player للغرفة الحالية
  • طباعة المحث "> " (علامة أكبر من متبوعة بمسافة) بدون سطر جديد — استخدم io.write() بدلاً من print()، حيث تقوم io.write() بإخراج النص دون إضافة سطر جديد في النهاية
  • قراءة command الخاص بالمستخدم باستخدام io.read()
  • طباعة "You entered: " متبوعة بـ command الذي أدخله المستخدم
  • التحقق مما إذا كان command هو "quit" - إذا كان كذلك، قم بتعيين قيمة gameRunning إلى false للخروج من loop

يجب أن تستمر الـ loop حتى يكتب المستخدم "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 عبر الإنترنت