Guest User

Untitled

a guest
Jun 13th, 2020
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.52 KB | None | 0 0
  1. from tkinter import *
  2. from tkinter import messagebox
  3. from random import choice
  4.  
  5. def main():
  6. global root
  7. root = Tk()
  8. root.title("Настройка")
  9. root.geometry(f"{700}x{750}+{int(root.winfo_screenwidth() // 2 - 350)}+{int(root.winfo_screenheight() // 2 - 375)}")
  10. root["bg"] = "gold"
  11. root.resizable(False, False)
  12. root.focus_get()
  13. root.protocol('WM_DELETE_WINDOW', close_game)
  14. mainSetting()
  15. mainloop()
  16.  
  17.  
  18.  
  19. def close_game():
  20. root.quit()
  21.  
  22.  
  23. def type_var():
  24. if var_r.get() == 1:
  25. text8.place_forget()
  26. text3.place(x=350, y=310, anchor=CENTER)
  27. text4.place(x=300, y=350, anchor=CENTER)
  28. text5.place(x=450, y=350, anchor=CENTER)
  29. entry1.place(x=350, y=350, anchor=CENTER)
  30. text6.place(x=283, y=380, anchor=CENTER)
  31. entry2.place(x=350, y=380, anchor=CENTER)
  32. text7.place(x=450, y=380, anchor=CENTER)
  33. if var_r.get() == 2:
  34. text3.place_forget()
  35. text4.place_forget()
  36. text5.place_forget()
  37. entry1.place_forget()
  38. text6.place_forget()
  39. entry2.place_forget()
  40. text7.place_forget()
  41. text8.place(x=350, y=310, anchor=CENTER)
  42.  
  43.  
  44. def check_error():
  45. global errorlog, setting, n
  46. if setting:
  47. if len(entry0.get()) >= 32:
  48. errorentry0 = "Nickname: больше 32 символов\n"
  49. entry0["bg"] = "pink"
  50. elif len(entry0.get()) == 0:
  51. errorentry0 = "Nickname: Пустое поле\n"
  52. entry0["bg"] = "pink"
  53. else:
  54. errorentry0 = ""
  55. entry0["bg"] = "white"
  56. if entry1.get().isdigit():
  57. if int(entry1.get()) >= 1 and int(entry1.get()) <= 10:
  58. errorentry1 = ""
  59. entry1["bg"] = "white"
  60. else:
  61. errorentry1 = f"Speed: {entry1.get()} не входит в диапозон от 1 до 10\n"
  62. entry1["bg"] = "pink"
  63. else:
  64. errorentry1 = f"Speed: {entry1.get()} не является числом\n"
  65. entry1["bg"] = "pink"
  66. if entry2.get().isdigit():
  67. if int(entry2.get()) >= 10 and int(entry2.get()) <= 40:
  68. errorentry2 = ""
  69. entry2["bg"] = "white"
  70. else:
  71. errorentry2 = f"Size board: {entry2.get()} не входит в диапозон от 10 до 40\n"
  72. entry2["bg"] = "pink"
  73. else:
  74. errorentry2 = f"Size board: {entry2.get()} не является числом\n"
  75. entry2["bg"] = "pink"
  76. errorlog = errorentry0 + errorentry1 + errorentry2
  77. n = entry2.get()
  78. root.after(1, check_error)
  79.  
  80.  
  81. def mainSetting():
  82. global setting, var_r, entry0, entry1, entry2, text3, text4, text5, text6, text7, text8, a, canvas
  83. setting = True
  84. text0 = created_widget(root, "Игрок", 'Arial 18 bold', None, "gold", 350, 100, CENTER, None, None, None, None, None,
  85. "Label")
  86. entry0 = created_widget(root, None, 'Arial 10 italic', None, "white", 350, 130, CENTER, 3, 32, 1, "No Name", None,
  87. "Entry")
  88. entry0.focus_set()
  89. text1 = created_widget(root, "*Позволяет ввести любое имя \nСокращение до 32 символов", 'Arial 7', "grey", "gold",
  90. 350, 160, CENTER, None, None, None, None, None, "Label")
  91. text2 = created_widget(root, "Тип игры", 'Arial 18 bold', None, "gold", 350, 240, CENTER, None, None, None, None,
  92. None,
  93. "Label")
  94. var_r = IntVar()
  95. rbutton1 = Radiobutton(root, text='Обычная', fg="green", font='Arial 12 bold', bg="gold", variable=var_r, value=1,
  96. command=type_var)
  97. rbutton1.pack()
  98. rbutton1.place(x=280, y=280, anchor=CENTER)
  99. rbutton2 = Radiobutton(root, text='Испытание', fg="red", font='Arial 12 bold', bg="gold", variable=var_r, value=2,
  100. command=type_var)
  101. rbutton2.pack()
  102. rbutton2.place(x=420, y=280, anchor=CENTER)
  103. var_r.set(1)
  104. text3 = created_widget(root, "*Игра до смерти", 'Arial 10', "grey", "gold", 350, 310, CENTER, None, None, None,
  105. None,
  106. None,
  107. "Label")
  108. text4 = created_widget(root, "Speed", 'Arial 13', None, "gold", 300, 350, CENTER, None, None, None, None, None,
  109. "Label")
  110. text5 = created_widget(root, "*Скорость\nМинимум = 1 Максимум = 10", 'Arial 7', "grey", "gold", 440, 350, CENTER,
  111. None, None, None, None, None, "Label")
  112. entry1 = created_widget(root, None, 'Arial 12 italic', None, "gold", 350, 350, CENTER, 3, 3, 1, "1", None, "Entry")
  113. text6 = created_widget(root, "Size board", 'Arial 13', None, "gold", 283, 380, CENTER, None, None, None, None, None,
  114. "Label")
  115. entry2 = created_widget(root, None, 'Arial 12 italic', None, "white", 350, 380, CENTER, 3, 3, 1, "10", None,
  116. "Entry")
  117. text7 = created_widget(root, "*Размер поля\nМинимум 10х10 Максимум 40х40", 'Arial 7', "grey", "gold", 440, 380,
  118. CENTER, None, None, None, None, None, "Label")
  119. text8 = created_widget(root, "*Игра до 100 очков за уровень\n10 Уровней, каждый следующий сложнее", "Arial 10",
  120. "grey", "gold", 350, 310, CENTER, None, None, None, None, None, "Label")
  121. btn0 = created_widget(root, "Start", "Arial 13", None, None, 350, 440, CENTER, None, None, None, None, start,
  122. "Button")
  123. check_error()
  124. type_var()
  125.  
  126.  
  127. def created_widget(master, text=None, font=None, fg_font=None, bg_font=None, x=None, y=None, anchor=None, bd=None,
  128. width=None, insert1=None, insert2=None, command=None, type_widget=None):
  129. if type_widget == "Label":
  130. widget = Label(master, text=text, fg=fg_font, font=font, bg=bg_font)
  131. widget.pack()
  132. widget.place(x=x, y=y, ancho=anchor)
  133. return widget
  134. if type_widget == "Entry":
  135. widget = Entry(master, bd=bd, width=width, bg=bg_font, font=font)
  136. widget.pack()
  137. widget.place(x=x, y=y, ancho=anchor)
  138. widget.insert(insert1, insert2)
  139. return widget
  140. if type_widget == "Button":
  141. widget = Button(master, text=text, font=font, command=command)
  142. widget.pack()
  143. widget.place(x=x, y=y, ancho=anchor)
  144. return widget
  145.  
  146.  
  147. def start():
  148. global setting, n, dh, h, canvas, coords_table
  149. if errorlog:
  150. messagebox.showerror("Ошибка", errorlog)
  151. else:
  152. setting = False
  153. canvas = Canvas(root, width=700, height=700, bg='black')
  154. canvas.pack(anchor=S, expand=1)
  155. root["bg"] = "black"
  156. n = int(n)
  157. h = 700 / n
  158. dhx = 0
  159. dhy = 0
  160. coords_table = []
  161. for x in range(n):
  162. canvas.create_line(0, dhx, 700, dhx, fill='green', width=2)
  163. for y in range(n):
  164. canvas.create_line(dhy, 0, dhy, 700, fill='green', width=2)
  165. coords_table.append((dhy,dhx))
  166. dhy += h
  167. dhx += h
  168. dhy = 0
  169. coords_table = choice(coords_table)
  170. snake = crt_snake()
  171.  
  172. class crt_snake:
  173. canvas.create_line(0, 0, 700, 700, fill='green', width=2)
  174.  
  175. main()
Advertisement
Add Comment
Please, Sign In to add comment