1 Introduction
Turtle drawing is very suitable to guide children to learn programming. Originally from the Logo programming language created in 1967 by Wally Feurzeig, Seymour Papert and Cynthia Solomon.
The turtle module provides basic components for drawing turtles in both object-oriented and procedural-oriented forms. Since it uses tkinter for its basic graphical interface, it requires a version of Python that supports Tk.
(1) Object-oriented interface Mainly use "2+2" classes: All methods of TurtleScreen/Screen also have corresponding functions, which are part of the process-oriented interface. All methods of RawTurtle/Turtle also have corresponding functions, i.e. as part of the procedure-oriented interface.
(2) Procedural interface The procedural interface provides functions corresponding to the methods of the Screen and Turtle classes. The function name is the same as the corresponding method name. When the corresponding function of the method of the Screen class is called, a Screen object will be created automatically. An (anonymous) Turtle object is automatically created when the corresponding function of the method of the Turtle class is called.
If you need to have multiple turtles on the screen, you must use an object-oriented interface.
2. Interface description
2.1 Turtle action
2.1.1 Moving and drawing
Order
illustrate
forward() 、 fd()
go ahead
backward() 、 bk() 、 back()
step back
right() 、 rt()
Turn right
left() 、 lt()
Turn left
goto() 、 setpos() 、 setposition()
go to/locate
setx()
set the x-coordinate
sets()
set the y coordinate
setheading()、 seth()
set orientation
home()
return to origin
circle()
draw a circle
dot()
draw dots
stamp()
seal
clearstamp()
clear stamp
clearstamps()
Clear multiple stamps
undo()
undo
speed()
speed
2.1.2 Get the status of the turtle
Order
illustrate
position() 、 pos()
Location
towards()
target direction
xcor()
x-coordinate
ycor()
y-coordinate
heading()
toward
distance()
distance
2.2 Brush Control
2.2.1 Drawing Status
Order
illustrate
pendown() 、pd() 、down()
brush drop
penup() 、 pu() 、 up()
brush raised
pensize() 、 width()
brush thickness
pen()
brush
isdown()
whether the brush falls
2.2.2 Color Control
Order
illustrate
color()
color
pencolor()
brush color
fillcolor()
fill color
2.2.3 Padding
Order
illustrate
filling()
Whether to fill
begin_fill()
start filling
end_fill()
end padding
2.2.4 More drawing controls
Order
illustrate
reset()
reset
clear()
empty
write()
write
2.3 TurtleScreen/Screen Method
2.3.1 Window Control
Order
illustrate
bgcolor()
background color
bgpic()
Background picture
clearscreen()
empty
resetscreen()
reset
screensize()
screen size
setworldcoordinates()
Set the world coordinate system
2.3.2 Using screen events
Order
illustrate
listen() monitor
onkey() 、onkeyrelease()
When the keyboard is pressed and released
onkeypress()
when the keyboard is pressed
onclick() 、 onscreenclick()
when the screen is tapped
ontimer()
when the timing is reached
mainloop()、done()
main loop
2.3.3 Screen proprietary methods
Order
illustrate
bye()
quit
exitonclick()
exit when clicked
setup()
set up
title()
title
3. Example test
3.1 Turtle star
from turtle import *
title( 'turtle绘图 - 爱看书的小沐' )
color( 'red' , 'yellow' )
speed( 10 )
begin_fill( )
while True :
forward( 200 )
left( 170 )
if abs ( pos( ) ) < 1 :
break
end_fill( )
done( )
or
import turtle as t
t. color( "red" , "yellow" )
t. speed( 10 )
t. begin_fill( )
for _ in range ( 50 ) :
t. forward( 200 )
t. left( 170 )
t. end_fill( )
t. done( )
3.2 Fractal Trees
import turtle
toplevel = 8
angle = 30
rangle = 15
def drawTree ( length, level) :
turtle. left( angle)
turtle. color( "black" )
turtle. forward( length)
if level == toplevel:
turtle. color( "pink" )
turtle. circle( 2 , 360 )
if level < toplevel:
drawTree( length - 10 , level + 1 )
turtle. back( length)
turtle. right( angle + rangle)
turtle. color( "black" )
turtle. forward( length)
if level == toplevel:
turtle. color( "pink" )
turtle. circle( 2 , 360 )
if level < toplevel:
drawTree( length - 10 , level + 1 )
turtle. color( "black" )
turtle. back( length)
turtle. left( rangle)
def writePoem ( ) :
turtle. color( "blue" )
turtle. penup( )
turtle. goto( 150 , - 120 )
turtle. write( '碧玉妆成一树高,' , font= ( "隶书" , 15 ) )
turtle. goto( 150 , - 140 )
turtle. write( '万条垂下绿丝绦。' , font= ( "隶书" , 15 ) )
turtle. goto( 150 , - 160 )
turtle. write( '不知细叶谁裁出,' , font= ( "隶书" , 15 ) )
turtle. goto( 150 , - 180 )
turtle. write( '二月春风似剪刀。' , font= ( "隶书" , 15 ) )
turtle. hideturtle( )
turtle. left( 90 )
turtle. penup( )
turtle. back( 300 )
turtle. pendown( )
turtle. forward( 100 )
turtle. speed( 11 )
drawTree( 80 , 1 )
writePoem( )
turtle. done( )
3.3 Ballooning
from turtle import *
from random import randrange, choice
balloons= [ ]
color_option= [ "red" , "blue" , "green" , "purple" , "pink" , "yellow" , "orange" , "black" ]
size= 50
def line ( x, y, a, b, line_width= 1 , color_name= "black" ) :
up( )
goto( x, y)
down( )
color( color_name)
width( line_width)
goto( a, b)
def distance ( x, y, a, b) :
return ( ( a- x) ** 2 + ( b- y) ** 2 ) ** 0.5
def tap ( x, y) :
for i in range ( len ( balloons) ) :
if distance( x, y, balloons[ i] [ 0 ] , balloons[ i] [ 1 ] ) < ( size/ 2 ) :
balloons. pop( i)
return
def draw ( ) :
clear( )
for i in range ( 1 , ( len ( balloons) + 1 ) ) :
line( balloons[ - i] [ 0 ] , balloons[ - i] [ 1 ] , balloons[ - i] [ 0 ] , balloons[ - i] [ 1 ] - size* 1.5 , 1 )
up( )
goto( balloons[ - i] [ 0 ] , balloons[ - i] [ 1 ] )
dot( size, balloons[ - i] [ 2 ] )
balloons[ - i] [ 1 ] = balloons[ - i] [ 1 ] + 1
update( )
def gameLoop ( ) :
if randrange( 0 , 50 ) == 1 :
x= randrange( - 200 + size, 200 - size)
c= choice( color_option)
balloons. append( [ x, - 200 - size, c] )
draw( )
ontimer( gameLoop, 10 )
if __name__ == "__main__" :
setup( 600 , 350 , 0 , 0 )
bgpic( r'C:\Users\tomcat\desktop\20220919041856.png' )
hideturtle( )
tracer( False )
listen( )
onscreenclick( tap)
gameLoop( )
done( )
3.4 turtledemo — collection of demo scripts
The turtledemo package is a collection of demo scripts. These scripts can be run and viewed by opening the provided demo viewer with the following command:
python -m turtledemo
python -m turtledemo.bytedesign
epilogue
如果您觉得该方法或代码有一点点用处,可以给作者点个赞,或打赏杯咖啡;
╮( ̄▽ ̄)╭ 如果您感觉方法或代码不咋地
//(ㄒoㄒ)// ,就在评论处留言,作者继续改进;
o_O??? 如果您需要相关功能的代码定制化开发,可以留言私信作者;
(✿◡‿◡) 感谢各位大佬童鞋们的支持!
(´▽´)ノ (´▽´)! ! !