# Simple Two Player Game using Turtle in Python

Python comes with a module called turtle. It offers drawing with a cardboard screen and a turtle (pen). Move the turtle (pen) to draw a thing on the screen. There are other functions, such as forward() and backward(), to move the turtle.

The TurtleMove game is primarily a game of chance. Two players (Pink and Green) use their own turtles as the game's object to play this game.

## Instructions to play the game :

The game is played on the predetermined grid with specific boundaries.

• The turtle is moved by both players a unit of space.
• Now both players will flip a coin. If HEAD, then they will turn right; otherwise, they will turn left.
• Continue with steps 1 and 2 until both turtles are in the boundary.

## Implementing Using Turtle Python :

• For the grid boundaries, a turtle screen object is first generated.
• For each player, Pink and Green turtles are now formed.
• Using the turtle forward() method, both turtles are moved a unit of distance.
• Using randrange(0, 2), or 0 for left and 1 for right, the turn is decided.
• Each turtle's position is checked after each move, and if any turtle crosses the line, the game is over for that turtle.

Here is how it was done:

## Walking through the code :

First of all, we will be importing the turtle library into our program and along with that we will also import the random library which helps to choose random instances.

Then, we will define a method to check whether the turtles are within the screen or not. To do so, we will set the four sides or the bounds of the screen with the help of the window_width() and window_height() methods.

Then, we will find the current position or coordinates of the turtle using the xcor() method to find the x-coordinate and ycor() method to find the y-coordinate.

Then we use the following condition to check whether turtle is within screen bounds or not. Initially, the stilIn variable is set to true and if it will exceed the bounds, then the condition would break and the result would be returned as false, if not true.

Then, we will define another method to make sure that the 2 turtles are placed at different locations.

Next, we will initialize the main() method and will start by setting the screen for drawing with turtles.

Now, will start to introduce the 2 turtles and their required characteristics. First, the pink turtle will be initialized by taking an instance of the turtle object. Then we will set the colour of the pen to pink using the pencolor() method. Next, we will set the size of the pen required for drawing, using the pensize() method. The shape of the turtle will be set to "turtle" using the shape method and pos() method will be used to get its position.

Next, the green turtle will be initialized by taking an instance of the turtle object. Then we will set the colour of the pen to green using the pencolor() method. Next, we will set the size of the pen required for drawing, using the pensize() method. The shape of the turtle will be set to "turtle" using the shape method and hideturtle() method will be used in order to hide the turtle (pen).

After hiding the turtle, we will lift the pen so that it does not draws when it moves, using the penup() method and will move it to a certain position using the goto() method. After reaching the mentioned position, we will use the showturtle() method to make the turtle visible again and will drop the pen so that it can be used for drawing again from that position only, using the pendown() method. We used this shifting of position of the green turtle so that we are able to clearly differentiate between the paths of the 2 turtles.

Then we introduced 2 new variables called mT and jT, which will store the value whether the turtles are inside the boundary mentioned or not. Both of them are initially set true, meaning the turtles are inside the screen presently.

We will now use a while loop to start playing the game. When the turtles will be inside the screen then only this loop will be valid. First, the flipping of coin will be checked and its value will be stored in the variable coinPink. This will be picked using randrange() method. Then the angle for the movement ahead of the turtle will also be found and the value will be stored in the variable anglePink.

Then, to decide whether the turtle should turn left of right will be decided by this if-else condition. If the vale of colorPink will be 0, turtle will turn left by anglePink degrees else it will turn right with the same.

The entire above process will be repeated for the green turtle also to determine its movements ahead.

If the above mentioned conditions satisfy, then both the turtles pink and green will move forward by 40 units in their respective directions because of the use of the forward() method.

After they have moved forward, we will again check for their positions that whether they are inside the screen or not and these updated values will again be stored in the variables mT and jT.

For displaying the result, the turtle pen colour for both the turtles will be set to black using the pencolor() method.

and jT variables. And the output will be printed if any of these condition satisfies using the write() method. We also set the font of the result to bold "arial" style of 15 unit size.

Lastly, the exitonclick() method was used to take an exit after the completion of the game. Hence, the main() function was called again.

## Complete Code :

Output

Some of the different outputs of the aforementioned game code are attached below.