Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- from tkinter import messagebox
- from random import choice
- def main():
- global root
- root = Tk()
- root.title("Настройка")
- root.geometry(f"{700}x{750}+{int(root.winfo_screenwidth() // 2 - 350)}+{int(root.winfo_screenheight() // 2 - 375)}")
- root["bg"] = "gold"
- root.resizable(False, False)
- root.focus_get()
- root.protocol('WM_DELETE_WINDOW', close_game)
- mainSetting()
- mainloop()
- def close_game():
- root.quit()
- def type_var():
- if var_r.get() == 1:
- text8.place_forget()
- text3.place(x=350, y=310, anchor=CENTER)
- text4.place(x=300, y=350, anchor=CENTER)
- text5.place(x=450, y=350, anchor=CENTER)
- entry1.place(x=350, y=350, anchor=CENTER)
- text6.place(x=283, y=380, anchor=CENTER)
- entry2.place(x=350, y=380, anchor=CENTER)
- text7.place(x=450, y=380, anchor=CENTER)
- if var_r.get() == 2:
- text3.place_forget()
- text4.place_forget()
- text5.place_forget()
- entry1.place_forget()
- text6.place_forget()
- entry2.place_forget()
- text7.place_forget()
- text8.place(x=350, y=310, anchor=CENTER)
- def check_error():
- global errorlog, setting, n
- if setting:
- if len(entry0.get()) >= 32:
- errorentry0 = "Nickname: больше 32 символов\n"
- entry0["bg"] = "pink"
- elif len(entry0.get()) == 0:
- errorentry0 = "Nickname: Пустое поле\n"
- entry0["bg"] = "pink"
- else:
- errorentry0 = ""
- entry0["bg"] = "white"
- if entry1.get().isdigit():
- if int(entry1.get()) >= 1 and int(entry1.get()) <= 10:
- errorentry1 = ""
- entry1["bg"] = "white"
- else:
- errorentry1 = f"Speed: {entry1.get()} не входит в диапозон от 1 до 10\n"
- entry1["bg"] = "pink"
- else:
- errorentry1 = f"Speed: {entry1.get()} не является числом\n"
- entry1["bg"] = "pink"
- if entry2.get().isdigit():
- if int(entry2.get()) >= 10 and int(entry2.get()) <= 40:
- errorentry2 = ""
- entry2["bg"] = "white"
- else:
- errorentry2 = f"Size board: {entry2.get()} не входит в диапозон от 10 до 40\n"
- entry2["bg"] = "pink"
- else:
- errorentry2 = f"Size board: {entry2.get()} не является числом\n"
- entry2["bg"] = "pink"
- errorlog = errorentry0 + errorentry1 + errorentry2
- n = entry2.get()
- root.after(1, check_error)
- def mainSetting():
- global setting, var_r, entry0, entry1, entry2, text3, text4, text5, text6, text7, text8, a, canvas
- setting = True
- text0 = created_widget(root, "Игрок", 'Arial 18 bold', None, "gold", 350, 100, CENTER, None, None, None, None, None,
- "Label")
- entry0 = created_widget(root, None, 'Arial 10 italic', None, "white", 350, 130, CENTER, 3, 32, 1, "No Name", None,
- "Entry")
- entry0.focus_set()
- text1 = created_widget(root, "*Позволяет ввести любое имя \nСокращение до 32 символов", 'Arial 7', "grey", "gold",
- 350, 160, CENTER, None, None, None, None, None, "Label")
- text2 = created_widget(root, "Тип игры", 'Arial 18 bold', None, "gold", 350, 240, CENTER, None, None, None, None,
- None,
- "Label")
- var_r = IntVar()
- rbutton1 = Radiobutton(root, text='Обычная', fg="green", font='Arial 12 bold', bg="gold", variable=var_r, value=1,
- command=type_var)
- rbutton1.pack()
- rbutton1.place(x=280, y=280, anchor=CENTER)
- rbutton2 = Radiobutton(root, text='Испытание', fg="red", font='Arial 12 bold', bg="gold", variable=var_r, value=2,
- command=type_var)
- rbutton2.pack()
- rbutton2.place(x=420, y=280, anchor=CENTER)
- var_r.set(1)
- text3 = created_widget(root, "*Игра до смерти", 'Arial 10', "grey", "gold", 350, 310, CENTER, None, None, None,
- None,
- None,
- "Label")
- text4 = created_widget(root, "Speed", 'Arial 13', None, "gold", 300, 350, CENTER, None, None, None, None, None,
- "Label")
- text5 = created_widget(root, "*Скорость\nМинимум = 1 Максимум = 10", 'Arial 7', "grey", "gold", 440, 350, CENTER,
- None, None, None, None, None, "Label")
- entry1 = created_widget(root, None, 'Arial 12 italic', None, "gold", 350, 350, CENTER, 3, 3, 1, "1", None, "Entry")
- text6 = created_widget(root, "Size board", 'Arial 13', None, "gold", 283, 380, CENTER, None, None, None, None, None,
- "Label")
- entry2 = created_widget(root, None, 'Arial 12 italic', None, "white", 350, 380, CENTER, 3, 3, 1, "10", None,
- "Entry")
- text7 = created_widget(root, "*Размер поля\nМинимум 10х10 Максимум 40х40", 'Arial 7', "grey", "gold", 440, 380,
- CENTER, None, None, None, None, None, "Label")
- text8 = created_widget(root, "*Игра до 100 очков за уровень\n10 Уровней, каждый следующий сложнее", "Arial 10",
- "grey", "gold", 350, 310, CENTER, None, None, None, None, None, "Label")
- btn0 = created_widget(root, "Start", "Arial 13", None, None, 350, 440, CENTER, None, None, None, None, start,
- "Button")
- check_error()
- type_var()
- def created_widget(master, text=None, font=None, fg_font=None, bg_font=None, x=None, y=None, anchor=None, bd=None,
- width=None, insert1=None, insert2=None, command=None, type_widget=None):
- if type_widget == "Label":
- widget = Label(master, text=text, fg=fg_font, font=font, bg=bg_font)
- widget.pack()
- widget.place(x=x, y=y, ancho=anchor)
- return widget
- if type_widget == "Entry":
- widget = Entry(master, bd=bd, width=width, bg=bg_font, font=font)
- widget.pack()
- widget.place(x=x, y=y, ancho=anchor)
- widget.insert(insert1, insert2)
- return widget
- if type_widget == "Button":
- widget = Button(master, text=text, font=font, command=command)
- widget.pack()
- widget.place(x=x, y=y, ancho=anchor)
- return widget
- def start():
- global setting, n, dh, h, canvas, coords_table
- if errorlog:
- messagebox.showerror("Ошибка", errorlog)
- else:
- setting = False
- canvas = Canvas(root, width=700, height=700, bg='black')
- canvas.pack(anchor=S, expand=1)
- root["bg"] = "black"
- n = int(n)
- h = 700 / n
- dhx = 0
- dhy = 0
- coords_table = []
- for x in range(n):
- canvas.create_line(0, dhx, 700, dhx, fill='green', width=2)
- for y in range(n):
- canvas.create_line(dhy, 0, dhy, 700, fill='green', width=2)
- coords_table.append((dhy,dhx))
- dhy += h
- dhx += h
- dhy = 0
- coords_table = choice(coords_table)
- snake = crt_snake()
- class crt_snake:
- canvas.create_line(0, 0, 700, 700, fill='green', width=2)
- main()
Advertisement
Add Comment
Please, Sign In to add comment