22 Python 3 - Turtle

2022/4/1 22:19:51

本文主要是介绍22 Python 3 - Turtle,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

 

Turtle is a special feathers of Python. Using Turtle, we can easily draw in a drawing board.

First we import the turtle module. Then create a window, next we create turtle object and using turtle method we can draw in the drawing board.

 

import turtle
  
# moves the pen in the 
# forward direction by 
# 110 pixels
turtle.forward(110)
  
# changes the direction of 
# the pen by 110 degrees in the
# left direction
turtle.left(110)
  
# moves the pen in the 
# forward direction in 
# the new direction by
# 110 pixels
turtle.forward(110)

 

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 turtle.done().

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 = turtle.Screen()
wn.bgcolor("light green")
wn.title("Turtle")
skk = turtle.Turtle()

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: 

skk.forward(100)

We have moved skk 100 pixels forward, Awesome! Now we complete the program with the done() function and We’re done! 

turtle.done()

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.

Some turtle method

METHODPARAMETERDESCRIPTION
Turtle() None It creates and returns a new turtle object
forward() amount It moves the turtle forward by the specified amount
backward() amount It moves the turtle backward by the specified amount
right() angle It turns the turtle clockwise
left() angle It turns the turtle counter clockwise
penup() None It 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 It returns the current heading
position() None It returns the current position
goto() x, y It moves the turtle to position x,y
begin_fill() None Remember the starting point for a filled polygon
end_fill() None It closes the polygon and fills with the current fill color
dot() None Leaves the dot at the current position
stamp() None Leaves an impression of a turtle shape at the current location
shape() shapename Should be ‘arrow’, ‘classic’, ‘turtle’ or ‘circle’

Example code

# import turtle library
import turtle             
my_window = turtle.Screen() 
my_window.bgcolor("blue")       # creates a graphics window
my_pen = turtle.Turtle()      
my_pen.forward(150)           
my_pen.left(90)               
my_pen.forward(75)
my_pen.color("white")
my_pen.pensize(12)
 

Output

 

 

Draw a Square

Example code

# import turtle library
import turtle             
my_pen = turtle.Turtle()      
for i in range(4):
   my_pen.forward(50)           
   my_pen.right(90)               
turtle.done()

Output

 

 

Draw a star

Example code

# import turtle library
import turtle             
my_pen = turtle.Turtle()      
for i in range(50):
   my_pen.forward(50)           
   my_pen.right(144)               
turtle.done()

Output

 

 

Draw a Hexagon

Example code

# import turtle library
import turtle             
polygon = turtle.Turtle()
my_num_sides = 6
my_side_length = 70
my_angle = 360.0 / my_num_sides
for i in range(my_num_sides):
   polygon.forward(my_side_length)           
   polygon.right(my_angle) 
turtle.done()

 

 

Output

 

 

Draw a square inside another square box.

Example code

# import turtle library
import turtle             
my_wn = turtle.Screen()
my_wn.bgcolor("light blue")
my_wn.title("Turtle")
my_pen = turtle.Turtle()
my_pen.color("black")
def my_sqrfunc(size):
   for i in range(4):
      my_pen.fd(size)
      my_pen.left(90)
      size = size - 5
my_sqrfunc(146)
my_sqrfunc(126)
my_sqrfunc(106)
my_sqrfunc(86)
my_sqrfunc(66)
my_sqrfunc(46)
my_sqrfunc(26)

 

 

Output

 

 

Drawing of another pattern

Example code

# import turtle library
import turtle             
my_wn = turtle.Screen()
turtle.speed(2)
for i in range(30):
   turtle.circle(5*i)
   turtle.circle(-5*i)
   turtle.left(i)
turtle.exitonclick()
 

Output

 

 

 

Drawing of another pattern

Example code

# import turtle library
import turtle             
colors = [ "red","purple","blue","green","orange","yellow"]
my_pen = turtle.Pen()
turtle.bgcolor("black")
for x in range(360):
   my_pen.pencolor(colors[x % 6])
   my_pen.width(x/100 + 1)
   my_pen.forward(x)
   my_pen.left(59)

Output

 

 

 

 

REF

https://www.tutorialspoint.com/turtle-programming-in-python

https://www.geeksforgeeks.org/turtle-programming-python/

https://www.geeksforgeeks.org/python-turtle-tutorial/

 



这篇关于22 Python 3 - Turtle的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程