简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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