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