Hello everyone, the editor will answer the following questions for you, draw colorful hexagons with python, how to draw a flower with python, let's take a look now!
---
title: example card 7 turtle draws snowflakes
tags: turtle,draw
---
import module
Import the turtle library and python's random
import turtle as p
import random
draw snowflakes
def snow(snow_count):
p.hideturtle()
p.speed(500)
p.pensize(2)
for i in range(snow_count):
r = random.random()
g = random.random()
b = random.random()
p.pencolor(r, g, b)
p.pu()
p.goto(random.randint(-350, 350), random.randint(1, 270))
p.pd()
dens = random.randint(8, 12)
snowsize = random.randint(10, 14)
for _ in range(dens):
p.forward(snowsize) # Move the snowsize pixel length to the current brush direction
p.backward(snowsize) # Move the snowsize pixel length in the opposite direction to the current brush
p.right(360 / dens) # move 360 / dens degrees clockwise
draw the ground
def ground(ground_line_count):
p.hideturtle()
p.speed(500)
for i in range(ground_line_count):
p.pensize(random.randint(5, 10))
x = random.randint(-400, 350)
y = random.randint(-280, -1)
r = -y / 280
g = -y / 280
b = -y / 280
p.pencolor(r, g, b)
p.penup() # lift up the brush
p.goto(x, y) # Let the brush move to this position
p.pendown() # put down the brush
p.forward(random.randint(40, 100)) # The current brush direction of the eye moves forward 40~100 distance
main function
def main():
p.setup(800, 600, 0, 0)
# p.trace(False)
p.bgcolor("black")
snow(30)
ground(30)
# p.tracer(True)
p.mainloop()
main()
Dynamic graph result display:
snowflakes
Case open source address in Github: https://github.com/jackzhenguo/python-small-examplesgithub.com
The small case is still very interesting, if you find it useful, remember to give me a thumbs up! Comparing