وعليكم السلام @صفيه الـ
لإنشاء الألعاب في البايثون بالطريقة الأمثل هي بإستخدام مكتبة pygame, لكن مثل هذه اللعبة tkinter ربما يكفي لبساطتها.
هذا هو الكود الكامل للعبة بإستخدام tkinter.
from tkinter import *
from random import randint
score = 0
ghost_door = randint(1,3)
door_num = 0
home = Tk()
home.title("Ghoost game")
title_font = ("Times New Roman",16,"bold","underline")
buttons_font = ("Times New Roman",14,"bold")
home_width = 600
home_height = 500
home.resizable(False,False)
screen_width = home.winfo_screenwidth()
screen_height = home.winfo_screenheight()
home_x = (screen_width // 2) - (home_width // 2)
home_y = ((screen_height // 2)- (home_height // 2))- 100
home.geometry(f"{home_width}x{home_height}+{home_x}+{home_y}")
def check_Ghost(doorNo):
global score
if doorNo == ghost_door:
lower_label['text'] = f"GHOOOOOOST,Run away!\nGame Over , Your Score : {score}"
lower_label.place(x=30,y=400)
else:
lower_label['text'] = f"No Ghost, Gooood,You enter the next rooom"
lower_label.place(x=30,y=400)
score += 1
game_title = Label(home,font = title_font,text = "Three doors ahead..===> Ghost is behind one of them\nChoose a door" )
game_title.place(x=70,y=30)
door_one = Button(home,font = buttons_font,text = "Door one",width = 12,height = 9,bg = "purple",command = lambda door=1 : check_Ghost(door))
door_one.place(x=30,y=140)
door_two = Button(home,font = buttons_font,text = "Door two",width = 12,height = 9,bg = "purple",command = lambda door=2 : check_Ghost(door))
door_two.place(x=200,y=140)
door_three = Button(home,font = buttons_font,text = "Door three",width = 12,height = 9,bg = "purple",command = lambda door=3 : check_Ghost(door))
door_three.place(x=370,y=140)
lower_label = Label(home,font = title_font,text = "" )
home.mainloop()
أطيب تحياتي.