Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the installation and principle analysis of turtle library?

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the installation and principle analysis of turtle library, the content is very detailed, interested friends can refer to, hope to be helpful to you.

Turtle library is a very popular drawing function library in Python, which mainly draws images according to coordinate axes. The brush is a small sea turtle, which draws all kinds of images by controlling the movement of sea turtles in the coordinate plane.

Install turtlepip install turtle canvas

The canvas in turtle is the area used for painting, and we can set its size and initial position.

Common canvas methods:

Screensize () method

Syntax: turtle.screensize (canvwidth=None, canvheight=None, bg=None) canvwidth-wide canvheight-high bg-background color example: import turtle, timeturtle.screensize (800,600, "green") turtle.screensize () # returns the default size (400,300) time.sleep (3)

Setup () method

Syntax: turtle.setup (width=0.5, height=0.75, startx=None, starty=None) width, height-when the input width and height are integers, pixels are represented. When decimal, it represents the scale startx and starty that occupy the computer screen-- this coordinate indicates the position of the vertex in the upper left corner of the rectangular window. If empty, the window is in the center of the screen. Example: import turtle, timeturtle.screensize (800,600, "green") turtle.screensize () # returns the default size (400,300) time.sleep (3) brush

Brushes have properties such as color, width of lines, and so on:

Turtle.pensize (): sets the width of the brush

Turtle.pencolor (): returns the current brush color with no parameters passed in. The passed parameter sets the brush color, which can be a string such as "red", "blue", or a RGB 3 tuple.

Turtle.speed (): sets the speed of brush movement. The speed range of brush painting is an integer. The larger the number, the faster the number.

Drawing command

There are many commands to manipulate turtle drawing, which can be divided into three commands, namely: brush motion command, brush control command and global control command.

The brush motion command command states that turtle.forward (distance) moves distance pixel length turtle.backward (distance) in the opposite direction of the current brush turtle.right (degree) moves distance pixel length clockwise. Left (degree) moves degree °turtle.pendown () draws graphics when moving counterclockwise, and also by default to draw turtle.goto (x field y) to move the brush to the coordinate x The position of y is not drawn when turtle.penup () is moved, pick up the pen For drawing from another place, the speed range of drawing with a turtle.speed (speed) brush [0Jing 10] integer turtle.circle () draws a circle with a positive (negative) radius, indicating that the center of the circle is on the left (right) of the brush command description of the width of turtle.pensize (width) when drawing turtle.pencolor () Brush Color turtle.fillcolor (colorstring) the fill color of drawing turtle.color (color1, color2) also sets pencolor=color1 Fillcolor=color2turtle.filling () returns whether the current filling state turtle.begin_fill () is ready to start filling the drawing turtle.end_fill () fill finishes turtle.hideturtle () Hidden Arrow display The global control command corresponding to the turtle.showturtle () and hideturtle () functions states that turtle.clear () clears the turtle window, but the position and state of the turtle does not change the turtle.reset () empty window. Reset turtle state to start state turtle.undo () undo the last turtle action turtle.isvisible () returns whether the current turtle is visible stamp () copy the current graphic turtle.write (s [, font= ("font-name", font_size, "font_type")]) write text, s is the text content, font is the font parameter, which contains the font name, size and type, respectively Font is optional, and the parameters of font are optional.

Draw a pentagram

Import turtleimport timeturtle.setup (1500, 400,0,0) turtle.pensize (5) turtle.pencolor ("pink") turtle.fillcolor ("red") turtle.begin_fill () for _ in range (5): turtle.forward (1500) turtle.right (144C) turtle.end_fill () time.sleep (5)

I saw an example before, you can draw a beautiful cherry tree, if you are interested, you can take a look at it.

Link: https://blog.csdn.net/z564359805/article/details/85861481#commentBox

The specific code is as follows:

#! / usr/bin/env python# coding=utf-8# draw a cherry blossom import turtleimport randomfrom turtleimport * from time import sleep# draw the torso of the cherry blossom (60) def tree (branchLen, t): sleep (0.0005) if branchLen > 3: if 8

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report