我想用turtle python中的数组填充不同颜色的圆圈

2024-09-30 12:17:37 发布

您现在位置:Python中文网/ 问答频道 /正文

enter image description here 我用硬代码做了4个圆,但是效率不高。在

这是我的代码,但是我很困惑如何访问color数组和坐标x&y数组,以便可以通过所有索引访问它。在

from turtle import *
setup()
title('4 CIRCLES')

col = ['yellow', 'green', 'blue', 'red']
x = [100,65,30,5]
y = [100,65,30,5]

def lingkaran(number, rad = 50) :
    for cir in range(number) :
        penup()
        goto(x, y)
        pendown()
        color(col)
        begin_fill()
        circle(rad)
        end_fill()
        lingkaran(4)

hideturtle()
done()

我想通过访问数组来简化它,希望有人能帮忙。 谢谢


Tags: 代码fromimportnumbertitlesetupcol数组
1条回答
网友
1楼 · 发布于 2024-09-30 12:17:37

因为我们观察的是一个固定的圆之间的距离,所以我会用坐标数组来代替起始位置和偏移量。然后我只在颜色数组上循环:

from turtle import *

title('4 CIRCLES')

COLORS = ['yellow', 'green', 'blue', 'red']

def lingkaran(colors, position, offset, radius, pen_width=3):
    width(pen_width)

    for color in colors:
        penup()
        goto(position)
        pendown()

        fillcolor(color)
        begin_fill()
        circle(radius)
        end_fill()

        position += offset

lingkaran(COLORS, Vec2D(-100, 100), Vec2D(35, -35), 50)

hideturtle()
done()

enter image description here

但对于这样的问题,有很多方法可以解决。在

相关问题 更多 >

    热门问题