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

How to realize Spiral Graph in turtle Library for getting started with python

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article shows you how python entry turtle library to achieve spiral graph, concise and easy to understand, absolutely can make you shine, through the detailed introduction of this article I hope you can gain something.

Keep track of the frequently used functions in the turtle library.

turn.forward(distance) Move the brush forward distance

twist. backward (distance) The brush moves backward distance

turn.right(degree) Turn the drawing direction to the right degree

turtle.exitonclick() Click to close the graphics window

turtle.penup() Lift brush, then move brush without drawing shape

turtle.pendown() Drop brush, then move brush to draw shape

turtle.pensize() Sets the brush width

turtle. pencil () Set brush color, common color {white,black,grey,dark,green,gold,violet,purple}

CSDN has helped me a lot since I started learning python. I wrote a few simple turtle drawings these days. Haha, I am interested in it. So I will share my three codes. They are purely original. Don't spray them if you don't like them.

I use VSCODE. Personally, I think it works well. It's just that Turtle reported an error and I haven't found a solution yet. However, the problem is not big.

The first, simplest square spiral graph I call circle 1.0.

import turtle n = 500turtle.penup()turtle.goto(-450,150)turtle.pendown()turtle.pencolor("blue") for i in range(500): n = n - 1 #n -= 1 turtle.speed(100) turtle.fd(n) turtle.right(90) turtle.pendone()#circle 1.0

Set the brush position in advance to start at (-450, 150) Adjust the brush color to blue, and then a for loop, in turn let the brush draw the distance shorter, for loop

Turtle.fd(n) decrements by one every time it cycles, and then you have the most basic version, circle 1.0.

The results are as follows:

Okay, the easiest way to do that is to start adding things, and I can use

n -= 1#makes the drawn image draw from the border to the center, so you can also use n += 1#to make the drawn image draw from the center to the border.

OK Circle 1.1 out

import turtle n = 0 #turtle.penup()turtle.goto(-450,150)turtle.pendown()turtle.pencolor("blue") for i in range(500): n = n + 1 # #n += 1 turtle.speed(100) turtle.fd(n) turtle.right(90) turtle.pendone()#circle 1.1

Circle 1.1 results as follows:

Circle 1.1

Where there is a #is where there is a difference from 1.0.

Coming soon Circle 1.2, you can merge 1.0 and 1.1 into one picture, overlap the two pictures or re-raise the pen to change the starting point

import turtle n = 500turtle.speed(100)turtle.penup()turtle.goto(-450,150)turtle.pendown()turtle.pencolor("blue") for i in range(1000): if i

< 500: n = n - 1 turtle.fd(n) turtle.right(90) else: n+=1 turtle.pencolor('red') turtle.fd(n) turtle.right(90) turtle.pendone() #转圈圈1.2 运行结果如下: 转圈圈1.2 接下来,就再次增加新的东西一层一层的分颜色来绘制 因为我的n值为800,然后取了8个颜色(黑红橙黄绿蓝蓝靛紫) import turtle turtle.penup()turtle.goto(-450,300)turtle.pendown()turtle.speed(100)n = 800for i in range(10000): n -= 1 if 800 >

= n >700: turtle.pencolor('purple') elif 700 >= n > 600: turtle.pencolor("indigo") elif 600 >= n > 500: turtle.pencolor("blue") elif 500 >= n > 400: turtle.pencolor("green") elif 400 >= n > 300: turtle.pencolor("yellow") elif 300 >= n > 200: turtle.pencolor("orange") elif 200 >= n > 100: turtle.pencolor("red") elif n > 0: turtle.pencolor("black") else: turtle.done() turtle.fd(n) turtle.right(90) Round 1.3

The results are as follows:

On this basis, we set the value of n to be greater than 0. What happens when the value of n is less than zero? Think about it, the first line of code listed below, relative to the drawing of the second line must be the opposite direction of drawing, according to the set n-=1, n value less than zero after the absolute value is larger and larger, then the spiral curve drawn should be larger and larger, then, on the basis of the above figure covered with a layer of color, then it becomes our circle 1.4

turtle.fd(-100)turtle.fd(100)

Modify the last elif and else judgment, that is, turn circle 1.4

import turtle turtle.penup()turtle.goto(-450,300)turtle.pendown()turtle.speed(100)n = 800for i in range(10000): n -= 1 if 800 >= n >700: turtle.pencolor('purple') elif 700 >= n > 600: turtle.pencolor("indigo") elif 600 >= n > 500: turtle.pencolor("blue") elif 500 >= n > 400: turtle.pencolor("green") elif 400 >= n > 300: turtle.pencolor("yellow") elif 300 >= n > 200: turtle.pencolor("orange") elif 200 >= n > 100: turtle.pencolor("red") elif n > 0: turtle.pencolor("black") # else: # turtle.pendone() elif n

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