اختر مغامرتك الخاصة
تعرف على كيفية دمج الوظائف إدخال والمنطق في لعبة بسيطة.
سنغطي ما يلي...
الآن بعد أن أصبح بإمكانك بناء وظائفك وتلقي إدخال، فلنقم بعمل شيء ممتع: لعبة مغامرات صغيرة يختار فيها المستخدم القصة!
يجمع هذا الدرس كل ما تعلمناه:input() ، المتغيرات،if /else ، والوظائف.
مسار قصتنا الأول
لنقم بإنشاء لعبة صغيرة حيث نكتب وظيفة تطلب من اللاعب إجراء اختيار وتتفاعل بناءً على إجابته.
سنركز على:
كيف
input()يتم استخدامه داخل الدالة.كيف
if،elif، وelseالتصريحات هي التي تقرر النتيجة.
نحن نكتب قصة بسيطة حيث يتخذ المستخدم قرارًا واحدًا، وتتغير القصة بناءً على إجابته.
def adventure():
print("You stand at a fork in the road.")
choice = input("Do you go left or right? ")
if choice == "left":
print("You encounter a friendly dragon!")
elif choice == "right":
print("You find a hidden treasure chest!")
else:
print("You get lost in the woods.")
adventure()لقد صنعنا للتو قصة تفاعلية!
أضف المزيد من العمق
الآن، لنُطيل القصة قليلًا بإضافة سؤال ثانٍ. هكذا تُنشئ مغامرات متفرعة!
def adventure():
print("You stand at a fork in the road.")
choice = input("Do you go left or right? ")
if choice == "left":
print("You encounter a friendly dragon!")
second = input("Do you talk to the dragon or walk away? ")
if second == "talk":
print("The dragon gives you a magical map!")
elif second == "walk away":
print("You safely head back home, a little disappointed.")
else:
print("The dragon flies off while you hesitate.")
elif choice == "right":
print("You find a hidden treasure chest!")
second = input("Do you open it or leave it? ")
if second == "open":
print("You find gold and a mysterious key!")
elif second == "leave":
print("You walk away, wondering what might've been inside.")
else:
print("It disappears while you're thinking.")
else:
print("You get lost in the woods.")
adventure()الآن للقصة فروع! هذا ما يعنيه "إضافة عمق" - خياراتك الآن تقود إلى مواقف وقرارات جديدة!
اجعلها لك
استخدم خيالك! غيّر الإعدادات والخيارات والنتائج.
فيما يلي مجموعة أمثلة ممتعة في الفضاء الخارجي:
def adventure():
print("We're in space. An alien ship approaches.")
response = input("Do we hide or wave at them? ")
if response == "hide":
print("They pass by peacefully.")
elif response == "wave":
print("They beam us aboard and make us their king!")
else:
print("They ignore us. We float alone forever.")
adventure()قصتك وقواعدك!