程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Implementation example of color circle in Python loop

編輯:Python

Catalog

Colorful rings are more beautiful

A. Course content

B. Knowledge point

C. Basic instructions used

D. Draw beautiful circles

E. Add color to drawing

Colorful rings are more beautiful A. Course content

Learn how to use lists by drawing colored circles 、 The use of color skills , Master through learning python Program drawing color graphics method .

B. Knowledge point

(1) Draw a circle

(2) Set brush color

(3) Tips for changing brush colors

(4) List tips

C. Basic instructions used

(1)import

(2)turtle.Pen()

(3)circle()

(4)right()

(5)for loop

(6)range()

D. Draw beautiful circles

We have learned through t.forward(x) Statement to let the turtle paint a straight line segment , Can the turtle draw a circle ? Certainly. , Can pass t.circle(x) Statement to draw a radius of x The circle of . Actually , As long as the t.forward(x) Statement modified to t.circle(x) sentence , You can let the turtle draw a complex figure , As shown in the figure :

t.circle(x) Statement can be drawn with a radius of x The circle of , Every cycle ,x The value of is increased 1, The radius of the circle is increased 1. After drawing a circle , The brush rotates 90°.

Tips :

circle() If there is only one parameter in the command , Is the radius :

If you have two parameters , Such as circle(10,180), First parameter 10 As the radius of , The second parameter 180 Degrees . Add the second parameter to draw arcs and circles .circle(10,180) The drawn figure is a semicircle .

E. Add color to drawing

Before, we drew black figures , Next, let's have the turtle brush draw a colorful figure . To draw a colored figure , Just change the color of the turtle brush to another color . adopt t.pencolor(‘red’) Statement to set the brush color .

Common colors include bule( Blue )、green( green )、yellow( yellow )、white( white )、black( black )、purple( violet )、brown( Brown )、gray( gray ) etc. .

The above explains how to set the brush to one color , Can we use one color for the rings in each direction 》 It will be more beautiful . As shown in the figure below :

How does the above program execute ?

(1) First, import the turtle drawing module , And change the t Set as turtle's brush ; Then create the list colors, And add red、yellow、blue、green Four elements .

(2) next range(100) Will generate from 0 To 99 A continuous number of ;t.circle(x) The sentence asks the turtle brush to draw a picture with x Is a circle of radius ;t.right(90) Statement to turn the turtle brush to the right 90°.

(3) When the loop is executed for the first time ,for Loop first accesses range The first element in the list 0, And will 0 Stored in variables x in , Variable x The value of becomes 0,0 Divide 4 Get the remainder 0. And then execute colors[0], From the list colors Take the first element “red” As a parameter of brush color . Then set the brush color to red (red), And draw a radius of 0 The circle of , Then turn the brush to the right 90°

(4) Perform the second cycle ,for Loop access range The second element in the list 1, And will 1 Stored in variables x in , Variable x The value of becomes 1.1 Divide 4 Get the remainder 1, And then execute colors[1], From the list colors Take the second element from “yellow” As a parameter of brush color . Then set the brush color to yellow , Draw a radius of 1 The circle of , Then turn the brush to the right 90°.

(5) This continues to be done again and again , When for Loop traversal range The last element in 99 when , Variable x The value of a 99,99 Divide 4 Get the remainder 3, And then execute colors[3], From the list colors Take the fourth element from “green” As a parameter of brush color . Then set the brush color to green (green), Draw a radius of 99 The circle of , Then turn the brush to the right 90°. In this way, a figure of four colors is drawn .

Tips :A Divide B, If A Than B Small , Then the remainder after division is itself . such as 1 Divide 4, Remainder is 1.

That's all python Details of the implementation example of the color ring of the loop , More about python Please pay attention to other related articles of software development network for the information of circular color circle !



  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved