Python 生成随机颜色五角星
以下是一个生成随机颜色五角星的 Python 代码:
import turtle
import random
# 定义一个函数来绘制五角星
def draw_star(size, color):
    turtle.color(color)
    turtle.begin_fill()
    for i in range(5):
        turtle.forward(size)
        turtle.right(144)
    turtle.end_fill()
# 设置画布
turtle.bgcolor('black')
# 循环绘制五角星
for i in range(20):
    # 随机生成颜色
    r = random.randint(0, 255)
    g = random.randint(0, 255)
    b = random.randint(0, 255)
    color = (r, g, b)
    # 随机生成大小和位置
    size = random.randint(10, 50)
    x = random.randint(-turtle.window_width()//2, turtle.window_width()//2)
    y = random.randint(-turtle.window_height()//2, turtle.window_height()//2)
    # 绘制五角星
    turtle.penup()
    turtle.goto(x, y)
    turtle.pendown()
    draw_star(size, color)
# 隐藏海龟
turtle.hideturtle()
# 等待用户关闭画布
turtle.done()
这段代码使用了 Python 标准库中的 turtle 模块来绘制五角星。它使用了循环来绘制多个五角星,每个五角星的大小和颜色都是随机生成的。
原文地址: https://www.cveoy.top/t/topic/nmnJ 著作权归作者所有。请勿转载和采集!