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

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

جزء من قسم Logic & Flow في رحلة Lua على Coddy — الدرس 28 من 54.

challenge icon

التحدي

سهل

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

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

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

  • طباعة وصف الغرفة الحالية للاعب
  • طباعة المطالبة "> " (علامة أكبر من متبوعة بمسافة) بدون سطر جديد
  • قراءة أمر المستخدم باستخدام 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

جميع دروس Logic & Flow