0% found this document useful (0 votes)
13 views11 pages

Python Turtle Programming Guide

The document provides an overview of Turtle programming in Python, detailing its methods and functionalities for drawing shapes. It includes step-by-step instructions for creating a turtle graphics program, along with example codes for drawing various shapes such as squares, stars, hexagons, and circles. Additionally, it mentions advanced turtle programs and introduces basic C graphics functions for drawing a moving cycle.

Uploaded by

sasi.venki86
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views11 pages

Python Turtle Programming Guide

The document provides an overview of Turtle programming in Python, detailing its methods and functionalities for drawing shapes. It includes step-by-step instructions for creating a turtle graphics program, along with example codes for drawing various shapes such as squares, stars, hexagons, and circles. Additionally, it mentions advanced turtle programs and introduces basic C graphics functions for drawing a moving cycle.

Uploaded by

sasi.venki86
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Turtle Programming in Python

Last Updated : 21 Mar, 2024



“Turtle” is a Python feature like a drawing board, which lets us command a


turtle to draw all over it! We can use functions like [Link](...) and
[Link](...) which can move the turtle around. Commonly used turtle
methods are :

Method Parameter Description

Turtle() None Creates and returns a new turtle object

forward() amount Moves the turtle forward by the specified amount

backward(
amount Moves the turtle backward by the specified amount
)

right() angle Turns the turtle clockwise

left() angle Turns the turtle counterclockwise

penup() None Picks up the turtle's Pen

pendown() None Puts down the turtle's Pen

up() None Picks up the turtle's Pen

down() None Puts down the turtle's Pen

color() Color name Changes the color of the turtle's pen

fillcolor() Color name Changes the color of the turtle will use to fill a polygon

heading() None Returns the current heading


Method Parameter Description

position() None Returns the current position

goto() x, y Move the turtle to position x,y

begin_fill(
None Remember the starting point for a filled polygon
)

end_fill() None Close the polygon and fill with the current fill color

dot() None Leave the dot at the current position

Leaves an impression of a turtle shape at the current


stamp() None
location

shape() shapename Should be 'arrow', 'classic', 'turtle' or 'circle'

Plotting using Turtle


To make use of the turtle methods and functionalities, we need to import
turtle."turtle" comes packed with the standard Python package and need
not be installed externally. The roadmap for executing a turtle program
follows 4 steps:

1. Import the turtle module


2. Create a turtle to control.
3. Draw around using the turtle methods.
4. Run [Link]().
So as stated above, before we can use turtle, we need to import it. We
import it as :
from turtle import *
# or
import turtle
After importing the turtle library and making all the turtle functionalities
available to us, we need to create a new drawing board(window) and a
turtle. Let's call the window as wn and the turtle as skk. So we code as:
wn = [Link]()
[Link]("light green")
[Link]("Turtle")
skk = [Link]()
Now that we have created the window and the turtle, we need to move the
turtle. To move forward 100 pixels in the direction skk is facing, we code:
[Link](100)
We have moved skk 100 pixels forward, Awesome! Now we complete the
program with the done() function and We're done!
[Link]()
So, we have created a program that draws a line 100 pixels long. We can
draw various shapes and fill different colors using turtle methods. There's
plethora of functions and programs to be coded using the turtle library in
python. Let's learn to draw some of the basic shapes.

Shape 1: Square

# Python program to draw square


# using Turtle Programming
import turtle
skk = [Link]()

for i in range(4):
[Link](50)
[Link](90)

[Link]()
Output:

Shape 2: Star
# Python program to draw star
# using Turtle Programming
import turtle
star = [Link]()

[Link](75)
[Link](100)

for i in range(4):
[Link](144)
[Link](100)

[Link]()
Output:

Shape 3: Hexagon

# Python program to draw hexagon


# using Turtle Programming
import turtle
polygon = [Link]()

num_sides = 6
side_length = 70
angle = 360.0 / num_sides

for i in range(num_sides):
[Link](side_length)
[Link](angle)

[Link]()
Output:
Shape 4: parallelogram

import turtle

# Initialize the turtle


t = [Link]()

# Set the turtle's speed


[Link](1)

# Draw the parallelogram


for i in range(2):
[Link](100)
[Link](60)
[Link](50)
[Link](120)
Output:

Shape 5 : Circle
import turtle

# Set up the turtle screen and set the background color to white
screen = [Link]()
[Link]("white")

# Create a new turtle and set its speed to the fastest possible
pen = [Link]()
[Link](0)

# Set the fill color to red


[Link]("red")
pen.begin_fill()

# Draw the circle with a radius of 100 pixels


[Link](100)

# End the fill and stop drawing


pen.end_fill()
[Link]()

# Keep the turtle window open until it is manually closed


[Link]()
Output:
Visit [Link] to get a taste of Turtle without having python pre-
installed. The shell in PythonTurtle is a full Python shell, and you can do
with it almost anything you can with a standard Python shell. You can
make loops, define functions, create classes, etc.
Some amazing Turtle Programs

