简体   繁体   English

我想在乌龟python中使用数组填充不同颜色的圆圈

[英]I want to fill circles with different colors using array in turtle python

在此输入图像描述 I made 4 circles like that using hardcode for each circle, but it's not efficient. 我用每个圆圈使用硬编码制作了四个这样的圆圈,但是效率不高。

this is my code, but I am confused how to access the array of color & the array of coordinate x & y, so that it can be accessed throug all the index. 这是我的代码,但是我很困惑如何访问颜色数组和坐标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()

I want to make it simple by accessing the arrays, hope someone can help. 我想通过访问数组来简化它,希望有人可以提供帮助。 Thanks 谢谢

Since we're looking at a fixed distance between circles, I'd toss the array of coordinates in favor of a starting position and an offset. 由于我们正在观察圆之间的固定距离,因此我将坐标数组抛向了起始位置和偏移量。 Then I'd loop simply on the array of colors: 然后,我将简单地在颜色数组上循环:

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()

在此输入图像描述

But there are lots of ways to go about a problem like this. 但是有很多方法可以解决这样的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM