السلام عليكم احسن استاد في العالم هادا الكود الخاص بلعبة بلاك جاك شكر علا مجهوداتك :
import random
import os
import time
دالة لتنظيف الشاشة بناءً على نظام التشغيل
def clear_screen():
if os.name == ‘nt’:
os.system(‘cls’) # تنظيف الشاشة في نظام Windows
else:
os.system(‘clear’) # تنظيف الشاشة في نظام Unix-based (مثل macOS و Linux)
شعارات اللعبة
def print_logo():
print(“”"
/\
.’ . '
.
.’ . { } ~-...-||-...-~ || '--
.-~~-.
{ }
.-~-. .-~-.
{ }
.__.'||
..’
||
'–`
_______ _
| | | |
| | __ __ ___ _ __ | | _ _ ___ _ __ ___
| | \ \ /\ / / / _ \ | ’ \ | | | | | | / _ \ | ’ \ / _ \
| | \ V V / | / | | | | | | | || | | () | | | | | | /
|| \/\/ \| || || \| \, | \/ || || \_|
/ |
|/
“”")
def print_divider():
print(“\n” + “=”*50 + “\n”)
القائمة الرئيسية للألعاب
def main_menu():
clear_screen()
games = [“Snake”, “Twenty One”, “?”]
for i, game in enumerate(games, start=1):
print(f"{i}- {game}")
selected_game = input("Select a game by its number: ")
return selected_game
لعبة Twenty One
def twenty_one_game():
clear_screen()
print(“Starting game …”)
time.sleep(2)
print_logo()
player_card1 = random.randint(1, 11)
player_card2 = random.randint(1, 11)
player_hand = [player_card1, player_card2]
player_score = player_card1 + player_card2
print_divider()
print(f"Your cards are {player_hand}, current score is {player_score}")
laptop_card1 = random.randint(1, 11)
laptop_card2 = random.randint(1, 11)
laptop_hand = [laptop_card1, laptop_card2]
laptop_score = laptop_card1 + laptop_card2
print(f"Laptop's visible card is {laptop_hand[0]}")
while player_score < 21:
draw_card = input("Do you want to draw another card? (y/n): ").lower()
if draw_card == 'y':
new_card = random.randint(1, 11)
player_hand.append(new_card)
player_score += new_card
print(f"Your cards are {player_hand}, current score is {player_score}")
print(f"Laptop's visible card is {laptop_hand[0]}")
if player_score > 21:
print("You went over 21! You lose!")
return
else:
break
while laptop_score < 17:
new_card = random.randint(1, 11)
laptop_hand.append(new_card)
laptop_score += new_card
print_divider()
print(f"Your final cards are {player_hand}, final score is {player_score}")
print(f"Laptop's cards are {laptop_hand}, final score is {laptop_score}")
print_divider()
if player_score > 21:
print("You went over 21! You lose!")
elif player_score > laptop_score or laptop_score > 21:
print("You win! 🎉")
elif player_score == laptop_score:
print("It's a tie! 🤝")
else:
print("Laptop wins! 💻")
Main loop
while True:
selected_game = main_menu()
if selected_game == “2”:
twenty_one_game()
else:
print(“Selected game is not implemented yet.”)
play_again = input("Do you want to play another game? (y/n): ").lower()
if play_again != 'y':
break