Home >Backend Development >Python Tutorial >How to fill colors in graphics in python
Using Turtle, you can not only draw lines, but also fill the drawn closed lines.
-Set fill color: fillecolor(r, g, b)
-Start filling: begin_fill()
-End filling: end_fill()
Draw a set of randomly distributed hearts with random sizes and different tones. First initialize a fill color. Then, use begin_fill() before drawing each shape and end_fill() after drawing. This will give you a filling effect.
Related recommendations: "Python Video Tutorial"
import turtle as t import random as r def pink(): color = (1, r.random(), 1) return color def randomrange(min, max): return min + (max- min)*r.random() def moveto(x, y): t.penup() t.goto(x, y) t.pendown() def heart(r, a): factor = 180 t.seth(a) t.circle(-r, factor) t.fd(2 * r) t.right(90) t.fd(2 * r) t.circle(-r, factor) t.setup(800, 800, 200, 200) t.speed(9) t.pensize(1) t.penup() for i in range(20): t.goto(randomrange(-300, 300), randomrange(-300, 300)) t.begin_fill() t.fillcolor(pink()) heart(randomrange(10, 50), randomrange(0, 90)) t.end_fill() moveto(400, -400) t.done()
The results are as follows:
The above is the detailed content of How to fill colors in graphics in python. For more information, please follow other related articles on the PHP Chinese website!