Раскраски фигурки с черепахой python - PullRequest
1 голос
/ 01 марта 2020

Я довольно новичок в python, и у меня есть задание создать эту рисунок с черепахой в python. Мне удалось создать фигуру, однако есть еще одна вещь, которую я не могу понять, это то, что мне нужно раскрасить треугольники, но они не окрашиваются, независимо от того, где я размещаю begin_fill ().

from turtle import *
from math import *
from numpy import arange
#speed(3)
screensize(1200,1000)
tracer(False) #disables the turtle animation

start_x1 = -300
start_y1 = 0
side_length = 50 #defines that the side of each figure will be 50px
hexagon_height = float(sqrt(side_length**2-(side_length/2)**2)) #calculates the height of the hexagon
start_x2 = round(start_x1+hexagon_height + (side_length/2),2)
start_y2 = float((side_length/2)+hexagon_height+side_length)
distance_x = float(hexagon_height*2+side_length)#calculates the distance in the x-axis of each figure
distance_y = float(start_y2*2)


def draw_hexagon():

    color("black","black")
    begin_fill()
    seth(30)

    for i in range(6):
        left(60)
        forward(side_length)
    end_fill()

def draw_squares():

    for x in range (0,360,60):#This for loop increases the angle by 60 until 360 
        begin_fill()
        color('black','red')
        seth(x)#sets the initial angle to start drawing the square
        for i in range (3):
            forward(side_length)
            left(90)
        end_fill()
#This function draws the lines around the set hexagon+squares to form the triangles        

def draw_triangles():

    #begin_fill()
    #color('black','yellow')    
    seth(180)#these 3 lines position the turtle at the starting place to start drawing the triangles
    back(50)
    left(60)
    for i in range(12):#these lines draw the 12 sides of the triangles
        if i%2==0:
            begin_fill()
            color('black','yellow')
            forward(side_length)
            right(30) 
            end_fill()
        else:
            forward(side_length)
            right(30)

#this function creates two lines of figures starting at coordinates (-300,0) and (-300,236.6)
def create_line():

    for y in arange (start_y1,400,distance_y):#This will create the two lines of figures separated by 238 px in the y axis
        penup()
        for x in arange(start_x1,400,distance_x):#This will move the figures 136.6 pixels starting from the x position of -300 until it reaches x=400
            goto(x,y)#This moves the figures in the x and y axis 
            pendown()
            draw_hexagon()
            draw_squares()
            draw_triangles()

#this function creates one line of figures starting at coordinates (-231.7,118.3)        

def create_line2():

    for y in arange(start_y2,400,distance_y):
        penup()
        for x in arange(start_x2,476,distance_x):
            goto(x,y)
            pendown()
            draw_hexagon()
            draw_squares()
            draw_triangles()


create_line()

create_line2()

Я попытался добавить в функцию, которая создает треугольники условие, чтобы только четные треугольники были раскрашены, но не могли заставить его работать.

Любая помощь очень ценится.

1 Ответ

0 голосов
/ 01 марта 2020

Проблема в том, что вы не рисуете полные треугольники. Закомментируйте draw_hexagon() и draw_squares() и проверьте результат:

enter image description here

В какой-то момент вы должны нарисовать полный треугольник. Например:

begin_fill()
forward(side_length)
right(120)
forward(side_length)
right(120)
forward(side_length)
end_fill()

Нарисуйте треугольник в случае i%2==0:

def draw_triangles():

    color('black','yellow')  
    seth(180)#these 3 lines position the turtle at the starting place to start drawing the triangles
    back(50)
    left(60)
    for i in range(12):#these lines draw the 12 sides of the triangles
        if i%2==0:
            color('black','yellow')

            # draw filled triangle
            begin_fill()
            forward(side_length)
            right(120)
            forward(side_length)
            right(120)
            forward(side_length)
            end_fill()

            right(120)
            forward(side_length)
            right(30) 
        else:
            forward(side_length)
            right(30)

...