проблемы с закрашиваемой областью при использовании turtle

KateYa

Новичок
Пользователь
Ноя 30, 2020
2
0
1
Добрый день. Начали с ребенком изучать Python. Столкнулись с такой проблемой: не очень понятно, как черепашка выбирает область для закрашивания. В частности, рисовали лепестки: без использования for они правильно закрасились, а когда стали рисовать их в цикле, то раскрашивание "испортилось". Подскажите, пожалуйста, что неправильно.

ОС windows, Python 3.2.5

Код без цикла:

Код:
from turtle import *
from math import *

speed(10)
pensize(1)
colormode(255)
pencolor("cyan")
fillcolor("cyan")

heigth = 100
base = 50
rad_petal = sqrt(heigth * heigth + base * base)

tga = base * 1.0 / heigth
atna =  atan(tga)
angle = (atna * 180.0 / pi)

#first petal

begin_fill()
home()
setheading(angle)
fd(rad_petal)
up()
home()
setheading(-angle)
down()
fd(rad_petal)

setheading(0)
circle(base, 180)
end_fill()

#second petal
pencolor("blue")
fillcolor("blue")
up()
home()
down()
begin_fill()
setheading(3*angle)
fd(rad_petal)
up()
home()
setheading(angle)
down()
fd(rad_petal)

setheading(2 * angle)
circle(base, 180)
end_fill()

#third petal
pencolor("purple")
fillcolor("purple")
up()
home()
down()
begin_fill()
setheading(5*angle)
fd(rad_petal)
up()
home()
setheading(3 * angle)
down()
fd(rad_petal)

setheading(4 * angle)
circle(base, 180)
end_fill()

hideturtle()

exitonclick()

Код с циклом:

Код:
from turtle import *
from math import *
from random import randint

speed(10)
pensize(1)
pencolor("cyan")
colormode(255)

heigth = 100
base = 50
rad_petal = sqrt(heigth * heigth + base * base)
tga = base * 1.0 / heigth
atna =  atan(tga)
angle = (atna * 180.0 / pi)

def invisible_to_home():
    up()
    home()
    down()

def petal(num):
    r = randint(0, 255)
    g = randint(0, 255)
    b = randint(0, 255)
    fillcolor((r ,g ,b))
    begin_fill()
    invisible_to_home()
    setheading((num * 2 + 1) * angle)
    fd(rad_petal)
    invisible_to_home()
    setheading((num * 2 - 1) * angle)
    fd(rad_petal)
    setheading(num * 2 * angle)
    circle(base, 180)
    end_fill()

amount = int(360/ angle / 2)
print(angle, " amount: ", amount)

for i in range (amount):
    petal(i)
    
invisible_to_home()
dot(10, "red")
hideturtle()

exitonclick()

Спасибо
 

regnor

Модератор
Команда форума
Модератор
Июл 7, 2020
2 625
469
83
в функции petal поменяйте местами строки begin_fill() и invisible_to_home(), вроде работает
Python:
def petal(num):
    r = randint(0, 255)
    g = randint(0, 255)
    b = randint(0, 255)
    fillcolor((r ,g ,b))
    invisible_to_home()
    begin_fill()
    setheading((num * 2 + 1) * angle)
    fd(rad_petal)
    invisible_to_home()
    setheading((num * 2 - 1) * angle)
    fd(rad_petal)
    setheading(num * 2 * angle)
    circle(base, 180)
    end_fill()
 
  • Мне нравится
Реакции: Student

KateYa

Новичок
Пользователь
Ноя 30, 2020
2
0
1
Да, спасибо. Работает
 

Форум IT Специалистов