Python Tkinter (GUI) da oddiy o'yin yozish

Python Tkinter (GUI) da oddiy o'yin yozish

Python Dasturlash Maktabi
Tkinterda-oddiy o'yin yasash

Bugun biz taniqli tosh, qaychi, qog'oz o'yinini yaratamiz. Python tili va tkinter kutubxonasi bu borada bizga yordam beradi, Buni qanday bajarishni bilmasangiz, ushbu maqolani o'qib chiqishingizni maslahat beraman.

Tosh🤜, Qaychi✌️, Qog'oz🤚


Bizga kerak bo'lgan birinchi narsa - bu boshlang'ich tuzilish, menyu, u shunday ko'rinishda bo'ladi:

from tkinter import *
import random as rdm


class Asosiy(Frame):
def __init__(self, root):
super(Asosiy, self).__init__(root)
self.startUI()

def startUI(self):
pass

if __name__ == '__main__':
root = Tk()
root.geometry("430x160+200+200")
root.title("Tosh, Qaychi, Qog'oz (@Uz_Python)")
root.resizable(False, False)
root["bg"] = "#FFF"
app = Asosiy(root)
app.pack()
root.mainloop()


Bu erda biz tosh, qaychi, qog'oz sarlavhasi va oq fon bilan 500 dan 500 gacha o'zgarmas oynani yaratamiz. Aynan shu oynada biz tugmachalarni, hisoblagichlarni va boshqalarni qo'shamiz.

Endi startUI uslubimizga quyidagi qatorlarni qo'shing:


Tugma = Button(root, text="Tosh", font=("Times New Roman", 15))
Tugma2 = Button(root, text="Qaychi", font=("Times New Roman", 15))
Tugma3 = Button(root, text="Qog'oz", font=("Times New Roman", 15))

Tugma.place(x=10, y=100, width=120, height=50)
Tugma2.place(x=155, y=100, width=120, height=50)
Tugma3.place(x=300, y=100, width=120, height=50)


Ushbu 7 qator bizning oynamizga hech narsa qilmaydigan 3 tugmachani qo'shadi.



Foydalanuvchi o'z tanlovini 3 tugmachadan birini bosish orqali amalga oshiradi, bu juda zo'r, ammo bizga tasodifiy modul uchun mo'ljallangan raqib kerak.


Va endi biz tanlovni qayta ishlaydigan va ushbu turda kim g'olib bo'lganiga javob beradigan funktsiyani qo'shamiz. Buni shunday qilamiz:


tugma = Button(root, text="Tosh🤜", font=("Times New Roman", 15),
command=lambda x=1: self.tugma_click(x))
tugma2 = Button(root, text="Qaychi✌️", font=("Times New Roman", 15),
command=lambda x=2: self.tugma_click(x))
tugma3 = Button(root, text="Qog'oz🤚", font=("Times New Roman", 15),
command=lambda x=3: self.tugma_click(x))


Bu yerda nima bo'layapti ?


Hammasi juda oddiy. Taxminan aytganda, agar o'yinchi toshga urilsa, 1, qaychi bo'lsa, keyin 2, agar qog'oz bo'lsa, u holda 3 ketadi va consolda ham ko'rsatiladi.


Mantiqni bajarishdan oldin, natijani o'yinchiga yetkazishimiz kerak va buning uchun biz Label-dan foydalanamiz . Uni boshlash uchun quyidagi satrlarni qo'shamiz :


self.lbl = Label(root, text="O'yinni boshlang!", 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"G'alaba: {self.win}\nMag'lubiyat:"
f" {self.lose}\nDurang: {self.drow}",
bg="#FFF")
self.lbl2.place(x=5, y=5)


Yaxshi. Endi biz tur natijalarini statistik ma'lumotlar bilan yozib qo'yamiz.


Keling, 3 ta hisoblagich qilamiz:


  1. Mag'lubiyat

  2. G'alaba

  3. Durang


Buning uchun bir xil startUI- ga quyidagi qatorni qo'shing:


self.win = self.drow = self.lose = 0


Endi asosiy sinfda btn_click usulini yarating va unga quyidagi satrlarni yozing:


def tugma_click(self, choise):
comp_choise = rdm.randint(1, 3)
print(choise)


va quyidagi satrlarni yozing:


if choise == comp_choise:
self.drow += 1
self.lbl.configure(text="Durang")
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="G'alaba")
else:
self.lose += 1
self.lbl.configure(text="Mag'lubiyat")

self.lbl2.configure(text=f"G'alaba: {self.win}\nMag'lubiyat:"
f" {self.lose}\nDurang: {self.drow}")

del comp_choise


Va nihoyat, barchasi shu yerda tugadi. Hammasi ishlaydi, siz o'ynashingiz mumkin.


To'liq kod:


from tkinter import *
import random as rdm
"""@uz_python telegram channel"""

class Asosiy(Frame):
def __init__(self, root):
super(Asosiy, self).__init__(root)
self.startUI()

def startUI(self):
tugma = Button(root, text="Tosh🤜", font=("Times New Roman", 15),
command=lambda x=1: self.tugma_click(x))
tugma2 = Button(root, text="Qaychi✌️", font=("Times New Roman", 15),
command=lambda x=2: self.tugma_click(x))
tugma3 = Button(root, text="Qog'oz🤚", font=("Times New Roman", 15),
command=lambda x=3: self.tugma_click(x))

tugma.place(x=10, y=100, width=120, height=50)
tugma2.place(x=155, y=100, width=120, height=50)
tugma3.place(x=300, y=100, width=120, height=50)

self.lbl = Label(root, text="O'yinni boshlang!", 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"G'alaba: {self.win}\nMag'lubiyat:"
f" {self.lose}\nDurang: {self.drow}",
bg="#FFF")
self.lbl2.place(x=5, y=5)

def tugma_click(self, choise):
comp_choise = rdm.randint(1, 3)

if choise == comp_choise:
self.drow += 1
self.lbl.configure(text="Durang")
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="G'alaba")
else:
self.lose += 1
self.lbl.configure(text="Mag'lubiyat")

self.lbl2.configure(text=f"G'alaba: {self.win}\nMag'lubiyat:"
f" {self.lose}\nDurang: {self.drow}")

del comp_choise


if __name__ == '__main__':
root = Tk()
root.geometry("430x160+200+200")
root.title("Tosh, Qaychi, Qog'oz (@Uz_Python)")
root.resizable(False, False)
root["bg"] = "#FFF"
app = Asosiy(root)
app.pack()
root.mainloop()



Report Page