TIC TAC TOE GAME USING TKINTER IN PYTHON
INTRODUCTION:
This
is a mini project on the famous game which all the people in the world have
played in their small age, the game is TIC TAC TOE. In olden ages we play the
game in paper or notebook, but in this digital period everything has come into
our electronic devices. Have we ever wondered how the game is designed in our
electronic device, so in this I will show how we can create the game by coding
it using python.
BUTTONS:
Python
is a language which is user friendly, we can code in python by using common
words. To print a statement we use the word ‘print’ and then followed by the
statement. The main thing in tic tac toe game is that we need 9 buttons. The
command to create a button is “button” within button we can change
the font of the button , size and many properties. The size of the button can
also be changed.
FUNCTION:
In
this program we are using two functions they are ‘play’ and
‘start’ we are declaring the functions using the word “def”.
‘lambda’ is the command which is used to call the function
Eg:
def play(button):
This
is how the function is declared and while calling the function we use
“command=lambda:play(button1)”Here button1 is passed to the function
‘play’ and after that the commands given below it is runned, as we have declared
it as global, it can be accessed by all the functions.
MESSAGE
BOX:
In the header we have
declared “import tkinter.messagebox”. The use of the message box is to display
a command box at the last with some information in it. The command box is shown
below,
answer =
tkinter.messagebox.askquestion('Congratulations X Player won the game.!!!', 'Do
you want to play again')
if answer == 'yes': start()
When we give the above
code the command box will display as it is diaplayed above. If we press ‘yes’
it will go to the function and the code will run and if we press ‘no’ it will
again go to the function and exit the game.
IF AND
ELSE IF:
In
‘if’ statement we have given all the possibilities of ‘X’ if any one becomes
true then the statements or condition below it will be displayed and hence
player ‘X’ will win the game, if nothing is true then it will go to ‘else if’
statement which can be written shortly as ‘elif’.
In
‘elif’ statement all the condition for ‘O’ will be checked if any one is true
then player ‘O’ will win the game if nothing is true then the match will be tie
and hence the player has to restart the game.
CODE:
from
tkinter import *
import
tkinter.messagebox
click = True
tk
= None
def
start():
global
tk
tk
= Tk()
tk.title("Tic
Tac Toe")
def
play(button):
global
click, tk
if
button["text"] == " " and click:
button["text"]
= "X"
click
= False
elif
button["text"] == " ":
button['text']
= "O"
click
= True
if
(button1["text"] == "X" and button2["text"] ==
"X" and button3["text"] == "X" or
button4["text"]
== "X" and button5["text"] == "X" and
button6["text"] == "X" or
button7["text"]
== "X" and button8["text"] == "X" and
button9["text"] == "X" or
button1["text"]
== "X" and button4["text"] == "X" and
button7["text"] == "X" or
button2["text"]
== "X" and button5["text"] == "X" and
button8["text"] == "X" or
button3["text"]
== "X" and button6["text"] == "X" and
button9["text"] == "X" or
button1["text"]
== "X" and button5["text"] == "X" and
button9["text"] == "X" or
button3["text"]
== "X" and button5["text"] == "X" and
button7["text"] == "X"):
answer
= tkinter.messagebox.askquestion('Congratulations X Player won the game.!!!',
'Do you want to play again')
tk.destroy()
if
answer == 'yes': start()
elif
(button1["text"] == "O" and button2["text"] ==
"O" and button3["text"] == "O" or
button4["text"]
== "O" and button5["text"] == "O" and
button6["text"] == "O" or
button7["text"]
== "O" and button8["text"] == "O" and
button9["text"] == "O" or
button1["text"]
== "O" and button4["text"] == "O" and button7["text"]
== "O" or
button2["text"]
== "O" and button5["text"] == "O" and
button8["text"] == "O" or
button3["text"]
== "O" and button6["text"] == "O" and
button9["text"] == "O" or
button1["text"]
== "O" and button5["text"] == "O" and
button9["text"] == "O" or
button3["text"]
== "O" and button5["text"] == "O" and
button7["text"] == "O"):
answer
= tkinter.messagebox.askquestion('Congratulations O Player won the game!!!',
'Do you want to play again')
tk.destroy()
if
answer == 'yes': start()
button1
= Button(tk, text=" ", font=('Times 26 bold'), height=4, width=8,
command=lambda:play(button1))
button1.grid(row=1,
column=0)
button2
= Button(tk, text=" ", font=('Times 26 bold'), height=4, width=8,
command=lambda:play(button2))
button2.grid(row=1,
column=1)
button3
= Button(tk, text=" ", font=('Times 26 bold'), height=4, width=8,
command=lambda:play(button3))
button3.grid(row=1,
column=2)
button4
= Button(tk, text=" ", font=('Times 26 bold'), height=4, width=8,
command=lambda:play(button4))
button4.grid(row=2,
column=0)
button5
= Button(tk, text=" ", font=("Times 26 bold"), height=4,
width=8, command=lambda:play(button5))
button5.grid(row=2,
column=1)
button6
= Button(tk, text=" ", font=('Times 26 bold'), height=4, width=8,
command=lambda:play(button6))
button6.grid(row=2,
column=2)
button7
= Button(tk, text=" ", font=('Times 26 bold'), height=4, width=8,
command=lambda:play(button7))
button7.grid(row=3,
column=0)
button8
= Button(tk, text=" ", font=('Times 26 bold'), height=4, width=8,
command=lambda:play(button8))
button8.grid(row=3,
column=1)
button9
= Button(tk, text=" ", font=('Times 26 bold'), height=4, width=8,
command=lambda:play(button9))
button9.grid(row=3,
column=2)
tk.mainloop()
start()
CONCLUSION:
Hence
the Tic Tac Toe game is designed by tkinter in python. If you are free you can
play this game for time pass. As now you all know how to code the game using
python so this game will be more interesting to play. If you have any doubt
regarding this project then you can watch my video in that video I have
explained all the lines clearly, the video link is given below,
Comments
Post a Comment