Advanced turtle (turtle) plotting (continued) and pythonturtle in Python
4. Color Filling
ColorThe function has three parameters. The first parameter specifies the red color, the second specifies the green color, and the third specifies the blue color. For example, to get the bright red color of the car, we use color (, 0), that is, let the turtles use a red paint brush.
This red, green, and blue mix is calledRGB (Red, Green, Blue). Because red, green, and blue are the three primary colors on the color light, any color can be adjusted by changing the proportion of the three primary colors.
Although we are not mixing pigments on computer screens (we use light !), However, we can think of the RGB solution as three paint bins, one red, one green and one blue. Every bucket is full. We can see that the value of a bucket is 1 (100% ). Then all the red and green pigments are mixed in a large tank, which produces yellow.
Now we try to draw a yellow circle with turtles. We need to use 100% of red and green paint, not blue:
The effect is as follows:
>>> T. color (100%, 0) # 100% red, 0% green, blue >>> t. begin_fill () # color the back shape> t. circle (50)> t. end_fill () # Fill the circle with RGB color
Yellow circle
1, used to draw a function that fills the circle
To make it easier to experiment with different colors, we can write the circle coloring code as a function:
>>> def mycircle(red,green,blue): t.color(red,green,blue) t.begin_fill() t.circle(50) t.end_fill()
We can use only green to draw A bright green circle.:
>>> mycircle(0,1,0)
You can also use half of the green color (0.5) to draw a dark green circle. B:
>>> mycircle(0,0.5,0)
A: B:
2. Use pure white and pure black
When the sky does not have the sun, the world becomes dark (assuming we haven't invented the light at that time ). By analogy, if you set the three colors to 0, it is equivalent to no light. At this time, the image is black, and vice versa.
>>> mycircle(0,0,0)
5. Draw a Square Function
>>> def mysquare(size): for x in range(1,5): t.forward(size) t.left(90)
>>> mysquare(25)>>> mysquare(50)>>> mysquare(75)>>> mysquare(100)>>> mysquare(125)
The effect is as follows:
6. Draw a colored square
To fill the square Color, first reset the canvas, start to fill the color, and then call the square function. The process is as follows:
>>> T. reset () >>> t. begin_fill () >>> mysquare (50) >>> t. end_fill () # before adding this line of code, you should see an empty square until you end Filling
The effect is as follows:
Now we can change this function so that it can draw a filled square or a non-filled square.
>>> def mysquare(size,filled): if filled == True: t.begin_fill() for x in range(1,5): t.forward(size) t.left(90) if filled == True: t.end_fill()
Below we can draw a square with a color filled:
>>> mysquare(50,True)
Then draw a square without coloring:
>>> mysquare(150,False)
7. Draw stars with good colors
Now we want to write a mystar function.
>>> Def mystar (size, filled): if filled = True: # Check whether filled is True. begin_fill () # If yes, start filling in for x in range (): t. forward (size) if x % 2 = 0: t. left (175) else: t. left (225) if filled = True: t. end_fill ()
Mystar Functions
Now we can draw a Golden Star (90% red, 75% green, 0% blue)
>>> t.color(0.9,0.75,0)>>> mystar(120,True)
The effect is as follows:
To draw a silhouette for the stars, change the color to black and draw the stars again without coloring them:
>>> t.color(0,0,0)>>> mystar(120,False)
The effect is as follows:
Summary
I forgot to eat and play games this afternoon. I have been learning Python plotting. I learned how to use the turtle module to draw several basic geometric figures, and to use the for loop and if statements to control the movements of turtles on the screen. At the same time, you can change the color of the turtle pen and fill in the shape it draws. Some functions (such as def functions) are used to reuse the drawing code, improving the efficiency.
The road is still long. Stick to it! Sharing