乌龟使三角形不同颜色 [英] Turtle make triangle different color

查看:43
本文介绍了乌龟使三角形不同颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我正在尝试复制这张图片:

Hi guys I'm trying to replicate this image:

快完成了,我只是有一个问题,三角形应该是黄色的,但似乎不起作用.

It's almost done I just have one issue, where the triangle is supposed to be yellow it isn't seeming to work.

我的:

代码:

fill(True)
fillcolor('green')
width(3)
forward(200)
left(120)
forward(200)
left(120)
forward(200)

fill(False)


right(180)
forward(100)
right(60)
forward(100)
left(120)




fill(True)
fillcolor('red')
forward(200)
left(120)
forward(200)
left(120)
forward(200)
fill(False)

任何帮助将不胜感激.(我不能在代码的第二部分添加黄色)

Any help would be appreciated. (I can't add yellow to the second part of the code)

推荐答案

据我所知,turtle 不支持透明度.绿色三角形将覆盖红色三角形,它们重叠的区域不会添加"到黄色.您必须明确绘制黄色三角形.尝试将其添加到您的代码中:

To the best of my knowledge, turtle does not support transparency. The green triangle will overlay the red triangle and the area where they overlap will not "add" to yellow. You have to explicitly draw the yellow triangle. Try adding this to your code:

fill(True)
fillcolor('yellow')
left(120)
forward(100)
left(120)
forward(100)
left(120)
forward(100)
fill(False)

此外,定义一个函数 def triangle(size, color) 以减少代码中的重复量可能是个好主意,例如像这样:

Also, it might be a good idea to define a function def triangle(size, color) to reduce the amount of repetition in your code, e.g. like this:

def triangle(size, color):
    fill(True)
    fillcolor(color)
    for _ in range(3):      
        forward(size)
        left(120)
    fill(False)

然后你只需要定位海龟并调用该函数来绘制一个三角形.

Then you just have to position the turtle and call that function to draw a triangle.

这篇关于乌龟使三角形不同颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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