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