从选项列表中一次绘制多个形状(Python Turtle Graphics)?
Drawing multiple shapes at a time from a list of options (Python Turtle Graphics)?
所以,首先,这是要求:
- 用户从 6 个形状列表中选择 3 个;
- 用户选择大小、填充颜色和线条颜色;
- 用户不能选择相同的形状两次
- 形状应该均匀分布,每个占据屏幕的 1/3
到目前为止,这是我的代码:
import turtle
turtle = turtle.Screen()
def circle():
def triangle():
def square():
def pentagon():
def hexagon():
def heptagon():
for list in ["1.Circle","2.Triangle","3.Square","4.Pentagon","5.Hexagon","6.Heptagon"]:
print(list)
shape1 = input("Choose one number from the following:")
if shape1 == "1":
for list in ["2.Triangle","3.Square","4.Pentagon","5.Hexagon","6.Heptagon"]:
print(list)
shape2 = input("Choose one number from the following:")
if shape2 == "2":
elif shape2 == "3":
elif shape2 == "4":
elif shape2 == "5":
elif shape2 == "6":
else:
print("Incorrect input. Please try again.")
if shape1 == "2":
if shape1 == "3":
if shape1 == "4":
if shape1 == "5":
if shape1 == "6":
else:
print("Incorrect input. Please try again.")
基本上,我非常困惑。我能找到在用户选择的一行中绘制三个形状的唯一方法是完成所有可能的结果 - 123、124、125、126、132、134 等,这将永远花费,看起来很可怕,而且那么我每次都必须编写 turtle 命令。如您所见,我尝试使用 def,但在我较小的测试代码中它根本不起作用,所以我也不确定我是否理解正确。
除此之外,我如何确保所有形状或它们应该在的位置?我能看到的唯一方法是为每个具有不同 "goto"s.
的结果编写单独的代码
有没有办法让用户一次输入所有三个选项(“123”、“231”等),然后让程序遍历每个数字并依次绘制?有没有办法将每个数字分配给一组绘制形状的代码?我对这一切都很陌生。我很感激你能给我的任何帮助。谢谢!
下面是一个示例框架,提示用户从(递减的)形状列表中划分 canvas 并绘制它们。只实现了圆圈,其他的形状需要自己补,离完成代码还很远,需要添加错误检查和其他收尾工作:
import turtle
CANVAS_WIDTH = 900
CANVAS_HEIGHT = 600
CHROME_WIDTH = 30 # allow for window borders, title bar, etc.
SHAPE_CHOICES = 3
def circle(bounds):
turtle.penup()
center_x = bounds['x'] + bounds['width'] // 2
bottom_y = bounds['y']
turtle.setposition(center_x, bottom_y)
turtle.pendown()
turtle.circle(min(bounds['width'], bounds['height']) // 2)
def triangle(bounds):
circle(bounds)
def square(bounds):
circle(bounds)
def pentagon(bounds):
circle(bounds)
def hexagon(bounds):
circle(bounds)
def heptagon(bounds):
circle(bounds)
DESCRIPTION, FUNCTION = 0, 1
shapes = [("Circle", circle), ("Triangle", triangle), ("Square", square), ("Hexagon", hexagon), ("Heptagon", heptagon)]
choices = []
turtle.setup(CANVAS_WIDTH + CHROME_WIDTH, CANVAS_HEIGHT + CHROME_WIDTH)
for _ in range(SHAPE_CHOICES):
for i, (description, function) in enumerate(shapes):
print("{}. {}".format(i + 1, description))
choice = int(input("Choose one number from the above: ")) - 1
choices.append(shapes[choice][FUNCTION])
del shapes[choice]
x, y = -CANVAS_WIDTH // 2, -CANVAS_HEIGHT // 2
width, height = CANVAS_WIDTH // SHAPE_CHOICES, CANVAS_HEIGHT // SHAPE_CHOICES
# I'm dividing the screen into thirds both horizontally and vertically
bounds = dict(x=x, y=y, width=width, height=height)
for choice in choices:
choice(bounds)
bounds['x'] += width
bounds['y'] += height
turtle.done()
所以,首先,这是要求:
- 用户从 6 个形状列表中选择 3 个;
- 用户选择大小、填充颜色和线条颜色;
- 用户不能选择相同的形状两次
- 形状应该均匀分布,每个占据屏幕的 1/3
到目前为止,这是我的代码:
import turtle
turtle = turtle.Screen()
def circle():
def triangle():
def square():
def pentagon():
def hexagon():
def heptagon():
for list in ["1.Circle","2.Triangle","3.Square","4.Pentagon","5.Hexagon","6.Heptagon"]:
print(list)
shape1 = input("Choose one number from the following:")
if shape1 == "1":
for list in ["2.Triangle","3.Square","4.Pentagon","5.Hexagon","6.Heptagon"]:
print(list)
shape2 = input("Choose one number from the following:")
if shape2 == "2":
elif shape2 == "3":
elif shape2 == "4":
elif shape2 == "5":
elif shape2 == "6":
else:
print("Incorrect input. Please try again.")
if shape1 == "2":
if shape1 == "3":
if shape1 == "4":
if shape1 == "5":
if shape1 == "6":
else:
print("Incorrect input. Please try again.")
基本上,我非常困惑。我能找到在用户选择的一行中绘制三个形状的唯一方法是完成所有可能的结果 - 123、124、125、126、132、134 等,这将永远花费,看起来很可怕,而且那么我每次都必须编写 turtle 命令。如您所见,我尝试使用 def,但在我较小的测试代码中它根本不起作用,所以我也不确定我是否理解正确。
除此之外,我如何确保所有形状或它们应该在的位置?我能看到的唯一方法是为每个具有不同 "goto"s.
的结果编写单独的代码有没有办法让用户一次输入所有三个选项(“123”、“231”等),然后让程序遍历每个数字并依次绘制?有没有办法将每个数字分配给一组绘制形状的代码?我对这一切都很陌生。我很感激你能给我的任何帮助。谢谢!
下面是一个示例框架,提示用户从(递减的)形状列表中划分 canvas 并绘制它们。只实现了圆圈,其他的形状需要自己补,离完成代码还很远,需要添加错误检查和其他收尾工作:
import turtle
CANVAS_WIDTH = 900
CANVAS_HEIGHT = 600
CHROME_WIDTH = 30 # allow for window borders, title bar, etc.
SHAPE_CHOICES = 3
def circle(bounds):
turtle.penup()
center_x = bounds['x'] + bounds['width'] // 2
bottom_y = bounds['y']
turtle.setposition(center_x, bottom_y)
turtle.pendown()
turtle.circle(min(bounds['width'], bounds['height']) // 2)
def triangle(bounds):
circle(bounds)
def square(bounds):
circle(bounds)
def pentagon(bounds):
circle(bounds)
def hexagon(bounds):
circle(bounds)
def heptagon(bounds):
circle(bounds)
DESCRIPTION, FUNCTION = 0, 1
shapes = [("Circle", circle), ("Triangle", triangle), ("Square", square), ("Hexagon", hexagon), ("Heptagon", heptagon)]
choices = []
turtle.setup(CANVAS_WIDTH + CHROME_WIDTH, CANVAS_HEIGHT + CHROME_WIDTH)
for _ in range(SHAPE_CHOICES):
for i, (description, function) in enumerate(shapes):
print("{}. {}".format(i + 1, description))
choice = int(input("Choose one number from the above: ")) - 1
choices.append(shapes[choice][FUNCTION])
del shapes[choice]
x, y = -CANVAS_WIDTH // 2, -CANVAS_HEIGHT // 2
width, height = CANVAS_WIDTH // SHAPE_CHOICES, CANVAS_HEIGHT // SHAPE_CHOICES
# I'm dividing the screen into thirds both horizontally and vertically
bounds = dict(x=x, y=y, width=width, height=height)
for choice in choices:
choice(bounds)
bounds['x'] += width
bounds['y'] += height
turtle.done()