我正在试着在我做的旧国旗上做一个垂直版本的美国国旗,但我不知道该怎么做,因为我已经有一段时间没有做这个了。
这是代码本身,它制作了一面美国国旗。我正在想办法让这张图片变得垂直。
海军站在哪一边并不重要,只要线路下降,我就试着改变一些数字,但它到处都是。
import turtle
import time
screen = turtle.getscreen()
screen.bgcolor("white")
oogway = turtle.Turtle()
oogway.speed(10)
oogway.penup()
oogway.shape("turtle")
flag_height = 250
flag_width = 475
start_x = -237
start_y = 125
stripe_height = flag_height/13
stripe_width = flag_width
star_size = 10
def draw_fill_rectangle(x, y, height, width, color):
oogway.goto(x,y)
oogway.pendown()
oogway.color(color)
oogway.begin_fill()
oogway.forward(height)
oogway.forward(90)
oogway.forward(width)
oogway.right(90)
oogway.forward(width)
oogway.right(90)
oogway.forward(height)
oogway.right(90)
oogway.end_fill()
oogway.penup()
def draw_star(x,y,color,length) :
oogway.goto(x,y)
oogway.setheading(0)
oogway.pendown()
oogway.begin_fill()
oogway.color(color)
for turn in range(0,5) :
oogway.forward(length)
oogway.right(144)
oogway.forward(length)
oogway.right(144)
oogway.end_fill()
oogway.penup()
def draw_stripes():
x = start_x
y = start_y
for stripe in range(0,6):
for color in ["red", "white"]:
draw_fill_rectangle(x, y, stripe_height, stripe_width, color)
y = y - stripe_height
draw_fill_rectangle(x, y, stripe_height, stripe_width, 'red')
y = y - stripe_height
def draw_square():
square_height = 133
square_width = 185
draw_fill_rectangle(start_x, start_y, square_height, square_width, 'navy')
def draw_six_stars_rows():
gap_between_stars = 30
gap_between_lines = stripe_height + 6
y = 112
# create 5 rows of stars
for row in range(0,5) :
x = -222
# create 6 stars in each row
for star in range (0,6) :
draw_star(x, y, 'white', star_size)
x = x + gap_between_stars
y = y - gap_between_lines
def draw_five_stars_rows():
gap_between_stars = 30
gap_between_lines = stripe_height + 6
y = 100
# create 4 rows of stars
for row in range(0,4) :
x = -206
# create 5 stars in each row
for star in range (0,5) :
draw_star(x, y, 'white', star_size)
x = x + gap_between_stars
y = y - gap_between_lines
# start after 5 seconds.
time.sleep(5)
# draw 13 stripes
draw_stripes()
# draw squares to hold stars
draw_square()
# draw 30 stars, 6 * 5
draw_six_stars_rows()
# draw 20 stars, 5 * 4. total 50 stars representing 50 states of USA
draw_five_stars_rows()
# hide the cursor/turtle
oogway.hideturtle()
# keep holding the screen until closed manually
screen.mainloop()发布于 2020-03-17 17:08:33
在我们解决垂直标志问题之前,让我们回顾一下您的水平标志问题。您的代码给人一种错误的印象,即它可以根据需要调整标志的大小:
flag_height = 250
flag_width = 475但是有太多的嵌入数字不允许这种情况发生:
start_x = -237
start_y = 125
star_size = 10
square_height = 133
square_width = 185
gap_between_stars = 30
gap_between_lines = stripe_height + 6
y = 112
x = -222
y = 100
x = -206因此,让我们重写您的代码,以绘制一个不包含所有这些嵌入数字的水平标志:
from turtle import Screen, Turtle
# Based on https://en.wikipedia.org/wiki/Flag_of_the_United_States
FLAG_HOIST = 250
FLAG_FLY = FLAG_HOIST * 1.9
STRIPE_WIDTH = FLAG_HOIST/13
CANTON_HOIST = STRIPE_WIDTH * 7
CANTON_FLY = 2 * FLAG_FLY / 5
STAR_DIAMETER = 4 * STRIPE_WIDTH / 5
VERTICAL_STAR_GAP = -CANTON_HOIST/5
HORIZONTAL_STAR_GAP = CANTON_FLY/6
START_X = -FLAG_FLY/2
START_Y = FLAG_HOIST/2
def draw_fill_rectangle(x, y, width, height, color):
turtle.color(color)
turtle.goto(x, y)
turtle.begin_fill()
for _ in range(2):
turtle.forward(width)
turtle.right(90)
turtle.forward(height)
turtle.right(90)
turtle.end_fill()
def draw_star(x, y, color, length):
turtle.color(color)
turtle.goto(x, y)
turtle.setheading(0)
turtle.pendown()
turtle.begin_fill()
for _ in range(5):
turtle.forward(length)
turtle.right(144)
turtle.end_fill()
turtle.penup()
def draw_stripes():
y = START_Y
for _ in range(6):
for color in ['red', 'white']:
draw_fill_rectangle(START_X, y, FLAG_FLY, STRIPE_WIDTH, color)
y -= STRIPE_WIDTH
draw_fill_rectangle(START_X, y, FLAG_FLY, STRIPE_WIDTH, 'red')
def draw_square():
draw_fill_rectangle(START_X, START_Y, CANTON_FLY, CANTON_HOIST, 'navy')
def draw_six_stars_rows():
y = START_Y + VERTICAL_STAR_GAP/2
# create 5 rows of stars
for _ in range(5):
x = START_X + (HORIZONTAL_STAR_GAP/2 - STAR_DIAMETER/2)
# create 6 stars in each row
for _ in range(6):
draw_star(x, y, 'white', abs(STAR_DIAMETER))
x += HORIZONTAL_STAR_GAP
y += VERTICAL_STAR_GAP
def draw_five_stars_rows():
y = START_Y + VERTICAL_STAR_GAP
# create 4 rows of stars
for _ in range(4):
x = START_X + (HORIZONTAL_STAR_GAP - STAR_DIAMETER/2)
# create 5 stars in each row
for _ in range(5):
draw_star(x, y, 'white', abs(STAR_DIAMETER))
x += HORIZONTAL_STAR_GAP
y += VERTICAL_STAR_GAP
screen = Screen()
turtle = Turtle()
turtle.hideturtle()
turtle.speed('fastest')
turtle.setheading(0)
turtle.penup()
# draw 13 stripes
draw_stripes()
# draw squares to hold stars
draw_square()
# draw 30 stars, 6 * 5
draw_six_stars_rows()
# draw 20 stars, 5 * 4. total 50 stars representing 50 states of USA
draw_five_stars_rows()
screen.exitonclick()

现在我们已经抽象了代码,将标志垂直转换为270度的默认方向,翻转一些contants上的符号,并在goto()调用中颠倒x和y:
13c13
< STAR_DIAMETER = 4 * STRIPE_WIDTH / 5
---
> STAR_DIAMETER = -4 * STRIPE_WIDTH / 5
16c16
< HORIZONTAL_STAR_GAP = CANTON_FLY/6
---
> HORIZONTAL_STAR_GAP = -CANTON_FLY/6
18c18
< START_X = -FLAG_FLY/2
---
> START_X = FLAG_FLY/2
23c23
< turtle.goto(x, y)
---
> turtle.goto(y, x)
36,37c36,37
< turtle.goto(x, y)
< turtle.setheading(0)
---
> turtle.goto(y, x)
> turtle.setheading(270)
91c91
< turtle.setheading(0)
---
> turtle.setheading(270)

https://stackoverflow.com/questions/60711921
复制相似问题