我试图用python在python中绘制美国国旗,最后迷失在我的代码中而没有找到我的错误。我也无法弄清楚如何为我的旗帜着色......我认为可行的是不是......我做了一些事情,现在我的代码崩溃了一半......请帮助我我是编程新手......
到目前为止,这是我的代码。
canvas
答案 0 :(得分:0)
我相信你拥有所有关键组件。您主要需要考虑相对于大小和起始位置,而不是硬编码值,并保持简单。 (例如color("blue")
而不是color(r, s, t)
,直到你开始工作。)并仔细看看旗帜上的星星安排。
我按照上述评论的方式重新编写了代码并进行了一些样式更改:
import turtle
X_POSITION, Y_POSITION = -150, 150
def draw_rectangle(length, height):
turtle.up()
C = height * (7 / 13.0)
D = length * (2 / 5.0)
L = height * (1 / 13.0)
## Draw rectangle first.
turtle.setpos(X_POSITION, Y_POSITION)
turtle.down()
turtle.forward(length)
turtle.right(90)
turtle.forward(height)
turtle.right(90)
turtle.forward(length)
turtle.right(90)
turtle.forward(height)
## Then draw the red stripes.
x, y = X_POSITION, Y_POSITION - L
turtle.color("red")
for z in range(0, 13, 2):
turtle.up()
turtle.setpos(x, y)
turtle.setheading(90)
turtle.down()
turtle.begin_fill()
turtle.forward(L)
turtle.right(90)
turtle.forward(length)
turtle.right(90)
turtle.forward(L)
turtle.right(90)
turtle.forward(length)
turtle.end_fill()
y -= 2 * L
## Draw the stars rectangle overlapping the stripes
turtle.up()
turtle.color('blue')
turtle.setpos(X_POSITION + D, Y_POSITION - C)
turtle.down()
turtle.begin_fill()
turtle.forward(D)
turtle.right(90)
turtle.forward(C)
turtle.right(90)
turtle.forward(D)
turtle.right(90)
turtle.forward(C)
turtle.end_fill()
## next is stars
turtle.up()
draw_stars(D, C)
turtle.up()
## This gets the turtle pen out of the way at the very end.
turtle.hideturtle()
def draw_stars(length, height):
row = height // 9
row_offset = row / 2.0
column = length // 6
column_offset = column / 2.0
y_position = row_offset
for z in range(9):
if z % 2 == 0:
draw_starrows(6, column_offset, y_position, column)
else:
draw_starrows(5, column, y_position, column)
y_position += row
def draw_starrows(star_count, x_offset, y_offset, spacing):
x, y = X_POSITION, Y_POSITION
turtle.color("white")
for z in range(star_count):
turtle.up()
turtle.setpos(x + x_offset, y - y_offset)
turtle.begin_fill()
turtle.down()
for _ in range(5):
turtle.forward(6.154)
turtle.left(144)
turtle.end_fill()
x += spacing
def draw_flag(height):
turtle.speed("fastest")
draw_rectangle(height * 1.9, height)
draw_flag(200)
turtle.done()
虽然缩放现在基本上有效(尝试draw_flag(100)
)但是星星本身(你做得很好,BTW)仍然是固定大小的,所以你需要回去并缩放它们以匹配其余的国旗: