01 logo

Draw helix and cool patterns using turtle library python

using turtle package in python

By CodeuniqPublished 4 years ago 3 min read
1

Today we will be using the python turtle package and draw simple patterns. Turtle is a really useful python package for beginners and programming kids. It is a drawing tool that can be used draw using simple codes. After importing turtle package , just give it the command turtle.forward(25), and it moves 15 pixels in the direction it is facing on the screen.

What is Turtle Library?

In computer graphics, turtle graphics are vector graphics using a relative cursor (the "turtle") upon a Cartesian plane. Turtle graphics is a key feature of the Logo programming language.It was part of the original Logo programming language developed by Wally Feurzig and Seymour Papert in 1966.The turtle module is an extended re-implementation of the same-named module from the Python standard distribution up to version Python 2.5.

The turtle module provides turtle graphics primitives, in both object-oriented and procedure-oriented ways. Because it uses tkinter for the underlying graphics, it needs a version of Python installed with Tk support.

The turtle has three attributes: a location, an orientation (or direction), and a pen. The pen, too, has attributes: color, width, and on/off state.

The turtle moves with commands that are relative to its own position, such as "move forward 10 spaces" and "turn left 90 degrees". The pen carried by the turtle can also be controlled, by enabling it, setting its color, or setting its width.

These are the common methods used for Move and draw

forward() backward()

right() left()

goto() setpos() setposition()

setx() sety()

speed()

Things are better understandable if it is explained with a practical example.There are a lot of sample programs available online. So, lets try out some practical examples:

First of all lets try drawing a rainbow on the screen using same package:

For making a rainbow using the turtle library for that we have to draw seven semi circles of different radius with seven colours. lets check that out...

#importing the turtle package

#“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 turtle.forward(…) and turtle.right(…)

#which can move the turtle around.

import turtle

# create a new drawing board(window)-->scr and a turtle object-->pen

scr=turtle.Screen();

pen=turtle.Turtle();

#setting the colors for drawing rainbow

col=['violet','indigo','blue','green','yellow','orange','red']

#setting screen feature and pen configuration

scr.setup(800,600)

scr.bgcolor('black')

pen.right(90) #trutle turns clockwise

pen.width(10) #set thickness

pen.speed(7) #drawing speed

# method to form semi-circles as in a rainbow

def drawSemicircle(colr, radius, position):

pen.color(colr) #set the color each time

pen.circle(radius,-180) #draw a circle

pen.up() #picks up turtle pen

pen.setpos(position,0) #move turtle pen to this position

pen.down() #puts down the turtle pen

pen.right(180) #turns the turtle clock wise

#loop to draw the seven semi circles

for i in range(7):

drawSemicircle(col[i],10*(i+8),-10*(i+1))

#exit turtle window

turtle.bye();

Now let's draw a helix pattern using turtle.

Helix is an object having a three-dimensional shape like that of a wire wound uniformly in a single layer around a cylinder or cone, as in a corkscrew or spiral staircase.

Now lets try making a helix using the turtle library

import turtle #import the turtle package and set colours and screen

colors = ['red', 'purple', 'blue', 'green', 'orange', 'yellow']

scr=turtle.Screen();

t=turtle.Turtle();

scr.setup(800,600) #setting window dimensions

scr.bgcolor('black')

for x in range(360): #loop for drawing the helix pattern

t.pencolor(colors[x%6])

t.width(x/100 + 1)

t.forward(x)

t.left(59)

turtle.bye()

You can see the output of the program in the above mentioned video.

Try out some more cool and complex patterns. As this would be a really cool and fun way to learn for beginners in python. Explore different methods used in this topic and improve your skills.

Hope you liked this content stay tuned for more

Thankyou.......

how to
1

About the Creator

Codeuniq

We are a team of enthusiastic techies and we'll update you with latest tech news and tips along with a bit of programming.

Reader insights

Be the first to share your insights about this piece.

How does it work?

Add your insights

Comments

There are no comments for this story

Be the first to respond and start the conversation.

Sign in to comment

    Find us on social media

    Miscellaneous links

    • Explore
    • Contact
    • Privacy Policy
    • Terms of Use
    • Support

    © 2024 Creatd, Inc. All Rights Reserved.