In this video we’ll start to build out a Tic Tac Toe game with Kivy and Python!

Tic Tac Toe is a simple game, but it’ll allow us to learn how to use basic logic with Kivy.

In this video we’ll build out the GUI for our game, as well as the restart button functionality.

#kivy #codemy

Python Code: toe.py
GitHub Code: toe.py

from kivy.lang import Builder
from kivymd.app import MDApp

class MainApp(MDApp):
	def build(self):
		self.theme_cls.theme_style = "Dark"
		self.theme_cls.primary_palette = "BlueGray"
		return Builder.load_file('toe.kv')
	
	# Define Who's turn it is
	turn = "X"
	

	

	def presser(self, btn):
		if self.turn == 'X':
			btn.text = "X"
			btn.disabled = True
			self.root.ids.score.text = "O's Turn!"
			self.turn = "O"
		else:
			btn.text = "O"
			btn.disabled = True
			self.root.ids.score.text = "X's Turn!"
			self.turn = "X"

		# Check To See if won
		self.win()

	def restart(self):
		# Reset Who's Turn It Is
		self.turn = "X"

		# Enable The Buttons
		self.root.ids.btn1.disabled = False
		self.root.ids.btn2.disabled = False
		self.root.ids.btn3.disabled = False
		self.root.ids.btn4.disabled = False
		self.root.ids.btn5.disabled = False
		self.root.ids.btn6.disabled = False
		self.root.ids.btn7.disabled = False
		self.root.ids.btn8.disabled = False
		self.root.ids.btn9.disabled = False

		# Clear The Buttons
		self.root.ids.btn1.text = ""
		self.root.ids.btn2.text = ""
		self.root.ids.btn3.text = ""
		self.root.ids.btn4.text = ""
		self.root.ids.btn5.text = ""
		self.root.ids.btn6.text = ""
		self.root.ids.btn7.text = ""
		self.root.ids.btn8.text = ""
		self.root.ids.btn9.text = ""

		
		# Reset The Score Label
		self.root.ids.score.text = "X GOES FIRST!"

	
MainApp().run()

Kivy Design Code: toe.kv
GitHub Code: toe.kv

MDFloatLayout:

	MDGridLayout:
		size_hint: .5, .5
		pos_hint: {'center_x': .5, 'center_y': .7}
		cols: 3
		rows: 3

		Button:
			id: btn1
			text: ""
			font_size: "45sp"
			on_release: app.presser(btn1)

		Button:
			id: btn2
			text: ""
			font_size: "45sp"
			on_release: app.presser(btn2)

		Button:
			id: btn3
			text: ""
			font_size: "45sp"
			on_release: app.presser(btn3)

		Button:
			id: btn4
			text: ""
			font_size: "45sp"
			on_release: app.presser(btn4)

		Button:
			id: btn5
			text: ""
			font_size: "45sp"
			on_release: app.presser(btn5)

		Button:
			id: btn6
			text: ""
			font_size: "45sp"
			on_release: app.presser(btn6)

		Button:
			id: btn7
			text: ""
			font_size: "45sp"
			on_release: app.presser(btn7)

		Button:
			id: btn8
			text: ""
			font_size: "45sp"
			on_release: app.presser(btn8)

		Button:
			id: btn9
			text: ""
			font_size: "45sp"
			on_release: app.presser(btn9)

	MDLabel:
		id: score
		font_size: "32sp"
		text: "X GOES FIRST!"
		halign: "center"
		pos_hint: {"center_y": .3}

	MDRaisedButton:
		id: restart
		text: "Restart The Game"
		pos_hint: {'center_x': .5, 'center_y': .15}
		on_release: app.restart()

		

John Elder

John is the CEO of Codemy.com where he teaches over 100,000 students how to code! He founded one of the Internet's earliest advertising networks and sold it to a publicly company at the height of the first dot com boom. After that he developed the award-winning Submission-Spider search engine submission software that's been used by over 3 million individuals, businesses, and governments in over 42 countries. He's written several Amazon #1 best selling books on coding, and runs a popular Youtube coding channel.

View all posts

Add comment

Your email address will not be published. Required fields are marked *

John Elder