Facebook
From Chunky Lion, 2 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 56
  1. from tkinter import *
  2. from functools import partial
  3. from random import randint
  4. import datetime
  5.  
  6. root = Tk()
  7. root.geometry('480x480')
  8. root.resizable(False, False)
  9. root.title("Kółko i krzyżyk")
  10. root.iconbitmap("logo.ico")
  11. photo_1 = PhotoImage(file="logo.png")
  12. clicked = False
  13. counter = 0
  14. nick1 = ""
  15. nick2 = ""
  16. last_move = []
  17.  
  18.  
  19. # Funkcja tworząca menu startowe gry.
  20. def gameMenu():
  21.     Label(root, image=photo_1).place(x=345, y=0)
  22.     Label(root, text='Kółko i krzyżyk', fg="dark green", font="Times 32 bold").place(x=30, y=35)
  23.     Label(root, text='Gracz "0":', fg="black", font="Times 16 bold").place(x=20, y=135)
  24.     nick1_menu = Entry(root, width=30, borderwidth=5)
  25.     nick1_menu.place(x=250, y=135)
  26.     Label(root, text='Gracz "X" (Multiplayer):', fg="black", font="Times 16 bold").place(x=20, y=170)
  27.     nick2_menu = Entry(root, width=30, borderwidth=5)
  28.     nick2_menu.place(x=250, y=170)
  29.     Label(root, text='Podaj rozmiar planszy:', fg="black", font="Times 16 bold").place(x=20, y=205)
  30.     size = Entry(root, width=30, borderwidth=5)
  31.     size.place(x=250, y=205)
  32.     Label(root, text='Menu', fg="black", font="Times 22 bold").place(x=200, y=235)
  33.     Button(root, text="Graj z komputerem", fg="black", font="Times 16", padx="70",
  34.            command=partial(singleplayer_game, nick1_menu, size)).place(x=82, y=275)
  35.     Button(root, text="Graj ze znajomym", fg="black", font="Times 16", padx="75",
  36.            command=partial(multiplayer_game, nick1_menu, nick2_menu, size)).place(x=82, y=320)
  37.     Button(root, text="Statystyki", fg="black", font="Times 16", padx="110",
  38.            command=partial(stat_display)).place(x=82, y=365)
  39.     Button(root, text="Wyjdź", fg="black", font="Times 16", padx="123", command=root.quit).place(x=82, y=410)
  40.  
  41.  
  42. # Pasem Menu dostępny w każdym momencie gry z podstawowymi opcjami oraz info
  43. def menuToolbar(button=0):
  44.     menu = Menu(root)
  45.     root.config(menu=menu)
  46.     filemenu = Menu(menu, tearoff=0)
  47.     menu.add_cascade(label='Opcje', menu=filemenu)
  48.     filemenu.add_command(label='Cofnij', command=partial(return_click, button))
  49.     filemenu.add_command(label='Reset', command=reset)
  50.     filemenu.add_command(label='Statystyki')
  51.     filemenu.add_separator()
  52.     filemenu.add_command(label='Wyjdź', command=root.quit)
  53.     infomenu = Menu(menu, tearoff=0)
  54.     menu.add_cascade(label='Info', menu=infomenu)
  55.     infomenu.add_command(label='Autorzy:')
  56.     infomenu.add_separator()
  57.     infomenu.add_command(label='Roksana Cieśla')
  58.     infomenu.add_command(label='Stanisław Dudiak')
  59.     infomenu.add_command(label='Jakub Dębski')
  60.     infomenu.add_command(label='Marcin Brzózka')
  61.  
  62.  
  63. # Funkcja resetująca - powrót do menu gry
  64. def reset():
  65.     global clicked
  66.     global counter
  67.     global nick1
  68.     global nick2
  69.     for widget in root.winfo_children():
  70.         widget.destroy()
  71.     clicked = False
  72.     counter = 0
  73.     nick1 = ""
  74.     nick2 = ""
  75.     menuToolbar()
  76.     gameMenu()
  77.  
  78.  
  79. # Fukcja przygotowująca grę ze znajomym
  80. def multiplayer_game(nick1_menu, nick2_menu, size_menu):
  81.     global nick1
  82.     global nick2
  83.     nick1 = nick1_menu.get()
  84.     nick2 = nick2_menu.get()
  85.     size = int(size_menu.get())
  86.  
  87.     if (size < 3):
  88.         size = 3
  89.     for widget in root.winfo_children():
  90.         widget.destroy()
  91.     button = [[0 for i in range(size)] for j in range(size)]
  92.     menuToolbar(button)
  93.     for i in range(size):
  94.         for j in range(size):
  95.             button[i][j] = Button(root, text=None, font="Times 24 bold",
  96.                                   command=partial(multiplayer_click, button, i, j, size))
  97.             button[i][j].grid(row=i, column=j, sticky="nsew")
  98.             Grid.rowconfigure(root, i, weight=1)
  99.             Grid.columnconfigure(root, j, weight=1)
  100.  
  101.  
  102. # Funkcja gry ze znajomym
  103. def multiplayer_click(button, i, j, size):
  104.     global clicked
  105.     global counter
  106.     global last_move
  107.     if (button[i][j]['text'] == "" and clicked):
  108.         button[i][j]['text'] = "X"
  109.         clicked = False
  110.         counter += 1
  111.         win(button, size)
  112.         last_move.append((i, j))
  113.     elif (button[i][j]['text'] == "" and not clicked):
  114.         button[i][j]['text'] = "O"
  115.         clicked = True
  116.         counter += 1
  117.         win(button, size)
  118.         last_move.append((i, j))
  119.  
  120.  
  121. # Przygotowanie gry z komputerem
  122. def singleplayer_game(nick1_menu, size_menu):
  123.     global nick1
  124.     global nick2
  125.     nick1 = nick1_menu.get()
  126.     nick2 = "Komputer"
  127.     size = int(size_menu.get())
  128.     if (size < 3):
  129.         size = 3
  130.     for widget in root.winfo_children():
  131.         widget.destroy()
  132.     menuToolbar()
  133.     button = [[0 for i in range(size)] for j in range(size)]
  134.     for i in range(size):
  135.         for j in range(size):
  136.             button[i][j] = Button(root, text=None, font="Times 24 bold",
  137.                                   command=partial(singleplayer_click, button, i, j, size))
  138.             button[i][j].grid(row=i, column=j, sticky="nsew")
  139.             Grid.rowconfigure(root, i, weight=1)
  140.             Grid.columnconfigure(root, j, weight=1)
  141.  
  142.  
  143. # Gra z komputerem
  144. def singleplayer_click(button, i, j, size):
  145.     global clicked
  146.     global counter
  147.     if (button[i][j]['text'] == "" and not clicked):
  148.         button[i][j]['text'] = "O"
  149.         clicked = True
  150.         counter += 1
  151.         win(button, size)
  152.         while (clicked):
  153.             computer_i = randint(0, size - 1)
  154.             computer_j = randint(0, size - 1)
  155.             if (button[computer_i][computer_j]['text'] == ""):
  156.                 button[computer_i][computer_j]['text'] = "X"
  157.                 clicked = False
  158.                 counter += 1
  159.                 win(button, size)
  160.  
  161.  
  162. # Funkcja sprawdzania zwycięzcy
  163. def win(button, size):
  164.     global nick1
  165.     global nick2
  166.     for i in range(size - 2):
  167.         for j in range(size - 2):
  168.             if (button[i][j]['text'] == button[i + 1][j]['text'] == button[i + 2][j]['text'] == "O") \
  169.                     or (button[i][j + 1]['text'] == button[i + 1][j + 1]['text'] == button[i + 2][j + 1]['text'] == "O") \
  170.                     or (button[i][j + 2]['text'] == button[i + 1][j + 2]['text'] == button[i + 2][j + 2]['text'] == "O") \
  171.                     or (button[i][j]['text'] == button[i][j + 1]['text'] == button[i][j + 2]['text'] == "O") \
  172.                     or (button[i + 1][j]['text'] == button[i + 1][j + 1]['text'] == button[i + 1][j + 2]['text'] == "O") \
  173.                     or (button[i + 2][j]['text'] == button[i + 2][j + 1]['text'] == button[i + 2][j + 2]['text'] == "O") \
  174.                     or (button[i][j]['text'] == button[i + 1][j + 1]['text'] == button[i + 2][j + 2]['text'] == "O") \
  175.                     or (button[i][j + 2]['text'] == button[i + 1][j + 1]['text'] == button[i + 2][j]['text'] == "O"):
  176.                 for widget in root.winfo_children():
  177.                     widget.destroy()
  178.                 menuToolbar()
  179.                 stat(nick1, nick2)
  180.                 Label(root, text="Wygrał", fg="dark green", font="Times 42 bold").pack()
  181.                 Label(root, text=nick1, fg="dark green", font="Times 42 bold").pack()
  182.                 Button(root, text="Menu", fg="black", font="Times 16", padx="123", command=reset).place(x=82, y=350)
  183.             elif (button[i][j]['text'] == button[i + 1][j]['text'] == button[i + 2][j]['text'] == "X") \
  184.                     or (button[i][j + 1]['text'] == button[i + 1][j + 1]['text'] == button[i + 2][j + 1]['text'] == "X") \
  185.                     or (button[i][j + 2]['text'] == button[i + 1][j + 2]['text'] == button[i + 2][j + 2]['text'] == "X") \
  186.                     or (button[i][j]['text'] == button[i][j + 1]['text'] == button[i][j + 2]['text'] == "X") \
  187.                     or (button[i + 1][j]['text'] == button[i + 1][j + 1]['text'] == button[i + 1][j + 2]['text'] == "X") \
  188.                     or (button[i + 2][j]['text'] == button[i + 2][j + 1]['text'] == button[i + 2][j + 2]['text'] == "X") \
  189.                     or (button[i][j]['text'] == button[i + 1][j + 1]['text'] == button[i + 2][j + 2]['text'] == "X") \
  190.                     or (button[i][j + 2]['text'] == button[i + 1][j + 1]['text'] == button[i + 2][j]['text'] == "X"):
  191.                 for widget in root.winfo_children():
  192.                     widget.destroy()
  193.                 menuToolbar()
  194.                 stat(nick2, nick1)
  195.                 Label(root, text="Wygrał", fg="dark green", font="Times 42 bold").pack()
  196.                 Label(root, text=nick2, fg="dark green", font="Times 42 bold").pack()
  197.                 Button(root, text="Menu", fg="black", font="Times 16", padx="123", command=reset).place(x=82, y=350)
  198.             elif (counter == size * size):
  199.                 for widget in root.winfo_children():
  200.                     widget.destroy()
  201.                 stat_draw(nick1, nick2)
  202.                 Label(root, text="Remis", fg="dark green", font="Times 42 bold").place(x=165, y=100)
  203.                 Button(root, text="Menu", fg="black", font="Times 16", padx="123", command=reset).place(x=82, y=350)
  204.  
  205.  
  206. # Funkcje odpowiedzialne za wyświetlanie statystyk
  207. def stat(winer, loser):
  208.     with open(r"Statystyki.txt", 'a') as file:
  209.         x = datetime.datetime.now()
  210.         date = x.strftime("%d/%m/%Y %H:%M:%S")
  211.         tekst = f"{date} - Wygrany: {winer}, Przegrany: {loser}\n"
  212.         file.write(tekst)
  213.  
  214.  
  215. def stat_draw(p1, p2):
  216.     with open(r"Statystyki.txt", 'a') as file:
  217.         x = datetime.datetime.now()
  218.         date = x.strftime("%d/%m/%Y %H:%M:%S")
  219.         tekst = f"{date} - Remis pomiędzy {p1} i {p2}\n"
  220.         file.write(tekst)
  221.  
  222.  
  223. def stat_display():
  224.     for widget in root.winfo_children():
  225.         widget.destroy()
  226.     menuToolbar()
  227.     file = open(r"Statystyki.txt", "r")
  228.     lista = [" "] * 17
  229.     list_of_stats = file.readlines()
  230.     list_of_stats.reverse()
  231.     for i in range(len(list_of_stats)):
  232.         lista[i] = list_of_stats[i]
  233.     for i in range(17):
  234.         Label(root, text=lista[i], fg="black", font="Times 13 bold").place(x=0, y=i * 24)
  235.     Button(root, text="Wróć do menu", fg="black", font="Times 16", padx="10", command=reset).place(x=170, y=420)
  236.  
  237.  
  238. def return_click(button):
  239.     global counter
  240.     global clicked
  241.     global last_move
  242.     if last_move != []:
  243.         if (clicked == True):
  244.             clicked = False
  245.         else:
  246.             clicked = True
  247.         counter -= 1
  248.         button[last_move[-1][0]][last_move[-1][1]]['text'] = ""
  249.         last_move.pop(-1)
  250.  
  251.  
  252. menuToolbar()
  253. gameMenu()
  254. root.mainloop()