معالجة مدخلات المستخدم
جزء من قسم المنطق والتحكم في التدفق في رحلة Lua على Coddy. الدرس 28 من 54.
التحدي
سهلقم بتوسيع حلقة اللعبة لقراءة أوامر المستخدم وعرضها باستخدام 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جميع دروس المنطق والتحكم في التدفق
1التكرار المتقدم عبر الجداول
التكرار باستخدام pairs()التكرار باستخدام ipairs()pairs() مقابل ipairs()مراجعة - ورقة الشخصية2المزيد من دوال مكتبة Table
table.concat()إنشاء الجداول و unpack()table.sort()فرز مخصص باستخدام الدوالمراجعة - لوحة أعلى النتائج5مشروع: محرك مغامرة نصية
إعداد المشروع: الغرفةربط الغرفتدرّب بنفسك: مترجم Lua عبر الإنترنت