How to create a game in python| Saifeealii

in #tutorials7 years ago (edited)

In this article you are going to learn how to create a basic simple game in python and with that you will also learn the basics of python you just need python and absolutely the simple basics of python Guys hello i am saifeealii and today i will show you how to create a simple game like tic tac toe, i know that you guys are laughing and thinking that what the trash content i had made.But this tutorial is for python beginners, not for python masters. So guys first of all you just know about if statement,else statement.Guys we will create a simple game and then we will jump to big games. Guys my game will look like that game that appears on google when you just type tic tac toe.

Guys lets start!

1

First of all open python shell and press ctrl +N

When new file is open then we will start typing code

2

Secondly we will type coding in the new file, i had given that codes from my repository.
Coding

from tkinter import *
import tkinter.messagebox
tk=Tk()
tk.title("Tic Tac Toe")

bclick = True

def ttt(buttons):
global bclick
if buttons["text"] == " " and bclick == True:
buttons["text"] = "X"
bclick = False
elif buttons["text"] == " " and bclick == False:
buttons["text"] = "O"
bclick = True

 elif(button1['text'] == 'X' and button2['text'] == 'X' and button3['text'] == 'X'):
      tkinter.messagebox.showinfo("Player X","Winner is X !!!")
 elif(button4['text'] == 'X' and button5['text'] == 'X' and button6['text'] == 'X'):   #123
      tkinter.messagebox.showinfo("Player X","Winner is X !!!")                        #456
                                                                                       #789    
 elif(button7['text'] =='X' and button8['text'] == 'X' and button9['text'] == 'X'):
      tkinter.messagebox.showinfo("Player X","Winner is X !!!")
 elif(button1['text'] == 'X' and button5['text'] == 'X' and button9['text'] == 'X'):
      tkinter.messagebox.showinfo("Player X","Winner is X !!!")
 elif(button3['text'] == 'X' and button5['text'] == 'X' and button7['text'] == 'X'):
      tkinter.messagebox.showinfo("Player X","Winner is X !!!")
 elif(button1['text'] == 'X' and button2['text'] == 'X' and button3['text'] == 'X'):
      tkinter.messagebox.showinfo("Player X","Winner is X !!!")
 elif(button1['text'] == 'X' and button4['text'] == 'X' and button7['text'] == 'X'):
      tkinter.messagebox.showinfo("Player X","Winner is X !!!")
 elif(button2['text'] == 'X' and button5['text'] == 'X' and button8['text'] == 'X'):
      tkinter.messagebox.showinfo("Player X","Winner is X !!!")
 elif(button7['text'] == 'X' and button6['text'] == 'X' and button9['text'] == 'X'):
      tkinter.messagebox.showinfo("Player X",'Winner is X !!!')

 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 button5['text'] == 'O' and button9['text'] == 'O'or
      button3['text'] == 'O' and button5['text'] == 'O' and button7['text'] == 'O'or
      button1['text'] == 'O' and button2['text'] == 'O' and button3['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
      button7['text'] == 'O' and button6['text'] == 'O' and button9['text'] == 'O'):
      tkinter.messagebox.showinfo("Player O",'Winner is O !!!!')

buttons=StringVar()

button1 = Button(tk,text=" ",font=('Times 20 bold'),bg='gray',fg='white',height=4,width=8,command=lambda:ttt(button1))
button1.grid(row=1,column=0,sticky = S+N+E+W)

button2 = Button(tk,text=' ',font=('Times 20 bold'),bg='gray',fg='white',height=4,width=8,command=lambda:ttt(button2))
button2.grid(row=1,column=1,sticky = S+N+E+W)

button3 = Button(tk,text=' ',font=('Times 20 bold'),bg='gray',fg='white',height=4,width=8,command=lambda:ttt(button3))
button3.grid(row=1,column=2,sticky = S+N+E+W)

button4 = Button(tk,text=' ',font=('Times 20 bold'),bg='gray',fg='white',height=4,width=8,command=lambda:ttt(button4))
button4.grid(row=2,column=0,sticky = S+N+E+W)

button5 = Button(tk,text=' ',font=('Times 20 bold'),bg='gray',fg='white',height=4,width=8,command=lambda:ttt(button5))
button5.grid(row=2,column=1,sticky = S+N+E+W)

button6 = Button(tk,text=' ',font=('Times 20 bold'),bg='gray',fg='white',height=4,width=8,command=lambda:ttt(button6))
button6.grid(row=2,column=2,sticky = S+N+E+W)

button7 = Button(tk,text=' ',font=('Times 20 bold'),bg='gray',fg='white',height=4,width=8,command=lambda:ttt(button7))
button7.grid(row=3,column=0,sticky = S+N+E+W)

button8 = Button(tk,text=' ',font=('Times 20 bold'),bg='gray',fg='white',height=4,width=8,command=lambda:ttt(button8))
button8.grid(row=3,column=1,sticky = S+N+E+W)

button9 = Button(tk,text=' ',font=('Times 20 bold'),bg='gray',fg='white',height=4,width=8,command=lambda:ttt(button9))
button9.grid(row=3,column=2,sticky = S+N+E+W)

tk.mainloop()

When the coding is complete then press F5 then it will ask you for saving the file,after saving file in the format .py it will open a python module and then a box with the game tictacttoe

I hope you liked my contribution and with my contribution you know ed how to create a tictac toe
Stay tune for my next contribution.

Sort:  

Hi! I am a robot. I just upvoted you! I found similar content that readers might be interested in:
https://stackoverflow.com/questions/43436142/tic-tac-toe-game-python-tkinter-where-is-the-bug