Drawing a Sun using Python Turtle Module

Introduction

The Logo programming language is frequently linked to turtle graphics. In the late 1960s, Seymour Papert added turtle graphics support to Logo to support his version of the turtle robot, which is a simple robot controlled from the user’s workstation and designed to carry out the drawing functions assigned to it using a small retractable pen set into or attached to the robot’s body.

The standard library of the Python programming language now contains a Turtle graphics module. Turtle in Python, like its Logo ancestor, allows programmers to manipulate one or more turtles in a two-dimensional space.

Overview of the syntax

A location, an orientation (or direction), and a pen are the three qualities of the turtle. Color, width, and on/off state are all properties of the pen (also called down and up).

“Move ahead 10 spaces” and “turn left 90 degrees” are orders that the turtle responds to based on its current location. The turtle’s pen can also be managed by enabling it, changing its color, and adjusting its breadth. By visualizing what they would do if they were the turtle, a pupil may comprehend (and forecast and reason about) the turtle’s motion. This is referred to as “body syntonic” reasoning by Seymour Papert.

Basic syntax:

import turtle // start of the program
     //body
    //of the main
   //code
turtle.done() //end of the program

Inorder to understand the codes to draw various shapes given below, Getting Started with Powerful yet Easy Python Graphics Module, Turtle.

Methods

A Python method is a label that can be applied to an object and is a piece of code that may be run on that object.

The most frequently, used turtle methods are:

MethodParameterDescription
Turtle()NoneCreates and returns a new turtle object
forward()amountMoves the turtle forward by the specified amount
backward()amountMoves the turtle backward by the specified amount
right()angleTurns the turtle clockwise
left()angleTurns the turtle counterclockwise
penup()NonePicks up the turtle’s Pen
pendown()NonePuts down the turtle’s Pen
up()NonePicks up the turtle’s Pen
down()NonePuts down the turtle’s Pen
color()Color nameChanges the color of the turtle’s pen
fillcolor()Color nameChanges the color of the turtle will use to fill a polygon
heading()NoneReturns the current heading
position()NoneReturns the current position
goto()x, yMove the turtle to position x,y
begin_fill()NoneRemember the starting point for a filled polygon
end_fill()NoneClose the polygon and fill with the current fill color
dot()NoneLeave the dot at the current position
stamp()NoneLeaves an impression of a turtle shape at the current location
shape()shapenameShould be ‘arrow’, ‘classic’, ‘turtle’ or ‘circle’

Sun Made with Python Turtle Module

The principles governs everything in every subject. They’re the basis on which all incredible exploits are built. To execute acts that appear magical to us simple humans, great scientists and artists equally rely on their most basic skills. Same is the case for python turtle module.

In order to make any drawings in python turtle, for this instance a sun, the fundamentals are mandatory. If you think about it a sun is only a color circle with lines around it. Enough of explaining, let’s get coding!

The following is the code for drawing a sun using python turtle module:

# importing the turtle python module
import turtle

screen = turtle.Screen()

# background color
screen.bgcolor("teal")

# turtle object
sun = turtle.Turtle()

# define function
# for drawing rays of Sun
def drawFourRays(t, length, radius):
	
	for i in range(4):
		t.penup()
		t.forward(radius)
		t.pendown()
		t.forward(length)
		t.penup()
		t.backward(length + radius)
		t.left(90)


# Draw circle
# to make sun
sun.penup()
sun.goto(85, 110)
sun.fillcolor("yellow")
sun.pendown()
sun.begin_fill()
sun.circle(45)
sun.end_fill()

# Use the defined
# function to draw rays
sun.penup()
sun.goto(85, 169)
sun.pendown()
drawFourRays(sun, 85, 54)
sun.right(45)
drawFourRays(sun, 85, 54)
sun.left(45)

# To keep the
# output window active
turtle.done()

From above code implementation of a sun in python turtle module. We can observe,

  • Turtle module should be imported.
  • Set up a turtle screen.
  • Create a turtle screen.
  • Draw the sun.
# Draw circle
# to make sun
sun.penup()
sun.goto(85, 110)
sun.fillcolor("yellow")
sun.pendown()
sun.begin_fill()
sun.circle(45)
sun.end_fill()
  • Define function for making the sun rays.
# define function
# for drawing rays of Sun
def drawFourRays(t, length, radius):
	
	for i in range(4):
		t.penup()
		t.forward(radius)
		t.pendown()
		t.forward(length)
		t.penup()
		t.backward(length + radius)
		t.left(90)
  • Use the user defined function to draw the sun rays around the sun.
# Use the defined
# function to draw rays
sun.penup()
sun.goto(85, 169)
sun.pendown()
drawFourRays(sun, 85, 54)
sun.right(45)
drawFourRays(sun, 85, 54)
sun.left(45)
  • As for the color we can see the sun is yellow and the background color is teal.
# Yellow color for the sun
sun.fillcolor("yellow")

# Teal color for the background
screen.bgcolor("teal")

Output

The below is the output for the code above. We can see that the sun is yellow, the background is teal and the lines or the sun rays are black in color.

SHARE Drawing a Sun using Python Turtle Module

You may also like...

Leave a Reply

Your email address will not be published.

Share