1. Spiral Square Outside In and Inside Out

import turtle #Inside_Out


wn = [Link]()
[Link]("light green")
skk = [Link]()
[Link]("blue")

def sqrfunc(size):
for i in range(4):
[Link](size)
[Link](90)
size = size + 5

sqrfunc(6)
sqrfunc(26)
sqrfunc(46)
sqrfunc(66)
sqrfunc(86)
sqrfunc(106)
sqrfunc(126)
sqrfunc(146)
Output:

2. User Input Pattern

# Python program to user input pattern


# using Turtle Programming
import turtle #Outside_In
import turtle
import time
import random

print ("This program draws shapes based on the number you enter in a
uniform pattern.")
num_str = input("Enter the side number of the shape you want to draw: ")
if num_str.isdigit():
squares = int(num_str)

angle = 180 - 180*(squares-2)/squares

[Link]

x=0
y=0
[Link](x, y)

numshapes = 8
for x in range(numshapes):
[Link]([Link](), [Link](), [Link]())
x += 5
y += 5
[Link](x)
[Link](y)
for i in range(squares):
turtle.begin_fill()
[Link]()
[Link](40)
[Link](angle)
[Link](40)
print ([Link]())
[Link]()
turtle.end_fill()

[Link](11)
[Link]()
3. Spiral Helix Pattern

# Python program to draw


# Spiral Helix Pattern
# using Turtle Programming

import turtle
loadWindow = [Link]()
[Link](2)

for i in range(100):
[Link](5*i)
[Link](-5*i)
[Link](i)
[Link]()
Output:

4. Rainbow Benzene

# Python program to draw


# Rainbow Benzene
# using Turtle Programming
import turtle
colors = ['red', 'purple', 'blue', 'green', 'orange', 'yellow']
t = [Link]()
[Link]('black')
for x in range(360):
[Link](colors[x%6])
[Link](x//100 + 1)
[Link](x)
[Link](59)
Output:

2. Draw a moving cycle using computer


graphics programming in C/C++
Last Updated : 23 Jul, 2025



In C graphics, the graphics.h functions are used to draw different shapes


like circles, rectangles, etc, display text(any message) in a different format
(different fonts and colors). By using the functions in the header
graphics.h, programs, animations, and different games can also be made.
In this article, let's discuss how to draw a moving cycle in C
using graphics.
Functions used:
 line(x1, y1, x2, y2) : It is a function provided by graphics.h header
file to draw a line. Here x1, y1 is the first coordinates of the line, and
x2, y2 are the second coordinates of the line respectively.
 circle(x, y, r) : It is a function provided by graphics.h header file to draw
a circle. The x, y are the center points of the circle and r is the radius of
the circle.
 rectangle(X1, Y1, X2, Y2) : It is employed in the creation of a
rectangle. The rectangle must be drawn using the coordinates of the
left top and right bottom corners. The X-coordinate and Y-coordinate of
the top left corner are X1 and Y1 and the X-coordinate and Y-
coordinate of the bottom right corner are X2 and Y2 respectively.
 delay(n): It is used to hold the program for a specific time period. Here
n is the number of seconds you want to hold the program.
 cleardevice(): It is used to clear the screen in graphic mode. It sets the
position of the cursor to its initial position, that is, (0, 0) coordinates.
 closegraph(): It is used to close the graph.
Approach: Following are the steps below to generate a moving cycle:
 Pass the three arguments to the initgraph() function to initialize the
graphics driver and graphics mode.
 Create the upper body of the cycle by drawing lines.
 Create the wheels of the cycle by drawing circles and choose the
coordinates so that the wheels aligned just below the upper body of the
cycle.
 The next step is to create the road by drawing lines and stone by
drawing rectangles.
 Choose the coordinates so that the cycle is just above the road.
 Change the cycle's position using a loop continuously so that it
appears to be moving on the road.
Below is the implementation of the above approach:
// C++ program to draw the moving
// cycle using computer graphics

#include <conio.h>
#include <dos.h>
#include <graphics.h>
#include <iostream.h>

// Driver code
int main()
{
int gd = DETECT, gm, i, a;

// Path of the program


initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
// Move the cycle
for (i = 0; i < 600; i++) {
// Upper body of cycle
line(50 + i, 405, 100 + i, 405);
line(75 + i, 375, 125 + i, 375);
line(50 + i, 405, 75 + i, 375);
line(100 + i, 405, 100 + i, 345);
line(150 + i, 405, 100 + i, 345);
line(75 + i, 345, 75 + i, 370);
line(70 + i, 370, 80 + i, 370);
line(80 + i, 345, 100 + i, 345);

// Wheel
circle(150 + i, 405, 30);
circle(50 + i, 405, 30);

// Road
line(0, 436, getmaxx(), 436);

// Stone
rectangle(getmaxx() - i, 436,
650 - i, 431);

// Stop the screen for 10 secs


delay(10);

// Clear the screen


cleardevice();
}

getch();

// Close the graph


closegraph();
}

You might also like