Pythonda tkinter kutubxonasidan foydalanib sodda rang o'yinini yaratish
#Kutubxonalarni import qilamiz
from tkinter import *
import random
#oyna uchun namuna
root = Tk ()
#sarlavha
root.title ("rang o'yini ")
#ishchi oynaning o'lchami
root.geometry ("650x450+300+90")
#fonni ishga tushirish va sozlash
#fon uchun rang tanlash
bg = "pink"
root.config (bg = bg)
root.resizable (False, False)
entry = StringVar ()
#o'zgaruvchilarni ishga tushirish
count_ = total = 0
time = 3
#o'yin sarlavhasi
title = Label (root, text = "Ranglar o'yini", font = ("ariel", 30, "underline bold"), width = 0, bg = "pink", fg="black")
title.place (x = 205, y = 25)
#boshlash funksiyasi o'yinni boshlash uchun
def start ():
global time
btn. config (state = DISABLED)
#vaqtni orqaga hisoblovchi funksiya
def count_down ():
global time, total, count_
if time >=0:
countdown.
config (text = time)
time = time - 1
#vaqtni yangilash uchun countdown funksiyasiga murojat qilish
countdown.after (1000, count_down)
#teglar uchun fon rangini sozlash
timeleftlbl.config (bg = bg)
countdown.config (bg = bg)
scorelbl.config (bg = bg)
count.config (text = count_, bg = bg)
totallbl.config (text = total, bg = bg)
outof.config (text = "/", bg = bg)
else
:
time = 10
count1 = count_
total = count_ = 0
#o'yin tugadi oynasi
new_root = Toplevel ()
#sarlavha
new_root.title (" Ranglar o'yini ")
#oyna o'lchami
new_root.geometry ("650x450+300+90")
new_root.config (bg = "black")
#oynani yashirish
root.withdraw ()
#game over oynasi teglari
game_over = Label (new_root, text = "O'yin tugadi ", font = ("ariel", 40, "bold"), width = 0)
game_over.place (x = 205, y = 120)
natija = Label (new_root, text = "NATIJANGIZ", font = ("ariel", 20, "bold"), width = 10)
natija.place (x =205, y = 40)
natija1 = Label (new_root, text = count1, font = ("ariel", 20, "bold"), width = 10)
natija1.place (x = 425, y = 40)
#o'yinni qayta boshlash uchun funksiya
def replay (event = None):
#new_root oynasini yashirish
new_root.withdraw ()
#root oynasini ko'rsatish
root.deiconify ()
#
e.focus ()
#boshlash funksiyasini chaqirish
start ()
#o'yinni qayta boshlash tugmasi
Play_again = Button (new_root, text = "Qata boshlash", font =
("ariel", 20, "bold"), bg = "black", fg =
"gold", width = 0, relief = RIDGE, command =
replay)
Play_again.place (x = 250, y = 200)
#enter tugmasini new_root oynasiga bog'lash
new_root.bind ("", replay)
count_down ()
def changes ():
global
bg
#ranglar majmui
colors =["red", "green", "blue", "yellow", "pink", "brown"]
#tasodifiy rangni tanlash
bg = random.choice (colors)
#yangi tasodifiy rangni fon va lebellar uchun sozlash
root.config (bg = bg)
lbl.config (bg = bg)
title.config (bg = bg)
timeleftlbl.config (bg = bg)
countdown.config (bg = bg)
scorelbl.config (bg = bg)
count.config (text = count_, bg = bg)
totallbl.config (text = total, bg = bg)
outof.config (text = "/", bg = bg)
entry.set ("")
def scores (event = None):
global
count_,
total
#rang to'g'ri kiritilsa hisoblashni va fonni yangilash
if
entry.
get ()
== bg:
count_ = count_ + 1
total = total + 1
changes ()
#rang noto'g'ri kiritilsa fonni yangilash
else
:
total = total + 1
changes ()
btn1.config (command = scores)
#kiritilgan rangni tasdiqlash
root.bind ("", scores)
#o'yinni boshlash tugmasi
btn = Button (root, text = "O'yinni boshlash", font =
("ariel", 20, "bold"), bg = "black", fg =
"gold2", width = 15, relief = RIDGE, command =
start)
btn.place (x = 200, y = 100)
#qolgan vaqtni ko'rsatuvhi nishon
timeleftlbl = Label (root, text = "Qolgan vaqt : ", font =
("ariel", 15, "bold"), height = 2, bg =
bg)
timeleftlbl.place (x = 55, y = 165)
#umumiy vaqt
countdown = Label (root, font = ("ariel", 15, "bold"), height =
2, bg = bg)
countdown.place (x = 175, y = 165)
#umumiy natijani ko'rsatuvchi
scorelbl = Label (root, text = "Umumiy natijangiz: ", font =
("ariel", 15, "bold"), height = 2, bg =
bg)
scorelbl.place (x = 345, y = 165)
#to'g'ri kiritilganlar soni
count = Label (root, font = ("ariel", 15, "bold"), height = 2, bg =
bg)
count.place (x = 550, y = 165)
#label to display "/" symbol
outof = Label (root, font = ("ariel", 15, "bold"), height = 2, bg =
bg)
outof.place (x = 575, y = 165)
#to'g'ri va noto'g'ri kiritilgan ranglarning umumiy natijasi
totallbl = Label (root, font = ("ariel", 15, "bold"), height =
2, bg = bg)
totallbl.place (x = 590, y = 165)
#rang nomini kiritish labeli
lbl = Label (root, text = "Rang nomini kiritish: ", width =
16, font = ("ariel", 15, "bold"), bg = "pink", fg =
"black")
lbl.place (x = 115, y = 295)
#kirituvchi qism
e = Entry (root, textvariable = entry, width = 15, font = ("ariel", 15, "bold"))
e.place (x = 340, y =295)
e.focus ()
#kiritilgan rangni tasdiqlash tugmasi
btn1 = Button (root, text = "Tasdiqlash", font = ("ariel", 18, "bold"), bg = "black", fg ="gold2", width = 10, relief = RIDGE)
btn1.place (x =245, y = 380)
root.mainloop ()
Do'stlaringiz bilan baham: |