2301_81606124 2023-12-03 22:22 采纳率: 0%
浏览 8

如何用python画20个颜色、大小、位置随机的五角星

如何用python画20个颜色、大小、位置随机的五角星?!,、

  • 写回答

3条回答 默认 最新

  • 叶灼hua 2023-12-03 23:34
    关注
    
    import turtle
    import random
    
    # 创建窗口
    wn = turtle.Screen()
    wn.setup(width=600, height=600)
    
    
    # 定义五角星函数
    def draw_pentagram(x, y, color, size):
        turtle.penup()
        turtle.goto(x, y)
        turtle.pendown()
        turtle.color(color)
        turtle.begin_fill()
        for i in range(5):
            turtle.forward(size)
            turtle.right(144)
        turtle.end_fill()
    
    
    # 绘制二十个五角星
    for i in range(20):
        # 随机位置和颜色和大小
        rand_x = random.randint(-250, 250)
        rand_y = random.randint(-250, 250)
        rand_color = random.choice(['red', 'orange', 'yellow', 'green', 'blue', 'purple'])
        size = random.randrange(-200, 200)
    
        draw_pentagram(rand_x, rand_y, rand_color, size)
    # 隐藏画笔
    turtle.hideturtle()
    turtle.done()
    
    
    评论

报告相同问题?

问题事件

  • 创建了问题 12月3日