تعلم صنع لعبة Hangman باستخدام Python
 |
| hangman game made with python |
مرحبًا بكم! في هذا المنشور, سنتعلم كيفية صنع لعبة Hangman (المشنقة, أو التخمين) باستخدام لغة البرمجة بايثون Python. لعبة Hangman هي لعبة تخمين الكلمات، حيث يحاول اللاعب تخمين الكلمة حرفًا بحرف قبل أن يكمل شكل الرجل المشنوق أو شكل المشنقة أو المقصلة.
سيناريو لعبة Hangman أو Guessing Game:
تبدأ اللعبة بظهور تعريف للكلمة Definition يتم قرائته من ملف json يتم حفظه في نفس مكان ملف الكود (نفس الفولدر) ثم الكلمة لكن كل حرف مستبدل ب " - ". بناء علي تعريف الكلمة, يتم البدء في تخمين الحروف. في حالة أن الحرف هو إحدي حروف الكلمة, يظهر الحرف مكان ال " - ". كل محاولة خطأ تقوم بإنقاص عدد المحاولات المتبقية.
- اسم ملف الكلمات هو words.json, لذلك سنستدعي مكتبة json بالإضافة إلي مكتبة random والتي ستختار كلمات بشكل عشوائى من ملف words.json. اذا كان ملف الكلمات فى نفس مكان ملف اللعبة, نكتب اسم الملف بشكل مباشر. اذا كان ملف الكلمات في مكان آخر, يجب كتابة ال path بالكامل الخاص بالملف.
استيراد المكتبات
في البداية، نستورد المكتبات اللازمة، وهي random لاختيار الكلمة العشوائية و json لقراءة ملف الكلمات.
تحميل الكلمات من ملف JSON:
نقوم بفتح ملف words.json الذي يحتوي على الكلمات وتعريفاتها، ثم نقوم بتحميل البيانات من هذا الملف.
with open("words.json") as f:
words_data = json.load(f):
words_data = json.load(f)
تصفية الكلمات التي تحتوي على تعريفات:
.نقوم بفلترة الكلمات الموجودة في القائمة لنحصل على الكلمات التي تحتوي على تعريفات
filtered_words = [word_data for word_data in words_data if "definition" in word_data]
التحقق من وجود كلمات بتعريفات:
نقوم بالتحقق مما إذا كان هناك كلمات بتعريفات أم لا، وإذا لم يكن هناك، نطبع رسالة تبليغية.
if not filtered_words:
print("No words with definitions found in 'words.json'.")
exit()
بدء اللعبة:
نقوم بطباعة رسالة ترحيبية واختيار كلمة عشوائية من بين الكلمات المصفاة، ونقوم بإظهار الكلمة بشكل مخفي بحروف شرطة.print('Welcome to Hangman')
secret_word_data = random.choice(filtered_words)
secret_word = secret_word_data["word"]
display_word = ['-'] * len(secret_word)
definition = secret_word_data["definition"]
print(f"Definition: {definition}")
print(display_word)
الدوران والتخمين:
نقوم بتكرار العمليات التالية حتى ينتهي اللعب، حيث يقوم اللاعب بإدخال حرف ونتحقق مما إذا كانت الحروف موجودة في الكلمة أم لا.
game_over = False
max_attempts = 7
attempts_left = max_attempts
hangman = ['''
+---+
| |
|
|
|
|
=========''', '''
+---+
| |
O |
|
|
|
=========''',
# نعرض الصور المتبقية للرجل المشنوق حسب عدد المحاولات المتبقية
]
while not game_over:
guess = input('Guess a letter: ').lower()
found = False
for position in range(len(secret_word)):
if secret_word[position] == guess:
display_word[position] = guess
found = True
if not found:
attempts_left -= 1
print(f'You have {attempts_left} attempts left')
print(hangman[max_attempts - attempts_left -1])
print(' '.join(display_word))
if '-' not in display_word:
print(f'\033[32mCongratulations! 🥰 The word is: {secret_word}\033[0m')
game_over = True
if attempts_left == 0:
print(f'\033[31mYou ran out of attempts! You lose! 😢\033[0m')
print(f'The word was: {secret_word}')
game_over = True
الكود بالكامل:
import random
import json
with open("words.json") as f:
words_data = json.load(f)
filtered_words = [word_data for word_data in words_data if "definition" in word_data]
if not filtered_words:
print("No words with definitions found in 'words.json'.")
exit()
print('Welcome to Hangman')
secret_word_data = random.choice(filtered_words)
secret_word = secret_word_data["word"]
display_word = ['-'] * len(secret_word)
definition = secret_word_data["definition"]
print(f"Definition: {definition}")
print(display_word)
game_over = False
max_attempts = 7
attempts_left = max_attempts
hangman = ['''
+---+
| |
|
|
|
|
=========''', '''
+---+
| |
O |
|
|
|
=========''',
]
while not game_over:
guess = input('Guess a letter: ').lower()
found = False
for position in range(len(secret_word)):
if secret_word[position] == guess:
display_word[position] = guess
found = True
if not found:
attempts_left -= 1
print(f'You have {attempts_left} attempts left')
print(hangman[max_attempts - attempts_left -1])
print(' '.join(display_word))
if '-' not in display_word:
print(f'\033[32mCongratulations! 🥰 The word is: {secret_word}\033[0m')
game_over = True
if attempts_left == 0:
print(f'\033[31mYou ran out of attempts! You lose! 😢\033[0m')
print(f'The word was: {secret_word}')
game_over = True
#################################################################
Comments
Post a Comment
Your opinion matters, your voice makes us proud and happy. Your words are our motivation.