用海龟给人物着色 python
Coloring figures with Turtle python
我是 python 的新手,我的任务是在 python 中用乌龟创建这个 Figure。我已经设法创建了图形,但是最后一件事我无法弄清楚,那就是我需要为三角形着色,但无论我将 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()
我尝试在创建三角形的函数中添加一个条件,以便只有偶数三角形被着色但无法使其工作。
非常感谢任何帮助。
问题是您没有绘制完整的三角形。注释掉 draw_hexagon()
和 draw_squares()
并检查结果:
在某些时候你必须画一个完整的三角形。例如:
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)
我是 python 的新手,我的任务是在 python 中用乌龟创建这个 Figure。我已经设法创建了图形,但是最后一件事我无法弄清楚,那就是我需要为三角形着色,但无论我将 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()
我尝试在创建三角形的函数中添加一个条件,以便只有偶数三角形被着色但无法使其工作。
非常感谢任何帮助。
问题是您没有绘制完整的三角形。注释掉 draw_hexagon()
和 draw_squares()
并检查结果:
在某些时候你必须画一个完整的三角形。例如:
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)