- from tkinter import *
- from functools import partial
- from random import randint
- import datetime
- root = Tk()
- root.geometry('480x480')
- root.resizable(False, False)
- root.title("Kółko i krzyżyk")
- root.iconbitmap("logo.ico")
- photo_1 = PhotoImage(file="logo.png")
- clicked = False
- counter = 0
- nick1 = ""
- nick2 = ""
- # Funkcja tworząca menu startowe gry.
- def gameMenu():
- Label(root, image=photo_1).place(x=345, y=0)
- Label(root, text='Kółko i krzyżyk', fg="dark green", font="Times 32 bold").place(x=30, y=35)
- Label(root, text='Gracz "0":', fg="black", font="Times 16 bold").place(x=20, y=135)
- nick1_menu = Entry(root, width=30, borderwidth=5)
- nick1_menu.place(x=250, y=135)
- Label(root, text='Gracz "X" (Multiplayer):', fg="black", font="Times 16 bold").place(x=20, y=170)
- nick2_menu = Entry(root, width=30, borderwidth=5)
- nick2_menu.place(x=250, y=170)
- Label(root, text='Podaj rozmiar planszy:', fg="black", font="Times 16 bold").place(x=20, y=205)
- size = Entry(root, width=30, borderwidth=5)
- size.place(x=250, y=205)
- Label(root, text='Menu', fg="black", font="Times 22 bold").place(x=200, y=235)
- Button(root, text="Graj z komputerem", fg="black", font="Times 16", padx="70",
- command=partial(singleplayer_game, nick1_menu, size)).place(x=82, y=275)
- Button(root, text="Graj ze znajomym", fg="black", font="Times 16", padx="75",
- command=partial(multiplayer_game, nick1_menu, nick2_menu, size)).place(x=82, y=320)
- Button(root, text="Statystyki", fg="black", font="Times 16", padx="110",
- command = partial(stat_display)).place(x=82, y=365)
- Button(root, text="Wyjdź", fg="black", font="Times 16", padx="123", command=root.quit).place(x=82, y=410)
- # Pasem Menu dostępny w każdym momencie gry z podstawowymi opcjami oraz info
- def menuToolbar():
- menu = Menu(root)
- root.config(menu=menu)
- filemenu = Menu(menu, tearoff=0)
- menu.add_cascade(label='Opcje', menu=filemenu)
- filemenu.add_command(label='Reset', command=reset)
- filemenu.add_command(label='Statystyki')
- filemenu.add_separator()
- filemenu.add_command(label='Wyjdź', command=root.quit)
- infomenu = Menu(menu, tearoff=0)
- menu.add_cascade(label='Info', menu=infomenu)
- infomenu.add_command(label='Autorzy:')
- infomenu.add_separator()
- infomenu.add_command(label='Roksana Cieśla')
- infomenu.add_command(label='Stanisław Dudiak')
- infomenu.add_command(label='Jakub Dębski')
- infomenu.add_command(label='Marcin Brzózka')
- # Funkcja resetująca - powrót do menu gry
- def reset():
- global clicked
- global counter
- global nick1
- global nick2
- for widget in root.winfo_children():
- widget.destroy()
- clicked = False
- counter = 0
- nick1 = ""
- nick2 = ""
- menuToolbar()
- gameMenu()
- # Fukcja przygotowująca grę ze znajomym
- def multiplayer_game(nick1_menu, nick2_menu, size_menu):
- global nick1
- global nick2
- nick1 = nick1_menu.get()
- nick2 = nick2_menu.get()
- size = int(size_menu.get())
- if (size < 3):
- size = 3
- for widget in root.winfo_children():
- widget.destroy()
- menuToolbar()
- button = [[0 for i in range(size)] for j in range(size)]
- for i in range(size):
- for j in range(size):
- button[i][j] = Button(root, text=None, font="Times 24 bold",
- command=partial(multiplayer_click, button, i, j, size))
- button[i][j].grid(row=i, column=j, sticky="nsew")
- Grid.rowconfigure(root, i, weight=1)
- Grid.columnconfigure(root, j, weight=1)
- # Funkcja gry ze znajomym
- def multiplayer_click(button, i, j, size):
- global clicked
- global counter
- if (button[i][j]['text'] == "" and clicked):
- button[i][j]['text'] = "X"
- clicked = False
- counter += 1
- win(button, size)
- elif (button[i][j]['text'] == "" and not clicked):
- button[i][j]['text'] = "O"
- clicked = True
- counter += 1
- win(button, size)
- #Przygotowanie gry z komputerem
- def singleplayer_game(nick1_menu, size_menu):
- global nick1
- global nick2
- nick1 = nick1_menu.get()
- nick2 = "Komputer"
- size = int(size_menu.get())
- if (size < 3):
- size = 3
- for widget in root.winfo_children():
- widget.destroy()
- menuToolbar()
- button = [[0 for i in range(size)] for j in range(size)]
- for i in range(size):
- for j in range(size):
- button[i][j] = Button(root, text=None, font="Times 24 bold",
- command=partial(singleplayer_click, button, i, j, size))
- button[i][j].grid(row=i, column=j, sticky="nsew")
- Grid.rowconfigure(root, i, weight=1)
- Grid.columnconfigure(root, j, weight=1)
- #Gra z komputerem
- def singleplayer_click(button, i, j, size):
- global clicked
- global counter
- if (button[i][j]['text'] == "" and not clicked):
- button[i][j]['text'] = "O"
- clicked = True
- counter += 1
- win(button, size)
- while (clicked):
- computer_i = randint(0, size - 1)
- computer_j = randint(0, size - 1)
- if (button[computer_i][computer_j]['text'] == ""):
- button[computer_i][computer_j]['text'] = "X"
- clicked = False
- counter += 1
- win(button, size)
- #Funkcja sprawdzania zwycięzcy
- def win(button, size):
- global nick1
- global nick2
- for i in range(size - 2):
- for j in range(size - 2):
- if (button[i][j]['text'] == button[i + 1][j]['text'] == button[i + 2][j]['text'] == "O") \
- or (button[i][j + 1]['text'] == button[i + 1][j + 1]['text'] == button[i + 2][j + 1]['text'] == "O") \
- or (button[i][j + 2]['text'] == button[i + 1][j + 2]['text'] == button[i + 2][j + 2]['text'] == "O") \
- or (button[i][j]['text'] == button[i][j + 1]['text'] == button[i][j + 2]['text'] == "O") \
- or (button[i + 1][j]['text'] == button[i + 1][j + 1]['text'] == button[i + 1][j + 2]['text'] == "O") \
- or (button[i + 2][j]['text'] == button[i + 2][j + 1]['text'] == button[i + 2][j + 2]['text'] == "O") \
- or (button[i][j]['text'] == button[i + 1][j + 1]['text'] == button[i + 2][j + 2]['text'] == "O") \
- or (button[i][j + 2]['text'] == button[i + 1][j + 1]['text'] == button[i + 2][j]['text'] == "O"):
- for widget in root.winfo_children():
- widget.destroy()
- menuToolbar()
- stat(nick1,nick2)
- Label(root, text="Wygrał", fg="dark green", font="Times 42 bold").pack()
- Label(root, text=nick1, fg="dark green", font="Times 42 bold").pack()
- Button(root, text="Menu", fg="black", font="Times 16", padx="123", command=reset).place(x=82, y=350)
- elif (button[i][j]['text'] == button[i + 1][j]['text'] == button[i + 2][j]['text'] == "X") \
- or (button[i][j + 1]['text'] == button[i + 1][j + 1]['text'] == button[i + 2][j + 1]['text'] == "X") \
- or (button[i][j + 2]['text'] == button[i + 1][j + 2]['text'] == button[i + 2][j + 2]['text'] == "X") \
- or (button[i][j]['text'] == button[i][j + 1]['text'] == button[i][j + 2]['text'] == "X") \
- or (button[i + 1][j]['text'] == button[i + 1][j + 1]['text'] == button[i + 1][j + 2]['text'] == "X") \
- or (button[i + 2][j]['text'] == button[i + 2][j + 1]['text'] == button[i + 2][j + 2]['text'] == "X") \
- or (button[i][j]['text'] == button[i + 1][j + 1]['text'] == button[i + 2][j + 2]['text'] == "X") \
- or (button[i][j + 2]['text'] == button[i + 1][j + 1]['text'] == button[i + 2][j]['text'] == "X"):
- for widget in root.winfo_children():
- widget.destroy()
- menuToolbar()
- stat(nick2, nick1)
- Label(root, text="Wygrał", fg="dark green", font="Times 42 bold").pack()
- Label(root, text=nick2, fg="dark green", font="Times 42 bold").pack()
- Button(root, text="Menu", fg="black", font="Times 16", padx="123", command=reset).place(x=82, y=350)
- elif (counter == size * size):
- for widget in root.winfo_children():
- widget.destroy()
- stat_draw(nick1,nick2)
- Label(root, text="Remis", fg="dark green", font="Times 42 bold").place(x=165, y=100)
- Button(root, text="Menu", fg="black", font="Times 16", padx="123", command=reset).place(x=82, y=350)
- #Funkcje odpowiedzialne za wyświetlanie statystyk
- def stat(wygrany,przegrany):
- file = open(r"Statystyki.txt", 'a')
- x = datetime.datetime.now()
- date = x.strftime("%x") + " " + x.strftime('%X')
- tekst = date + " Wygrany: " + wygrany + " Przegrany: " + przegrany + "\n"
- file.write(tekst)
- file.close()
- def stat_draw(p1,p2):
- file = open(r"Statystyki.txt", 'a')
- x = datetime.datetime.now()
- date = x.strftime("%x") + " " + x.strftime('%X')
- tekst = date + " Remis między " + p1 + " a " + p2 +"\n"
- file.write(tekst)
- file.close()
- def stat_display():
- for widget in root.winfo_children():
- widget.destroy()
- menuToolbar()
- file = open(r"Statystyki.txt", "r")
- lista = [" "] * 17
- list_of_stats = file.readlines()
- list_of_stats.reverse()
- for i in range(len(list_of_stats)):
- lista[i] = list_of_stats[i]
- for i in range(17):
- Label(root, text=lista[i], fg="black", font="Times 13 bold").place(x=0, y=i * 24)
- Button(root, text="Wróć do menu", fg="black", font="Times 16", padx="10", command=reset).place(x=170, y=420)
- menuToolbar()
- gameMenu()
- root.mainloop()