How to fill with multiple color in Python using turtle module?

6.8k views Asked by At

I am pretty new in Python, now trying to have fun with it before going into serious stuffs. I am trying to draw a simple one wheel cart type something, where body and cart will have different colors. i am trying this code:

import turtle
truck = turtle.Turtle()
truck.color("blue", "green")
truck.begin_fill ()
truck.fd(50)
truck.color("blue", "red")
truck.begin_fill ()
truck.circle(50)
truck.end_fill()
truck.fd(50)
for i in range(2):
    truck.right(90)
    truck.fd (100)
truck.right(90)
truck.fd(100)
truck.end_fill ()

This code only fill red color in the wheel. but the box remains uncolored. But if I remove extra color filling codes for circle, then my full structure becomes green.

I know by using penup and pendown, I can draw it with ease. Like, by using this code:

import turtle
truck = turtle.Turtle()
truck.color("blue", "green")
truck.begin_fill ()
for i in range(4):
   truck.right(90)
   truck.fd (100)
truck.end_fill ()
truck.penup()
truck.bk(50)
truck.pendown()
truck.color("blue", "red")
truck.begin_fill ()
truck.circle(50)
truck.end_fill()

Can I somehow tweak the first code sothat I wouldn't need to use penup and pendown methods?

1

There are 1 answers

2
Alex Ivanov On BEST ANSWER
#!/usr/bin/python
# -*- coding: utf-8 -*-

import turtle
truck = turtle.Turtle()
truck.fd(50)

truck.color("blue", "red")
truck.begin_fill ()
truck.circle(50)
truck.fd(50)
truck.end_fill()

truck.color("blue", "green")
truck.begin_fill ()
for i in range(2):
    truck.right(90)
    truck.fd (100)
truck.right(90)
truck.fd(100)
truck.end_fill ()

turtle.done()

enter image description here