Learn to make a Tic Tac Toe Game in Scratch (2024)

Kids usually love to play a lot of games, especially the ones where they have opponents to compete with.

One of the most interesting traditional games that helps develop cognitive skills in kids is the game Tic Tac Toe.

In this blog, I talk about how to create a Tic Tac Toe game using Scratch Programming.

Table of contents

  • What is Tic Tac Toe?
  • Introduction to Scratch
  • Steps to create a Tic Tac Toe Game in Scratch

What is Tic Tac Toe?

In some countries, Tic Tac Toe is also known as noughts and crosses.

Learn to make a Tic Tac Toe Game in Scratch (1)

This board game is usually played by two people wherein a 3×3 grid, one player must mark “X,” while the other must mark “O.” Players take turns marking “X” and “O.”

A player wins if they get three of their “X” or “O” in the same row, column, or diagonal. The game ends in a tie when the board is completely filled without any player winning.

Introduction to Scratch

Scratch is a block-based programming language that allows children to create games and animations while learning to think creatively.

Scratch is simple to learn because the blocks or codes are divided into categories such as Motion, Looks, Sound, Events, Controls, Sensing, Operators, Variables, and My Blocks.

Learn to make a Tic Tac Toe Game in Scratch (2)

Kids can easily debug their code in Scratch because the Code Area, where they place their codes, and the Stage, where the output is displayed, can both be seen at the same time.

Steps to create a Tic Tac Toe Game in Scratch

To create a game or an app, we must focus on three factors:

  1. Designing
  2. Coding
  3. Debugging

So, now let us start with the design part.

Designing

In Scratch, we must first create a backdrop.

Learn to make a Tic Tac Toe Game in Scratch (3)

After choosing the backdrop, create your Sprites. Click on Choose the sprite and add a button sprite.

Learn to make a Tic Tac Toe Game in Scratch (4)

Here we need 9 buttons to make a 3×3 grid

Learn to make a Tic Tac Toe Game in Scratch (5)

Each button should have 2 costumes marking X and O

In the backdrop, we have to add 2 costumes one for Game over if X wins and another for Game over if O wins.

Learn to make a Tic Tac Toe Game in Scratch (6)

Coding

Now I’ll go over the coding for the sprite Button1. The code for all nine buttons is the same. As a result, you can easily copy the codes and paste them into other buttons.

Code for Buttons

First, we have to create two variables named Button for storing the value “x” or “o” and a global variable for taking turns by the players and initializing them to zero.

Learn to make a Tic Tac Toe Game in Scratch (7)

When the first player clicks any button the costume will get switched to the “x” button. Now store the value”x” in the variable button and change the global variable by 1.

The next turn goes to player 2. When Player2 clicks another button the costume should be changed to the “o” button.

So here, we have to give the condition that if the global variable is 1, switch costume to the “o” button and store the value ”o” in the variable button. The broadcast message at the end of the script is used to check who wins the game X or O.

Learn to make a Tic Tac Toe Game in Scratch (8)

When the backdrop switches to either “X Wins” or “O Wins”, the buttons should be hidden.

Learn to make a Tic Tac Toe Game in Scratch (9)

Code for Backdrop

When you click the green flag, the backdrop should be set to the first backdrop. The backdrop should change only after the game is finished.

Learn to make a Tic Tac Toe Game in Scratch (10)

Following that, when we receive the broadcast message, we must examine all of the possibilities of “X” and “O” winning the game. If any of the conditions listed below in the image is met, “X” wins the game, and the Backdrop changes to “X Wins Game Over.”

Repeat for “O” and consider all the possibilities of “O” winning the game; if “O” wins, change the backdrop to “O Wins Game Over.”

Run the Project

View the entire coding and visualise the output by clicking on this link for a better understanding.

Conclusion

It is one of the most simple projects for beginners to create in Scratch.

This game was designed by me in such a way that only humans can play it, yet it is simple enough for beginners to understand. Here is a link to another version of the game which can also be played by a human and a computer.

Learn Scratch at BrightChamps with its specially designed curriculum that makes learning programming easy for students in Grade 1-12. BrightChamps also teaches a variety of other programmes that help kids build a foundation in computer programming through activities, assignments, and other means.

Frequently Asked Questions (FAQs)

How to draw a different backdrop instead of choosing a backdrop available in Scratch?

You can choose the paint option in Backdrop and draw your backdrop.

What is the use of the Variable Button in Tic Tac Toe game?

The Variable ”button” is used to store the values “x” and “o”

What is the use of the Global Variable in this game?

The Variable named global variable is used so that the players can get turns. For example, if the first player marks “X,” the second player should mark “O,” not “X.” To accomplish this, I used a global variable whose value alternates between 0 and 1. If the global variable is 0, “X” is marked; if the global variable is 1, “O” is marked.

Can we include scores for the two players?

Yes, you can add two variables ‘Score for player1’ and ‘Score for player2’, and add the block “Change score by 1” after “X” wins and “O” wins

Ask Us Anything!

Learn to make a Tic Tac Toe Game in Scratch (2024)
Top Articles
Latest Posts
Article information

Author: Lakeisha Bayer VM

Last Updated:

Views: 6654

Rating: 4.9 / 5 (49 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Lakeisha Bayer VM

Birthday: 1997-10-17

Address: Suite 835 34136 Adrian Mountains, Floydton, UT 81036

Phone: +3571527672278

Job: Manufacturing Agent

Hobby: Skimboarding, Photography, Roller skating, Knife making, Paintball, Embroidery, Gunsmithing

Introduction: My name is Lakeisha Bayer VM, I am a brainy, kind, enchanting, healthy, lovely, clean, witty person who loves writing and wants to share my knowledge and understanding with you.