海龟图形,画一颗星星? [英] Turtle graphics, draw a star?

查看:36
本文介绍了海龟图形,画一颗星星?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想画一个实心的星星,如:

I want to draw a filled-in star, such as:

到目前为止我有这个代码:

I have this code so far:

def draw_star(size,color):
    count = 0
    angle = 144
    while count <= 5:
        turtle.forward(size)
        turtle.right(angle)
        count += 1
    return

draw_star(100,"purple")

我想用函数传递的任何颜色填充星形.我该怎么做?

I want to fill in the star with whatever color the function is passed. How can I do this?

推荐答案

要获得 5 角星,您应该为每边画 2 条线.角度需要加到 72 (360/5)

To get a 5 pointed star, you should draw 2 lines for each side. The angles need to add to 72 (360/5)

import turtle
def draw_star(size, color):
    angle = 120
    turtle.fillcolor(color)
    turtle.begin_fill()

    for side in range(5):
        turtle.forward(size)
        turtle.right(angle)
        turtle.forward(size)
        turtle.right(72 - angle)
    turtle.end_fill()
    return

draw_star(100, "purple")

用不同的angle值进行实验,得到你想要的形状

Experiment with different values of angle to get the shape you want

这篇关于海龟图形,画一颗星星?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