Oct 12, 2021

Introduction to Turtle Graphics

Turtle graphics with python

Turtle graphics

Turtle is a python package which is used to create graphics, designs and it provides virtual canvas. It is pre-installed in python. Turtle provides interactive features that give us flexibility to work with python. Turtle was developed by Wally Feurzeig, Seymour Parpet and Cynthina Slolomon in 1967. Turtle graphics are associated with logo programming language. Logo is an educational programming language which allows us to draw graphs on the screen in a simple way. It is mainly known for its use of turtle graphics.

The turtle has three attributes: location (pixel), direction and pen. The pen also has attributes width, color and On/Off (up and down) state. The turtle moves with given commands, for example turtle.forward(20), this command moves the turtle 20 pixels in the direction it is facing and draws a line as it moves. turtle.right(25) This command turns the turtle in place 25 degrees clockwise. We can, intricate shapes and pictures can easily be drawn By using these two and others similar commands.

Basic Concept of Turtle Graphics

To work with turtle graphics to ensure that the python is installed in our system. The turtle is inbuilt in python so we do not need to install it explicitly. We can use applications such as IDLE, Spyder, jupyter and python. We can import the library by using the ‘import turtle’ command. This library consists of all the methods and functions that we will need to create graphics and design.

Some important methods of turtle

Turtle() : - This method creates and returns new turtle objects.

right() : This method is used to change the direction clockwise of the turtle by the value of the argument that it takes, For example right(90), It turns the turtle in place 90 degrees clockwise.

left() : This method is used to change the direction anticlockwise of the turtle by the value of the argument that it takes, For example left(90), It turns the turtle in place 90 degrees anticlockwise.

forward() : This method is used to move the turtle forward by the value of the argument that it takes. For example forward(20), It moves the turtle 20 pixels in the direction it is facing.

backward() : This method is used to move the turtle backward by the value of the argument that it takes. For example backward(20), It moves the turtle 20 pixels in the direction it is facing.

penup() or up() : This method is used when you want to move the turtle without drawing a line.

pendown() or down() : This method is used to tell the turtle to draw a line.

color() : This method is used to change the color of the turtle/pen used to draw a line of that color..

fillcolor() : This method is used to change the color of the turtle to fill a polygon.

position() : This method is used to return the current position.

goto(x,y) : This method is used to move the turtle to position x,y

Uses of Turtle graphics

It is mainly used for creating games and animation.

Some Simple program of Turtle graphics

  1. Example Code :

Draw simple line
 
# import turtle library
 
import turtle
 
line = turtle.Turtle()
 
#draw line up
 
line.forward(200)
 
turtle.done()
 
turtle.mainloop()

Output :

Draw simple line using turtle graphics

2. Example code :

In the following Example we see how to use penup() and pendown()

# import package
 
import turtle
 
# forward the turtle (drawing)
 
turtle.forward(70)
 
# up the turtle
 
turtle.penup()
 
# forward the turtle (no drawing)
 
turtle.forward(70)
 
# down the turtle
 
turtle.pendown()
 
# forward the turtle (drawing)
 
turtle.forward(70)

Output :

Draw line using penup() and pendown()

3. Example code

Draw square
 
import turtle
 
square = turtle.Turtle()
 
for i in range(4):
 
square.forward(200)
 
square.right(90)
 
turtle.done()

Output :

Draw square using turtle graphics

4. Example Code

Draw star

# import turtle library
 
import turtle
 
star = turtle.Turtle()
 
for i in range(5):
 
#draw a aline
 
star.forward(200)
 
#turn 144 degree clockwise
 
star.right(144)
 
turtle.done()

Output :

Draw star using turtle graphics

Common projects built in python using Turtle module.

Snake Game : In this game, the player uses the arrow keys to move a "snake" around the board. As soon as the snake gets food, it eats and thereby grows larger. when the snake either moves off the screen or moves into itself then the game is over.

snake game

Pong game : In this game, the player controls a game paddle by moving it across the right and left side of the screen. They compete with the opponent player controlling a second paddle on the opposing side. The player uses the paddle to hit the ball back and forth. Points are earned when one of them fails to return the ball to the other and The goal of the each player is to reach eleven points before the opponents.

pong game

21 Game : The 21 Game is a two-player counting game where each player can add 1, 2, or 3, starting at zero, to the total. the total must not exceed 21 and the player who lands on 21 loses.

21 game

Pac-Man : The main goal of this game is to eat all the dots placed in the maze and avoid the four colored ghosts red, yellow, blue and violet. When the pac-man eats all the dots, the player is the winner and advances to the next level. if the pac-man makes contact with a ghost, he will lose a life, when all the lives are lost then the game is over.

pac-man game

Space invaders game : In this game, the space invaders is a fixed shooter in which the player moves a laser cannon across the bottom of the screen and fires on enemies. The enemies come first as five rows of eleven that move left and right in the group and shift downward each time they reach a screen edge. The player has three lives. If the player makes contact with enemies or the invaders reach the bottom of the screen, then the game will end and he will lose a life. When all lives are lost the game is over.

space invaders game

How Codersarts can Help you in Turtle Graphics ?

Codersarts provide:

  • Turtle Graphics Assignment help

  • Turtle Graphics Error Resolving Help

  • Mentorship in Turtle Graphics from Experts

  • Turtle Graphics Development Project

If you are looking for any kind of Help in Turtle Graphics Contact us