اليوم سنقوم بإنشاء لعبة معروفة من الحجر والمقص والورق. ستساعدنا مكتبة بايثون بايثون و tkinter في هذا ، لكن إذا كنت لا تعرف ما هو عليه ، فإنني أنصحك بقراءة هذا المقال.
أول ما نحتاج إليه هو بنية أولية ، نافذة ، ستبدو هكذا بالنسبة لي:
from tkinter import * import random as rdm class Main(Frame): def __init__(self, root): super(Main, self).__init__(root) self.startUI() def startUI(self): pass if __name__ == '__main__': root = Tk() root.geometry("500x500+200+200") root.title(", , ") root.resizable(False, False) root["bg"] = "#FFF" app = Main(root) app.pack() root.mainloop()
هنا نقوم بإنشاء نافذة ثابتة من 500 إلى 500 بعنوان "الحجر والمقص والورق" وخلفية بيضاء. سنضيف في هذه النافذة الأزرار والعدادات ، إلخ.
الآن في طريقة startUI لدينا أضف الأسطر التالية :
btn = Button(root, text="", font=("Times New Roman", 15)) btn = Button(root, text="", font=("Times New Roman", 15)) btn3 = Button(root, text="", font=("Times New Roman", 15)) btn.place(x=10, y=100, width=120, height=50) btn2.place(x=155, y=100, width=120, height=50) btn3.place(x=300, y=100, width=120, height=50)
هذه الخطوط السبعة ستضيف 3 أزرار إلى نافذتنا التي لا تفعل شيئًا. سوف نصلح هذا لاحقا.
يقوم المستخدم باختياره من خلال النقر على أحد الأزرار الثلاثة ، هذا أمر رائع ، لكننا نحتاج إلى خصم ، وهذا ما تريده الوحدة النمطية العشوائية.
والآن سنضيف وظيفة تتولى الاختيار ، ونقدم إجابة من فاز في هذه الجولة. دعونا نفعل ذلك مثل هذا:
btn = Button(root, text="", font=("Times New Roman", 15), command=lambda x=1: self.btn_click(x)) btn2 = Button(root, text="", font=("Times New Roman", 15), command=lambda x=2: self.btn_click(x)) btn3 = Button(root, text="", font=("Times New Roman", 15), command=lambda x=3: self.btn_click(x))
ما الذي يحدث هنا؟
كل شيء بسيط جدا. تحدث تقريبًا ، إذا ضغط أحد اللاعبين على حجر ، فسيذهب 1 ، وإذا كان هناك مقص ، ثم 2 ، وإذا كان الورق ، ثم 3 ، ولن يذهب فقط ، ولكن سيتم عرضه أيضًا في وحدة التحكم.
على حساب الكمبيوتر. انه يجعل اختياره ، ولكن اختياره لا يذهب إلى أي مكان.
قبل القيام بالمنطق ، نحتاج إلى تمرير النتيجة إلى اللاعب ، ولهذا سنستخدم العلامة . أضف الأسطر التالية إلى startUI :
self.lbl = Label(root, text=" !", bg="#FFF", font=("Times New Roman", 21, "bold")) self.lbl.place(x=120, y=25) self.lbl2 = Label(root, justify="left", font=("Times New Roman", 13), text=f": {self.win}\n:" f" {self.lose}\n: {self.drow}", bg="#FFF") self.lbl2.place(x=5, y=5)
ممتاز. الآن لدينا نقش نخرج فيه بنتيجة الجولة ونقش بالإحصائيات.
لنجعل 3 عدادات:
1. الخسائر
2. الانتصارات
3. لا شيء
للقيام بذلك ، أضف السطر التالي إلى startUI نفسه:
self.win = self.drow = self.lose = 0
الآن ، في الفئة الرئيسية ، قم بإنشاء طريقة btn_click ، واكتب الأسطر التالية فيه:
def btn_click(self, choise): comp_choise = rdm.randint(1, 3) print(choise)
الموسيقى لم تلعب طويلا. في نفس المكان ، في btn_click ، احذف
طباعة (كويس)
واكتب هذه السطور:
if choise == comp_choise: self.drow += 1 self.lbl.configure(text="") elif choise == 1 and comp_choise == 2 \ or choise == 2 and comp_choise == 3 \ or choise == 3 and comp_choise == 1: self.win += 1 self.lbl.configure(text="") else: self.lose += 1 self.lbl.configure(text="") self.lbl2.configure(text=f": {self.win}\n:" f" {self.lose}\n: {self.drow}") del comp_choise
في الواقع كل شيء ، هذه هي نهاية الخلق. كل شيء يعمل ، يمكنك اللعب.
كود كامل:
from tkinter import * import random as rdm class Main(Frame): def __init__(self, root): super(Main, self).__init__(root) self.startUI() def startUI(self): btn = Button(root, text="", font=("Times New Roman", 15), command=lambda x=1: self.btn_click(x)) btn2 = Button(root, text="", font=("Times New Roman", 15), command=lambda x=2: self.btn_click(x)) btn3 = Button(root, text="", font=("Times New Roman", 15), command=lambda x=3: self.btn_click(x)) btn.place(x=10, y=100, width=120, height=50) btn2.place(x=155, y=100, width=120, height=50) btn3.place(x=300, y=100, width=120, height=50) self.lbl = Label(root, text=" !", bg="#FFF", font=("Times New Roman", 21, "bold")) self.lbl.place(x=150, y=25) self.win = self.drow = self.lose = 0 self.lbl2 = Label(root, justify="left", font=("Times New Roman", 13), text=f": {self.win}\n:" f" {self.lose}\n: {self.drow}", bg="#FFF") self.lbl2.place(x=5, y=5) def btn_click(self, choise): comp_choise = rdm.randint(1, 3) if choise == comp_choise: self.drow += 1 self.lbl.configure(text="") elif choise == 1 and comp_choise == 2 \ or choise == 2 and comp_choise == 3 \ or choise == 3 and comp_choise == 1: self.win += 1 self.lbl.configure(text="") else: self.lose += 1 self.lbl.configure(text="") self.lbl2.configure(text=f": {self.win}\n:" f" {self.lose}\n: {self.drow}") del comp_choise if __name__ == '__main__': root = Tk() root.geometry("430x160+200+200") root.title(", , ") root.resizable(False, False) root["bg"] = "#FFF" app = Main(root) app.pack() root.mainloop()