diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..6aee1eb Binary files /dev/null and b/.DS_Store differ diff --git a/Gui.PNG b/Gui.PNG new file mode 100644 index 0000000..ddfa971 Binary files /dev/null and b/Gui.PNG differ diff --git a/README.md b/README.md index adc31e7..41337e6 100644 --- a/README.md +++ b/README.md @@ -1 +1,3 @@ -## # Simple Snake Game in Python 3 for RANDOM, TIME, Turtle themes \ No newline at end of file +# Tic Tac Toe Game In Python + + \ No newline at end of file diff --git a/Winning_message.PNG b/Winning_message.PNG new file mode 100644 index 0000000..9ebcf05 Binary files /dev/null and b/Winning_message.PNG differ diff --git a/draw_messge.PNG b/draw_messge.PNG new file mode 100644 index 0000000..08f6243 Binary files /dev/null and b/draw_messge.PNG differ diff --git a/resources/base.png b/resources/base.png new file mode 100644 index 0000000..95c69de Binary files /dev/null and b/resources/base.png differ diff --git a/snake.py b/snake.py deleted file mode 100644 index cea7e7f..0000000 --- a/snake.py +++ /dev/null @@ -1,185 +0,0 @@ -# Simple Snake Game in Python 3 for RANDOM, TIME, turtle themes - - -import turtle -import time -import random - -delay = 0.1 - -# Score -score = 0 -high_score = 0 - -# Set up the screen -wn = turtle.Screen() -wn.title("Snake Game by SUBUX") -wn.bgcolor("darkblue") -wn.setup(width=600, height=600) -wn.tracer(0) # Turns off the screen updates - -# Snake head -head = turtle.Turtle() -head.speed(0) -head.shape("square") -head.color("black") -head.penup() -head.goto(0,0) -head.direction = "stop" - -# Snake food -food = turtle.Turtle() -food.speed(0) -food.shape("circle") -food.color("red") -food.penup() -food.goto(0,100) - -segments = [] - -# Pen -pen = turtle.Turtle() -pen.speed(0) -pen.shape("square") -pen.color("white") -pen.penup() -pen.hideturtle() -pen.goto(0, 260) -pen.write("Score: 0 High Score: 0", align="center", font=("Courier", 24, "normal")) - -# Functions -def go_up(): - if head.direction != "down": - head.direction = "up" - -def go_down(): - if head.direction != "up": - head.direction = "down" - -def go_left(): - if head.direction != "right": - head.direction = "left" - -def go_right(): - if head.direction != "left": - head.direction = "right" - -def move(): - if head.direction == "up": - y = head.ycor() - head.sety(y + 20) - - if head.direction == "down": - y = head.ycor() - head.sety(y - 20) - - if head.direction == "left": - x = head.xcor() - head.setx(x - 20) - - if head.direction == "right": - x = head.xcor() - head.setx(x + 20) - -# Keyboard bindings -wn.listen() -wn.onkeypress(go_up, "w") -wn.onkeypress(go_down, "s") -wn.onkeypress(go_left, "a") -wn.onkeypress(go_right, "d") - -# Main game loop -while True: - wn.update() - - # Check for a collision with the border - if head.xcor()>290 or head.xcor()<-290 or head.ycor()>290 or head.ycor()<-290: - time.sleep(1) - head.goto(0,0) - head.direction = "stop" - - # Hide the segments - for segment in segments: - segment.goto(1000, 1000) - - # Clear the segments list - segments.clear() - - # Reset the score - score = 0 - - # Reset the delay - delay = 0.1 - - pen.clear() - pen.write("Score: {} High Score: {}".format(score, high_score), align="center", font=("Courier", 24, "normal")) - - - # Check for a collision with the food - if head.distance(food) < 20: - # Move the food to a random spot - x = random.randint(-290, 290) - y = random.randint(-290, 290) - food.goto(x,y) - - # Add a segment - new_segment = turtle.Turtle() - new_segment.speed(0) - new_segment.shape("square") - new_segment.color("grey") - new_segment.penup() - segments.append(new_segment) - - # Shorten the delay - delay -= 0.001 - - # Increase the score - score += 10 - - if score > high_score: - high_score = score - - pen.clear() - pen.write("Score: {} High Score: {}".format(score, high_score), align="center", font=("Courier", 24, "normal")) - - # Move the end segments first in reverse order - for index in range(len(segments)-1, 0, -1): - x = segments[index-1].xcor() - y = segments[index-1].ycor() - segments[index].goto(x, y) - - # Move segment 0 to where the head is - if len(segments) > 0: - x = head.xcor() - y = head.ycor() - segments[0].goto(x,y) - - move() - - # Check for head collision with the body segments - for segment in segments: - if segment.distance(head) < 20: - time.sleep(1) - head.goto(0,0) - head.direction = "stop" - - # Hide the segments - for segment in segments: - segment.goto(1000, 1000) - - # Clear the segments list - segments.clear() - - # Reset the score - score = 0 - - # Reset the delay - delay = 0.1 - - # Update the score display - pen.clear() - pen.write("Score: {} High Score: {}".format(score, high_score), align="center", font=("Courier", 24, "normal")) - - time.sleep(delay) - -wn.mainloop() \ No newline at end of file diff --git a/ttgame.py b/ttgame.py new file mode 100644 index 0000000..2aee41e --- /dev/null +++ b/ttgame.py @@ -0,0 +1,208 @@ +from tkinter import * +from tkinter import ttk +import tkinter.messagebox + +root=Tk() +root.title("Tic Tac Toe") +#add Buttons +bu1=ttk.Button(root,text=' ') +bu1.grid(row=0,column=0,sticky='snew',ipadx=40,ipady=40) +bu1.config(command=lambda: ButtonClick(1)) + +bu2=ttk.Button(root,text=' ') +bu2.grid(row=0,column=1,sticky='snew',ipadx=40,ipady=40) +bu2.config(command=lambda: ButtonClick(2)) + +bu3=ttk.Button(root,text=' ') +bu3.grid(row=0,column=2,sticky='snew',ipadx=40,ipady=40) +bu3.config(command=lambda: ButtonClick(3)) + +bu4=ttk.Button(root,text=' ') +bu4.grid(row=1,column=0,sticky='snew',ipadx=40,ipady=40) +bu4.config(command=lambda: ButtonClick(4)) + +bu5=ttk.Button(root,text=' ') +bu5.grid(row=1,column=1,sticky='snew',ipadx=40,ipady=40) +bu5.config(command=lambda: ButtonClick(5)) + +bu6=ttk.Button(root,text=' ') +bu6.grid(row=1,column=2,sticky='snew',ipadx=40,ipady=40) +bu6.config(command=lambda: ButtonClick(6)) + +bu7=ttk.Button(root,text=' ') +bu7.grid(row=2,column=0,sticky='snew',ipadx=40,ipady=40) +bu7.config(command=lambda: ButtonClick(7)) + +bu8=ttk.Button(root,text=' ') +bu8.grid(row=2,column=1,sticky='snew',ipadx=40,ipady=40) +bu8.config(command=lambda: ButtonClick(8)) + +bu9=ttk.Button(root,text=' ') +bu9.grid(row=2,column=2,sticky='snew',ipadx=40,ipady=40) +bu9.config(command=lambda: ButtonClick(9)) + +playerturn=ttk.Label(root,text=" Player 1 turn! ") +playerturn.grid(row=3,column=0,sticky='snew',ipadx=40,ipady=40) + +playerdetails=ttk.Label(root,text=" Player 1 is X\n\n Player 2 is O") +playerdetails.grid(row=3,column=2,sticky='snew',ipadx=40,ipady=40) + +res=ttk.Button(root,text='Restart') +res.grid(row=3,column=1,sticky='snew',ipadx=40,ipady=40) +res.config(command=lambda: restartbutton()) + +a=1 +b=0 +c=0 +def restartbutton(): + global a,b,c + a=1 + b=0 + c=0 + playerturn['text']=" Player 1 turn! " + bu1['text']=' ' + bu2['text']=' ' + bu3['text']=' ' + bu4['text']=' ' + bu5['text']=' ' + bu6['text']=' ' + bu7['text']=' ' + bu8['text']=' ' + bu9['text']=' ' + bu1.state(['!disabled']) + bu2.state(['!disabled']) + bu3.state(['!disabled']) + bu4.state(['!disabled']) + bu5.state(['!disabled']) + bu6.state(['!disabled']) + bu7.state(['!disabled']) + bu8.state(['!disabled']) + bu9.state(['!disabled']) + +#after getting result(win or loss or draw) disable button +def disableButton(): + bu1.state(['disabled']) + bu2.state(['disabled']) + bu3.state(['disabled']) + bu4.state(['disabled']) + bu5.state(['disabled']) + bu6.state(['disabled']) + bu7.state(['disabled']) + bu8.state(['disabled']) + bu9.state(['disabled']) + + +def ButtonClick(id): + global a,b,c + print("ID:{}".format(id)) + + #for player 1 turn + if id==1 and bu1['text']==' ' and a==1: + bu1['text']="X" + a=0 + b+=1 + if id==2 and bu2['text']==' ' and a==1: + bu2['text']="X" + a=0 + b+=1 + if id==3 and bu3['text']==' ' and a==1: + bu3['text']="X" + a=0 + b+=1 + if id==4 and bu4['text']==' ' and a==1: + bu4['text']="X" + a=0 + b+=1 + if id==5 and bu5['text']==' ' and a==1: + bu5['text']="X" + a=0 + b+=1 + if id==6 and bu6['text']==' ' and a==1: + bu6['text']="X" + a=0 + b+=1 + if id==7 and bu7['text']==' ' and a==1: + bu7['text']="X" + a=0 + b+=1 + if id==8 and bu8['text']==' ' and a==1: + bu8['text']="X" + a=0 + b+=1 + if id==9 and bu9['text']==' ' and a==1: + bu9['text']="X" + a=0 + b+=1 + #for player 2 turn + if id==1 and bu1['text']==' ' and a==0: + bu1['text']="O" + a=1 + b+=1 + if id==2 and bu2['text']==' ' and a==0: + bu2['text']="O" + a=1 + b+=1 + if id==3 and bu3['text']==' ' and a==0: + bu3['text']="O" + a=1 + b+=1 + if id==4 and bu4['text']==' ' and a==0: + bu4['text']="O" + a=1 + b+=1 + if id==5 and bu5['text']==' ' and a==0: + bu5['text']="O" + a=1 + b+=1 + if id==6 and bu6['text']==' ' and a==0: + bu6['text']="O" + a=1 + b+=1 + if id==7 and bu7['text']==' ' and a==0: + bu7['text']="O" + a=1 + b+=1 + if id==8 and bu8['text']==' ' and a==0: + bu8['text']="O" + a=1 + b+=1 + if id==9 and bu9['text']==' ' and a==0: + bu9['text']="O" + a=1 + b+=1 + + #checking for winner + if( bu1['text']=='X' and bu2['text']=='X' and bu3['text']=='X' or + bu4['text']=='X' and bu5['text']=='X' and bu6['text']=='X' or + bu7['text']=='X' and bu8['text']=='X' and bu9['text']=='X' or + bu1['text']=='X' and bu4['text']=='X' and bu7['text']=='X' or + bu2['text']=='X' and bu5['text']=='X' and bu8['text']=='X' or + bu3['text']=='X' and bu6['text']=='X' and bu9['text']=='X' or + bu1['text']=='X' and bu5['text']=='X' and bu9['text']=='X' or + bu3['text']=='X' and bu5['text']=='X' and bu7['text']=='X'): + disableButton() + c=1 + tkinter.messagebox.showinfo("Tic Tac Toe","Winner is player 1") + elif( bu1['text']=='O' and bu2['text']=='O' and bu3['text']=='O' or + bu4['text']=='O' and bu5['text']=='O' and bu6['text']=='O' or + bu7['text']=='O' and bu8['text']=='O' and bu9['text']=='O' or + bu1['text']=='O' and bu4['text']=='O' and bu7['text']=='O' or + bu2['text']=='O' and bu5['text']=='O' and bu8['text']=='O' or + bu3['text']=='O' and bu6['text']=='O' and bu9['text']=='O' or + bu1['text']=='O' and bu5['text']=='O' and bu9['text']=='O' or + bu3['text']=='O' and bu5['text']=='O' and bu7['text']=='O'): + disableButton() + c=1 + tkinter.messagebox.showinfo("Tic Tac Toe","Winner is player 2") + elif b==9: + disableButton() + c=1 + tkinter.messagebox.showinfo("Tic Tac Toe","Match is Draw.") + + if a==1 and c==0: + playerturn['text']=" Player 1 turn! " + elif a==0 and c==0: + playerturn['text']=" Player 2 turn! " + +root.mainloop() +