Python 爱心烟花(Turtle 图形库)

import turtle

import random

 

turtle.bgcolor("black")

turtle.speed(0)

turtle.hideturtle()

 

def draw_heart(x, y, size, color):

    turtle.penup()

    turtle.goto(x, y)

    turtle.pendown()

    turtle.color(color)

    turtle.begin_fill()

    turtle.left(50)

    turtle.forward(size)

    turtle.circle(size * 0.4, 200)

    turtle.right(140)

    turtle.circle(size * 0.4, 200)

    turtle.forward(size)

    turtle.end_fill()

 

for _ in range(50):

    x = random.randint(-300, 300)

    y = random.randint(-300, 300)

    size = random.randint(10, 50)

    color = random.choice(["red", "pink", "orange", "yellow"])

    draw_heart(x, y, size, color)

 

turtle.done()

你可能感兴趣的:(python)