One can create a game in many ways the way used in this blog is not absolute.
The purpose of this blog is to let the users understand unity and coding a bit better than before.
Step1: Images needed for the project.
We will be needing 4 images to create this game.
1-Cross
2-Circle
3-Board
4-Background
Step2: Setting up the Scene.
Set the game view to 2d.
1. MainCamera- Select the main camera and under Camera component it has a property named Projection set it to Orthographic.
2. GameOver- Create an empty gameobject and add sprite renderer to it. Add a sprite image to it by clicking on sprite property. Set the Color to black or any other color as per your preference.
Import the image by simply dragging the script from explorer to unity under project panel. Select the image and change it texture type to sprite 2D and UI.
3. BackGround- Used to create the black background at the back.
4. Create an empty gameobject and name it Board, Add a sprite renderer to it by simply clicking on add component. Create an image in any photo editor software like this:-
5. Now create an empty gameobject under board gameobject and add box collider and a spirit renderer to it. After that make 8 more copies of its (Ctrl+D).Name those in descending order starting from 9 to 1.
Now arrange them accordingly as shown below.
6. Create a canvas and inside a canvas create a text as right click UI>Text.
7. Create a UI Button same as created the text. Name the Button as New game and the text under button as BttnTxt. Naming them correct is important as we are going to use them in scripts.
8.Arrange all the items And at the end your Scene will look like this:
Step3: Writing the script.
Step3.1:-Onclick on a box the spirit of zero or cross must be assigned to that box.
To do that first we have to know the box we hit and then change its spirit only once so that once a box spirit is assigned to zero/cross should not change on second click on it.
Select all the box collider under board and add a tag on them naming “Boxes”.
Now write the following script-
In this script first we will create a list which will find all the boxes in board and add them to the list. Secondly, we want an array to store are 2 cross/zero spirit.
Now in start function the script will find all the gameobjects with tag “Boxes” and will store them into the list.
In update function firstly we check if a user clicks the let mouse button a ray from the main camera is shooted towards mouse position and if the mouse is on any box the ray hits the collider.
If the hit.collider is not null than boxcollider it hits was store in a local variable box and is disabled to ensure that user cannot click on same box twice.
Secondly the spirit is assigned to the gameobject.
Right now it is only adding zero in all boxes.
Step 3.2: Making zero/Cross appear alternatively.
To do this I have used a Boolean variable to make it false/true.
On true zero will appear and on false cross will appear.
Script-
Step3.3: Checking the patterns to generate the result who wins or is match a draw.
This can be done in several ways.
Either we can check the spirit names or can change the gameobject names or can change the name of gameobjects stored in our list.
I will do this by checking are list as it will save the extra coding required to call all the spirits.
First onclick on gameobject we have to change its name as well as update our list.
Secondly we have to check that all the Boxes stored in our list are in order.
Script-
Step3.4: Check for the pattern and activating our gameover gameobject showing text who wins and new game button to refresh/start a new game.
Checking a pattern is quite easy all we have to do is read the gameobject names and if gameobject names matches horizontally, vertically or diagonally the user wins.
Script-
Now some additional thing added in code are the newbttn variable to assign new button, gameover variable to assign gameover gameobject .You have to also add UI library to your script so we can use text properties.
Assign gameover and new button to your script in the inspector.Play the game and we are done with the Tic Tac Toe game.
Only thing left is to refresh the game when click on new button.
Step4; Making refresh script to refresh all things changed in game to start state in play mode.
Create another script name refresh and attach to board.
Than select your NewGameBttn and add the script to its onclick event.
Script-
At last add a line in your zeroKhata Script.
At each place where we are checking the pattern.And we are done.
Thanks for the support till now. The post is open to suggestion and edit. Do comments your opinions.
Till than Happy Coding.
Post A Comment:
0 comments: