Advertisement
Ralip

Darts

Apr 17th, 2022 (edited)
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 31.67 KB | None | 0 0
  1. from tkinter import *
  2. import os
  3. import random
  4.  
  5. # Класс статистики игрока ==============================================================================================
  6. class Stata:
  7.  
  8.     def __init__(self, name):
  9.         self.name = name  # Постоянный никнейм игрока (возможно, фамилия)
  10.         self.hit = [0] * 21  # Количество попаданий в простые секторы (для каждого сектора (от 1 до 20))
  11.         self.hit2 = [0] * 21  # Количество попаданий в секторы на удвоение (для каждого сектора)
  12.         self.hit3 = [0] * 21  # Количество попаданий в секторы на утроение (для каждого сектора)
  13.         self.hit25 = 0  # Количество попаданий во внешннее кольцо яблочка
  14.         self.hit50 = 0  # Количество попаданий "в яблочко"
  15.         self.hit0 = 0  # Количество промахов
  16.  
  17. # Глобальные переменные
  18. score1 = 999  # Счёт первого игрока
  19. score2 = 999  # Второго
  20. cast = 3  # Игрок, выполняющий текущий бросок (в ходе игры значение меняется с 1 на 2 и обратно)
  21. casting = 0  # Количество брошенных дротиков (0, 1, 2, 3. Когда 3 - обнуляется, а cast изменяется)
  22. gamemode = 301  # Условное название режима игры
  23. POINT = 20  # Сектор, в который попал дротик
  24. ITOGPOINT = 20  # Очки, полученные с учётом попадание на удвоение или утроение
  25. lastX2 = bool # Содержит информация о том, был ли совершён последний бросок на удвоение или нет
  26. score1mem = 999 # Запоминает предыдущее значение счёта первого игрока (для ситуации перебора или отмены хода)
  27. score2mem = 999 # второго
  28. random.seed()
  29. # Основное окно ========================================================================================================
  30. root = Tk()
  31. root.title("Дартс")
  32. root.geometry("1280x996")  # root.wm_state('zoomed')
  33.  
  34. # Окно правил дартса ===================================================================================================
  35. def openRules():
  36.     os.system('PravilaDarts2018.pdf')  # Открыть pdf файл через программу по умолчанию
  37.     # Rules = Toplevel(root)
  38.     # Rules.geometry("600x920")
  39.     # Label(Rules, text="Правила вида спорта "Дартс"").pack()
  40.     # RulesCloseButton = Button(Rules, text="Закрыть", command=Rules.destroy)
  41.     # RulesCloseButton.pack()
  42.     # Rules.grab_set()
  43.  
  44. # frame1 - ввод ника первого игрока ====================================================================================
  45. frame1 = Frame(root, bg='white')
  46. frame1.place(relwidth=0.4, relheight=0.2, relx=0.05, rely=0.02)
  47. Label(frame1, text='Первый игрок', bg='white', padx=5, pady=15, font='Arial 28 bold').pack()
  48. name1 = StringVar()  # Переменная - имя первого игрока
  49. name1.set('Игрок1')
  50. name1pole = Entry(master=frame1, font='Arial 22', width=25, textvariable=name1)
  51. name1pole.pack(padx=5, pady=5)
  52.  
  53. # frame2 - ввод ника второго игрока ====================================================================================
  54. frame2 = Frame(root, bg='white')
  55. frame2.place(relwidth=0.4, relheight=0.2, relx=0.55, rely=0.02)
  56. Label(frame2, text='Второй игрок', bg='white', padx=5, pady=15, font='Arial 28 bold').pack()
  57. name2 = StringVar()  # Переменная - имя второго игрока
  58. name2.set('Игрок2')
  59. name2pole = Entry(master=frame2, font='Arial 22', width=25, textvariable=name2)
  60. name2pole.pack(padx=5, pady=5)
  61.  
  62. # Ограничение длины текста при вводе ника (16 символов):
  63. def name_len_limit(entry_text):
  64.     if len(name1.get()) > 16 or len(name2.get()) > 16:
  65.         entry_text.set(entry_text.get()[:16])
  66.         Player1.config(textvariable=name1)  # Исправление ошибки с неудалением одного  лишнего сим-
  67.         Player2.config(textvariable=name2)  # вола из Лейблов Player1 и Player2
  68.  
  69.  
  70. name1.trace("w", lambda *args: name_len_limit(name1))
  71. name2.trace("w", lambda *args: name_len_limit(name2))
  72.  
  73. # frame3 - Кнопки выбора типа игры =====================================================================================
  74. frame3 = Frame(root, bg='white')
  75. frame3.place(relwidth=0.9, relheight=0.2, relx=0.05, rely=0.24)
  76. Label(frame3, text='Выбор типа игры', bg='white', padx=5, pady=15, font='Arial 28 bold').pack()
  77. frame3_1 = Frame(frame3, bg='white', width=300, height=120)
  78. frame3_1.pack()
  79. Mode301 = Label(master=frame3_1, text=' 301 ', padx=5, pady=15, bd=8, relief='ridge', font='Arial 28 bold')
  80. Mode301.pack(side=LEFT)
  81. Label(frame3_1, text=' ', padx=5, pady=5, bg='white').pack(side=LEFT)  # Промежуток между кнопками
  82. Mode501 = Label(master=frame3_1, text=' 501 ', padx=5, pady=15, bd=8, relief='raised', font='Arial 28')
  83. Mode501.pack(side=LEFT)
  84.  
  85. def clickMode301(event):  # Нажатия "кнопок"
  86.     Mode301.config(relief='ridge', font='Arial 28 bold')
  87.     Mode501.config(relief='raised', font='Arial 28')
  88.     global gamemode
  89.     gamemode = 301
  90.  
  91. def clickMode501(event):
  92.     Mode301.config(relief='raised', font='Arial 28')
  93.     Mode501.config(relief='ridge', font='Arial 28 bold')
  94.     global gamemode
  95.     gamemode = 501
  96.  
  97. Mode301.bind('<Button-1>', clickMode301)
  98. Mode501.bind('<Button-1>', clickMode501)
  99.  
  100. # frame4 - кто начинает игру ===========================================================================================
  101. frame4 = Frame(root, bg='white')
  102. frame4.place(relwidth=0.9, relheight=0.3, relx=0.05, rely=0.46)
  103. Label(frame4, text='Начинает игру', bg='white', padx=5, pady=15, font='Arial 28 bold').pack()
  104. frame4_1 = Frame(frame4, bg='white', width=600, height=120)
  105. frame4_1.pack()
  106. Player1 = Label(master=frame4_1, textvariable=name1, padx=5, pady=15, bd=8, relief='raised', font='Arial 28')
  107. Player1.pack(side=LEFT)
  108. Label(frame4_1, text=' ', padx=5, pady=5, bg='white').pack(side=LEFT)  # Промежуток между кнопками
  109. Player2 = Label(master=frame4_1, textvariable=name2, padx=5, pady=15, bd=8, relief='raised', font='Arial 28')
  110. Player2.pack(side=LEFT)
  111. Label(frame4, text='', padx=2, pady=2, bg='white', font='Arial 2').pack()  # Промежуток между кнопками
  112. RandomPlayer = Label(master=frame4, text='Случайно', padx=5, pady=15, bd=8, relief='ridge', font='Arial 28 bold')
  113. RandomPlayer.pack()
  114.  
  115. def clickPlayer1(event):
  116.     Player1.config(relief='ridge', font='Arial 28 bold')
  117.     Player2.config(relief='raised', font='Arial 28')
  118.     RandomPlayer.config(relief='raised', font='Arial 28')
  119.     global cast
  120.     cast = 1
  121.  
  122. def clickPlayer2(event):
  123.     Player1.config(relief='raised', font='Arial 28')
  124.     Player2.config(relief='ridge', font='Arial 28 bold')
  125.     RandomPlayer.config(relief='raised', font='Arial 28')
  126.     global cast
  127.     cast = 2
  128.  
  129. def clickRandomPlayer(event):
  130.     Player1.config(relief='raised', font='Arial 28')
  131.     Player2.config(relief='raised', font='Arial 28')
  132.     RandomPlayer.config(relief='ridge', font='Arial 28 bold')
  133.     global cast
  134.     cast = 3
  135.  
  136. Player1.bind('<Button-1>', clickPlayer1)
  137. Player2.bind('<Button-1>', clickPlayer2)
  138. RandomPlayer.bind('<Button-1>', clickRandomPlayer)
  139.  
  140. # frame5 - для кнопок меню =============================================================================================
  141. frame5 = Frame(root, bg='white')
  142. frame5.place(relwidth=0.9, relheight=0.2, relx=0.05, rely=0.78)
  143. Label(frame5, text=' ', padx=5, pady=5, bg='white').pack()  ###
  144. frame5_1 = Frame(frame5, bg='red', width=600, height=120)
  145. frame5_1.pack()
  146. Label(frame5, text=' ', padx=15, pady=5, bg='white').pack(side=LEFT)  ###
  147. RulesButton = Button(frame5, text='Правила игры Дартс', font='Arial 18', command=openRules)
  148. RulesButton.pack(side=LEFT)
  149. StataButton = Button(frame5, text='Статистика игроков', font='Arial 18')
  150. Label(frame5, text=' ', padx=15, pady=5, bg='white').pack(side=RIGHT)  ###
  151. StataButton.pack(side=RIGHT)
  152.  
  153. score1Var = StringVar()
  154. score2Var = StringVar()
  155. # Окно остаётся основное, но виджеты новые, предназначенные непосредственно для раунда дартса:
  156. frame21 = Frame(root, bg='white',
  157.                 highlightthickness=4)  # Некоторые виджеты объявлены заранее для одновременного использования
  158. frame22 = Frame(root, bg='white', highlightthickness=4)  # в функции Game и в функции FocusCast
  159. Dartsman1 = Label(frame21, textvariable=name1, bg='white', padx=5, pady=15, font='Arial 28 bold')
  160. Dartsman2 = Label(frame22, textvariable=name2, bg='white', padx=5, pady=15, font='Arial 28 bold')
  161. frame21_1 = Frame(frame21, bg='red', width=180, height=60)
  162. shoot1_1Label = Label(frame21_1, text='', bg='white', padx=3, pady=3, font='Arial 20')
  163. shoot1_1 = Label(frame21_1, bg='white', padx=3, pady=3, font='Arial 20 bold')
  164. frame21_2 = Frame(frame21, bg='red', width=180, height=60)
  165. shoot1_2Label = Label(frame21_2, text='', bg='white', padx=3, pady=3, font='Arial 20')
  166. shoot1_2 = Label(frame21_2, bg='white', padx=3, pady=3, font='Arial 20 bold')
  167. frame21_3 = Frame(frame21, bg='red', width=180, height=60)
  168. shoot1_3Label = Label(frame21_3, text='', bg='white', padx=3, pady=3, font='Arial 20')
  169. shoot1_3 = Label(frame21_3, bg='white', padx=3, pady=3, font='Arial 20 bold')
  170.  
  171. frame22_1 = Frame(frame22, bg='red', width=180, height=60)
  172. shoot2_1Label = Label(frame22_1, text='', bg='white', padx=3, pady=3, font='Arial 20')
  173. shoot2_1 = Label(frame22_1, bg='white', padx=3, pady=3, font='Arial 20 bold')
  174. frame22_2 = Frame(frame22, bg='red', width=180, height=60)
  175. shoot2_2Label = Label(frame22_2, text='', bg='white', padx=3, pady=3, font='Arial 20')
  176. shoot2_2 = Label(frame22_2, bg='white', padx=3, pady=3, font='Arial 20 bold')
  177. frame22_3 = Frame(frame22, bg='red', width=180, height=60)
  178. shoot2_3Label = Label(frame22_3, text='', bg='white', padx=3, pady=3, font='Arial 20')
  179. shoot2_3 = Label(frame22_3, bg='white', padx=3, pady=3, font='Arial 20 bold')
  180.  
  181. frame23 = Frame(root, bg='white', relief='solid')
  182. def FCancel():
  183.     Cancel()
  184. CancelButton = Button(root, text='Отмена', font='Arial 18', command=FCancel)
  185.  
  186. def Game():
  187.     # Скрыть фреймы стартового экрана
  188.     frame1.place_forget()
  189.     frame2.place_forget()
  190.     frame3.place_forget()
  191.     frame4.place_forget()
  192.     frame5.place_forget()
  193.     name1pole.config(state='disabled') #Заблокировать поля
  194.     name2pole.config(state='disabled')
  195.  
  196.     shoot1 = 60
  197.     shoot2 = 60
  198.     shoot3 = 60
  199.  
  200.     # Определение типа игры
  201.     global gamemode
  202.     global score1
  203.     global score2
  204.     global score1mem
  205.     global score2mem
  206.     if gamemode == 301:
  207.         score1 = 301
  208.         score1Var.set('301')
  209.         score2 = 301
  210.         score2Var.set('301')
  211.         score1mem = 301
  212.         score2mem = 301
  213.     elif gamemode == 501:
  214.         score1 = 501
  215.         score1Var.set('501')
  216.         score2 = 501
  217.         score2Var.set('501')
  218.         score1mem = 501
  219.         score2mem = 501
  220.  
  221.     # frame21 - текущий счёт первого игрока=============================================================================
  222.     Label(root, text='').pack()
  223.     CancelButton.pack()
  224.     frame21.place(relwidth=0.4, relheight=0.3, relx=0.05, rely=0.02)
  225.     Dartsman1.pack()
  226.     Dartsman1score = Label(frame21, textvariable=score1Var, bg='white', padx=25, pady=15, font='Arial 28', bd=4,
  227.                            relief='groove')
  228.     Dartsman1score.pack()
  229.  
  230.     frame21_1.pack()
  231.     shoot1_1Label.pack(side=LEFT)
  232.     shoot1_1.pack(side=LEFT)
  233.  
  234.     frame21_2.pack()
  235.     shoot1_2Label.pack(side=LEFT)
  236.     shoot1_2.pack(side=LEFT)
  237.  
  238.     frame21_3.pack()
  239.     shoot1_3Label.pack(side=LEFT)
  240.     shoot1_3.pack(side=LEFT)
  241.  
  242.     # frame22 - текущий счёт второго игрока=============================================================================
  243.     frame22.place(relwidth=0.4, relheight=0.3, relx=0.55, rely=0.02)
  244.     Dartsman2.pack()
  245.     Dartsman2score = Label(frame22, textvariable=score2Var, bg='white', padx=25, pady=15, font='Arial 28', bd=4,
  246.                            relief='groove')
  247.     Dartsman2score.pack()
  248.  
  249.     frame22_1.pack()
  250.     shoot2_1Label.pack(side=LEFT)
  251.     shoot2_1.pack(side=LEFT)
  252.  
  253.     frame22_2.pack()
  254.     shoot2_2Label.pack(side=LEFT)
  255.     shoot2_2.pack(side=LEFT)
  256.  
  257.     frame22_3.pack()
  258.     shoot2_3Label.pack(side=LEFT)
  259.     shoot2_3.pack(side=LEFT)
  260.  
  261.     global cast
  262.     if cast != 1 and cast != 2:
  263.         cast = random.randint(1, 2)
  264.     FocusCast()
  265.  
  266.     # frame23 - кнопки с цифрами====================================================================================
  267.     frame23.place(relwidth=0.9, relheight=0.6, relx=0.05, rely=0.36)
  268.     frame23_1 = Frame(frame23, bg='red', width=900, height=150)
  269.     frame23_1.pack()
  270.     key20 = Label(frame23_1, text=20, bg='red3', fg='white', padx=25, pady=15, font='Arial 40 bold',
  271.                   highlightbackground='#ffffff', highlightthickness=6)
  272.     key20.pack(side=LEFT)
  273.     key20.bind('<Button-1>', Click20)
  274.     key19 = Label(frame23_1, text=19, bg='green', fg='white', padx=25, pady=15, font='Arial 40 bold',
  275.                   highlightbackground='#ffffff', highlightthickness=6)
  276.     key19.pack(side=LEFT)
  277.     key19.bind('<Button-1>', Click19)
  278.     key18 = Label(frame23_1, text=18, bg='red3', fg='white', padx=25, pady=15, font='Arial 40 bold',
  279.                   highlightbackground='#ffffff', highlightthickness=6)
  280.     key18.pack(side=LEFT)
  281.     key18.bind('<Button-1>', Click18)
  282.     key17 = Label(frame23_1, text=17, bg='green', fg='white', padx=25, pady=15, font='Arial 40 bold',
  283.                   highlightbackground='#ffffff', highlightthickness=6)
  284.     key17.pack(side=LEFT)
  285.     key17.bind('<Button-1>', Click17)
  286.     key16 = Label(frame23_1, text=16, bg='green', fg='white', padx=25, pady=15, font='Arial 40 bold',
  287.                   highlightbackground='#ffffff', highlightthickness=6)
  288.     key16.pack(side=LEFT)
  289.     key16.bind('<Button-1>', Click16)
  290.     frame23_2 = Frame(frame23, bg='red', width=900, height=150)
  291.     frame23_2.pack()
  292.     key15 = Label(frame23_2, text=15, bg='green', fg='white', padx=25, pady=15, font='Arial 40 bold',
  293.                   highlightbackground='#ffffff', highlightthickness=6)
  294.     key15.pack(side=LEFT)
  295.     key15.bind('<Button-1>', Click15)
  296.     key14 = Label(frame23_2, text=14, bg='red3', fg='white', padx=25, pady=15, font='Arial 40 bold',
  297.                   highlightbackground='#ffffff', highlightthickness=6)
  298.     key14.pack(side=LEFT)
  299.     key14.bind('<Button-1>', Click14)
  300.     key13 = Label(frame23_2, text=13, bg='red3', fg='white', padx=25, pady=15, font='Arial 40 bold',
  301.                   highlightbackground='#ffffff', highlightthickness=6)
  302.     key13.pack(side=LEFT)
  303.     key13.bind('<Button-1>', Click13)
  304.     key12 = Label(frame23_2, text=12, bg='red3', fg='white', padx=25, pady=15, font='Arial 40 bold',
  305.                   highlightbackground='#ffffff', highlightthickness=6)
  306.     key12.pack(side=LEFT)
  307.     key12.bind('<Button-1>', Click12)
  308.     key11 = Label(frame23_2, text=11, bg='green', fg='white', padx=25, pady=15, font='Arial 40 bold',
  309.                   highlightbackground='#ffffff', highlightthickness=6)
  310.     key11.pack(side=LEFT)
  311.     key11.bind('<Button-1>', Click11)
  312.     frame23_3 = Frame(frame23, bg='red', width=900, height=150)
  313.     frame23_3.pack()
  314.     key10 = Label(frame23_3, text=10, bg='red3', fg='white', padx=24, pady=15, font='Arial 40 bold',
  315.                   highlightbackground='#ffffff', highlightthickness=6)
  316.     key10.pack(side=LEFT)
  317.     key10.bind('<Button-1>', Click10)
  318.     key9 = Label(frame23_3, text=9, bg='green', fg='white', padx=40, pady=15, font='Arial 40 bold',
  319.                  highlightbackground='#ffffff', highlightthickness=6)
  320.     key9.pack(side=LEFT)
  321.     key9.bind('<Button-1>', Click9)
  322.     key8 = Label(frame23_3, text=8, bg='red3', fg='white', padx=40, pady=15, font='Arial 40 bold',
  323.                  highlightbackground='#ffffff', highlightthickness=6)
  324.     key8.pack(side=LEFT)
  325.     key8.bind('<Button-1>', Click8)
  326.     key7 = Label(frame23_3, text=7, bg='red3', fg='white', padx=40, pady=15, font='Arial 40 bold',
  327.                  highlightbackground='#ffffff', highlightthickness=6)
  328.     key7.pack(side=LEFT)
  329.     key7.bind('<Button-1>', Click7)
  330.     key6 = Label(frame23_3, text=6, bg='green', fg='white', padx=40, pady=15, font='Arial 40 bold',
  331.                  highlightbackground='#ffffff', highlightthickness=6)
  332.     key6.pack(side=LEFT)
  333.     key6.bind('<Button-1>', Click6)
  334.     frame23_4 = Frame(frame23, bg='red', width=900, height=150)
  335.     frame23_4.pack()
  336.     key5 = Label(frame23_4, text=5, bg='green', fg='white', padx=40, pady=15, font='Arial 40 bold',
  337.                  highlightbackground='#ffffff', highlightthickness=6)
  338.     key5.pack(side=LEFT)
  339.     key5.bind('<Button-1>', Click5)
  340.     key4 = Label(frame23_4, text=4, bg='green', fg='white', padx=40, pady=15, font='Arial 40 bold',
  341.                  highlightbackground='#ffffff', highlightthickness=6)
  342.     key4.pack(side=LEFT)
  343.     key4.bind('<Button-1>', Click4)
  344.     key3 = Label(frame23_4, text=3, bg='red3', fg='white', padx=40, pady=15, font='Arial 40 bold',
  345.                  highlightbackground='#ffffff', highlightthickness=6)
  346.     key3.pack(side=LEFT)
  347.     key3.bind('<Button-1>', Click3)
  348.     key2 = Label(frame23_4, text=2, bg='red3', fg='white', padx=40, pady=15, font='Arial 40 bold',
  349.                  highlightbackground='#ffffff', highlightthickness=6)
  350.     key2.pack(side=LEFT)
  351.     key2.bind('<Button-1>', Click2)
  352.     key1 = Label(frame23_4, text=1, bg='green', fg='white', padx=40, pady=15, font='Arial 40 bold',
  353.                  highlightbackground='#ffffff', highlightthickness=6)
  354.     key1.pack(side=LEFT)
  355.     key1.bind('<Button-1>', Click1)
  356.     frame23_5 = Frame(frame23, bg='red', width=900, height=150)
  357.     frame23_5.pack()
  358.     key25 = Label(frame23_5, text=25, bg='green', fg='white', padx=51, pady=15, font='Arial 60 bold',
  359.                   highlightbackground='#ffffff', highlightthickness=6)
  360.     key25.pack(side=LEFT)
  361.     key25.bind('<Button-1>', Click25)
  362.     key0 = Label(frame23_5, text=0, bg='black', fg='white', padx=77, pady=15, font='Arial 60 bold',
  363.                  highlightbackground='#ffffff', highlightthickness=6)
  364.     key0.pack(side=LEFT)
  365.     key0.bind('<Button-1>', Click0)
  366.     key50 = Label(frame23_5, text=50, bg='red3', fg='white', padx=51, pady=15, font='Arial 60 bold',
  367.                   highlightbackground='#ffffff', highlightthickness=6)
  368.     key50.pack(side=LEFT)
  369.     key50.bind('<Button-1>', Click50)
  370.  
  371. # Функция смены фокуса на другого игрока (когда один завершил свой бросок и к броску должен приступить второй)
  372. def FocusCast():
  373.     global cast
  374.     global score1
  375.     global score2
  376.     global score1mem
  377.     global score2mem
  378.     if cast == 1:
  379.         frame21.config(bg='white', highlightbackground='#000000')
  380.         frame22.config(bg='gray94', highlightbackground='#f0f0f0')
  381.         Dartsman1.config(bg='white')
  382.         Dartsman2.config(bg='gray94')
  383.         shoot1_1Label.config(bg='white', text='1 бросок...')
  384.         shoot1_2Label.config(bg='white', text='')
  385.         shoot1_3Label.config(bg='white', text='')
  386.         shoot1_1.config(bg='white', text='')
  387.         shoot1_2.config(bg='white', text='')
  388.         shoot1_3.config(bg='white', text='')
  389.         shoot2_1Label.config(bg='gray94')
  390.         shoot2_2Label.config(bg='gray94')
  391.         shoot2_3Label.config(bg='gray94')
  392.         shoot2_1.config(bg='gray94')
  393.         shoot2_2.config(bg='gray94')
  394.         shoot2_3.config(bg='gray94')
  395.         score1mem = score1
  396.     elif cast == 2:
  397.         frame21.config(bg='gray94', highlightbackground='#f0f0f0')
  398.         frame22.config(bg='white', highlightbackground='#000000')
  399.         Dartsman1.config(bg='gray94')
  400.         Dartsman2.config(bg='white')
  401.         shoot1_1Label.config(bg='gray94')
  402.         shoot1_2Label.config(bg='gray94')
  403.         shoot1_3Label.config(bg='gray94')
  404.         shoot1_1.config(bg='gray94')
  405.         shoot1_2.config(bg='gray94')
  406.         shoot1_3.config(bg='gray94')
  407.         shoot2_1Label.config(bg='white', text='1 бросок...')
  408.         shoot2_2Label.config(bg='white', text='')
  409.         shoot2_3Label.config(bg='white', text='')
  410.         shoot2_1.config(bg='white', text='')
  411.         shoot2_2.config(bg='white', text='')
  412.         shoot2_3.config(bg='white', text='')
  413.         score2mem = score2
  414.  
  415. def FocusChange(): #поменять значение переменной фокуса
  416.     global cast
  417.     global casting
  418.     if cast == 1:
  419.         cast = 2
  420.     else:
  421.         cast = 1
  422.     casting = 0
  423.  
  424. def Click20(Event): ShootWindow(20)
  425. def Click19(Event): ShootWindow(19)
  426. def Click18(Event): ShootWindow(18)
  427. def Click17(Event): ShootWindow(17)
  428. def Click16(Event): ShootWindow(16)
  429. def Click15(Event): ShootWindow(15)
  430. def Click14(Event): ShootWindow(14)
  431. def Click13(Event): ShootWindow(13)
  432. def Click12(Event): ShootWindow(12)
  433. def Click11(Event): ShootWindow(11)
  434. def Click10(Event): ShootWindow(10)
  435. def Click9(Event): ShootWindow(9)
  436. def Click8(Event): ShootWindow(8)
  437. def Click7(Event): ShootWindow(7)
  438. def Click6(Event): ShootWindow(6)
  439. def Click5(Event): ShootWindow(5)
  440. def Click4(Event): ShootWindow(4)
  441. def Click3(Event): ShootWindow(3)
  442. def Click2(Event): ShootWindow(2)
  443. def Click1(Event): ShootWindow(1)
  444. def Click25(Event): ShootWindow(25)
  445. def Click50(Event): ShootWindow(50)
  446. def Click0(Event): ShootWindow(0)
  447. #
  448. pointsVar = StringVar()
  449.  
  450. def ShootWindow(points):
  451.     global POINT
  452.     global ITOGPOINT
  453.     global score1
  454.     global score2
  455.     global cast
  456.     global casting
  457.     global score1mem
  458.     global score2mem
  459.     POINT = points
  460.     ClickX1() # По-умолчанию X1.
  461.     ShootWindow = Toplevel(root)
  462.     x = (root.winfo_screenwidth() - root.winfo_reqwidth()) / 2 - 250  # Чтобы поместить окошко в центре экрана
  463.     y = (root.winfo_screenheight() - root.winfo_reqheight()) / 2 - 175
  464.     ShootWindow.geometry("500x350")
  465.     ShootWindow.wm_geometry("+%d+%d" % (x, y))
  466.     ShootWindow.resizable(False, False)
  467.     FrameShootWindow = Frame(ShootWindow, width=500, height=120)
  468.     FrameShootWindow.pack()
  469.     NumberLabel = Label(FrameShootWindow, text=(str(points) + ' ×'), font='Arial 60', padx=5)
  470.     NumberLabel.pack(side=LEFT)
  471.     ItogLabel = Label(ShootWindow, textvariable=pointsVar, font='Arial 80 bold', fg='red3', pady=10)
  472.     ItogLabel.pack()
  473.     ButtonX1 = Button(FrameShootWindow, text='1', font='Arial 45 bold', command=ClickX1)
  474.     ButtonX1.pack(side=LEFT)
  475.     ButtonX2 = Button(FrameShootWindow, text='2', font='Arial 45 bold', command=ClickX2)
  476.     ButtonX2.pack(side=LEFT)
  477.     ButtonX3 = Button(FrameShootWindow, text='3', font='Arial 45 bold', command=ClickX3)
  478.     ButtonX3.pack(side=LEFT)
  479.     if points == 25 or points == 50 or points == 0: # Для этих вариантов попаданий должно быть окно без умножений.
  480.         FrameShootWindow.pack_forget()
  481.         ShootWindow.geometry("500x250")
  482.  
  483.     def ShootWindowDestroy():  # При закрытии этого окна:
  484.         global casting
  485.         global cast
  486.  
  487.         casting = casting + 1
  488.         if cast == 1: # Если ходит первый игрок
  489.             global score1
  490.             if score1 == ITOGPOINT and lastX2 == 1: # Обработка победы
  491.                 Win(1)
  492.                 ShootWindow.destroy()
  493.                 return
  494.             if score1 - ITOGPOINT < 2: # Обработка перебора
  495.                 if casting == 1:
  496.                     shoot1_1Label.config(text='1 бросок:')
  497.                     shoot1_1.config(text=('ПЕРЕБОР                '))
  498.                 elif casting == 2:
  499.                     shoot1_2Label.config(text='2 бросок:')
  500.                     shoot1_2.config(text=('ПЕРЕБОР                '))
  501.                 elif casting == 3:
  502.                     shoot1_3Label.config(text='3 бросок:')
  503.                     shoot1_3.config(text=('ПЕРЕБОР                '))
  504.                 score1 = score1mem
  505.                 score1Var.set(score1)
  506.                 FocusChange()
  507.                 FocusCast()
  508.                 ShootWindow.destroy()
  509.                 return # Ситуация перебора преждевременно заканчивает функцию
  510.             score1 = score1 - ITOGPOINT
  511.             score1Var.set(score1)
  512.             if casting == 1: # Если совершён первый бросок (первым игроком)
  513.                 shoot1_1Label.config(text='1 бросок:')
  514.                 shoot1_1.config(text=(str(ITOGPOINT) + '               ')) # Пробелы - сдвиг текста влево
  515.                 shoot1_2Label.config(text='2 бросок...')
  516.             if casting == 2: # Если совершён второй бросок...
  517.                 shoot1_2Label.config(text='2 бросок:')
  518.                 shoot1_2.config(text=(str(ITOGPOINT) + '                '))
  519.                 shoot1_3Label.config(text='3 бросок...')
  520.             if casting == 3:
  521.                 shoot1_3Label.config(text='3 бросок:')
  522.                 shoot1_3.config(text=(str(ITOGPOINT) + '                '))
  523.         if cast == 2: # Если ходит второй игрок
  524.             global score2
  525.             if score2 == ITOGPOINT and lastX2 == 1: # Обработка победы
  526.                 Win(2)
  527.                 ShootWindow.destroy()
  528.                 return
  529.             if score2 - ITOGPOINT < 2: # Обработка перебора
  530.                 if casting == 1:
  531.                     shoot2_1Label.config(text='1 бросок:')
  532.                     shoot2_1.config(text=('ПЕРЕБОР                '))
  533.                 elif casting == 2:
  534.                     shoot2_2Label.config(text='2 бросок:')
  535.                     shoot2_2.config(text=('ПЕРЕБОР                '))
  536.                 elif casting == 3:
  537.                     shoot2_3Label.config(text='3 бросок:')
  538.                     shoot2_3.config(text=('ПЕРЕБОР                '))
  539.                 score2 = score2mem
  540.                 score2Var.set(score2)
  541.                 FocusChange()
  542.                 FocusCast()
  543.                 ShootWindow.destroy()
  544.                 return # Ситуация перебора преждевременно заканчивает функцию
  545.             score2 = score2 - ITOGPOINT
  546.             score2Var.set(score2)
  547.             if casting == 1:
  548.                 shoot2_1Label.config(text='1 бросок:')
  549.                 shoot2_1.config(text=(str(ITOGPOINT) + '                '))
  550.                 shoot2_2Label.config(text='2 бросок...')
  551.             if casting == 2:
  552.                 shoot2_2Label.config(text='2 бросок:')
  553.                 shoot2_2.config(text=(str(ITOGPOINT) + '                '))
  554.                 shoot2_3Label.config(text='3 бросок...')
  555.             if casting == 3:
  556.                 shoot2_3Label.config(text='3 бросок:')
  557.                 shoot2_3.config(text=(str(ITOGPOINT) + '                '))
  558.         if casting == 3: # Если совершён третий бросок, то игрока нужно сменить.
  559.             FocusChange()
  560.             FocusCast()
  561.         ShootWindow.destroy()
  562.     def onClick(X):
  563.         if X.char == '1':
  564.             ClickX1()
  565.         if X.char == '2':
  566.             ClickX2()
  567.         if X.char == '3':
  568.             ClickX3()
  569.     def ClickEnter(Event): # Функция при нажатии Энтера
  570.         ShootWindowDestroy() # Вызывает закрытие окна
  571.     ShootWindow.bind('<Return>', ClickEnter) # Забита на нажатие энтера во всём окне.
  572.     ShootWindow.bind('<KeyPress>', onClick)
  573.     ButtonFix = Button(ShootWindow, text='Зафиксировать попадание', font='Arial 24', command=ShootWindowDestroy)
  574.     if points == 0: ButtonFix.config(text='Зафиксировать промах')
  575.     ButtonFix.pack()
  576.     ShootWindow.focus_set()
  577.     ShootWindow.grab_set()
  578.  
  579. def Cancel(): # Отмена последнего хода
  580.     global casting
  581.     global cast
  582.     global score1
  583.     global score2
  584.     global score1mem
  585.     global score2mem
  586.     if casting == 0:
  587.         if cast == 1:
  588.             score2 = score2mem
  589.             score2Var.set(score2)
  590.         if cast == 2:
  591.             score1 = score1mem
  592.             score1Var.set(score1)
  593.         FocusChange()
  594.         FocusCast()
  595.     else:
  596.         casting = 0
  597.         if cast == 1:
  598.             score1 = score1mem
  599.             score1Var.set(score1)
  600.         if cast == 2:
  601.             score2 = score2mem
  602.             score2Var.set(score2)
  603.         FocusCast()
  604.  
  605. def ClickX1():
  606.     global POINT
  607.     global ITOGPOINT
  608.     global lastX2
  609.     if POINT == 50: # Если попадание в булл, то оно тоже засчитывается как x2
  610.         lastX2 = 1
  611.     else:
  612.         lastX2 = 0
  613.     ITOGPOINT = POINT
  614.     pointsVar.set(str(ITOGPOINT))
  615.  
  616. def ClickX2():
  617.     global POINT
  618.     global ITOGPOINT
  619.     global lastX2
  620.     lastX2 = 1
  621.     ITOGPOINT = POINT * 2
  622.     pointsVar.set(str(ITOGPOINT))
  623.  
  624. def ClickX3():
  625.     global POINT
  626.     global ITOGPOINT
  627.     global lastX2
  628.     lastX2 = 0
  629.     ITOGPOINT = POINT * 3
  630.     pointsVar.set(str(ITOGPOINT))
  631.  
  632. StartButton = Button(frame5_1, text='Начать раунд', font='Arial 28', command=Game)
  633. StartButton.pack()
  634.  
  635. def Win(who):
  636.     def StopGame():
  637.         # Скрыть все фреймы игры:
  638.         frame21.place_forget()
  639.         frame22.place_forget()
  640.         frame23.place_forget()
  641.         # Вернуть фреймы меню:
  642.         frame1.place(relwidth=0.4, relheight=0.2, relx=0.05, rely=0.02)
  643.         frame2.place(relwidth=0.4, relheight=0.2, relx=0.55, rely=0.02)
  644.         frame3.place(relwidth=0.9, relheight=0.2, relx=0.05, rely=0.24)
  645.         frame4.place(relwidth=0.9, relheight=0.3, relx=0.05, rely=0.46)
  646.         frame5.place(relwidth=0.9, relheight=0.2, relx=0.05, rely=0.78)
  647.         name1pole.config(state='normal')
  648.         name2pole.config(state='normal')
  649.         global casting
  650.         casting = 0
  651.         WinWindow.destroy()
  652.     WinWindow = Toplevel(root)
  653.     x = (root.winfo_screenwidth() - root.winfo_reqwidth()) / 2 - 400  # Чтобы поместить окошко в центре экрана
  654.     y = (root.winfo_screenheight() - root.winfo_reqheight()) / 2 - 100 - 200
  655.     WinWindow.geometry("800x200")
  656.     WinWindow.wm_geometry("+%d+%d" % (x, y))
  657.     WinWindow.resizable(False, False)
  658.     FrameWinWindow = Frame(WinWindow, width=800, height=50)
  659.     FrameWinWindow.pack()
  660.     Label(FrameWinWindow, text=('Победитель: '), font='Arial 30', pady=25, padx=5).pack(side=LEFT)
  661.     Winner = Label(FrameWinWindow, textvariable=name1, font='Arial 30 bold',pady=25,padx=5)
  662.     if who == 2: Winner.config(textvariable=name2)
  663.     Winner.pack(side=LEFT)
  664.     ButtonMenu = Button(WinWindow, text='       ОК       ', font='Arial 24', command=StopGame)
  665.     ButtonMenu.pack()
  666.     WinWindow.focus_set()
  667.     WinWindow.grab_set()
  668.  
  669. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement