【问题标题】:Drawing multiple shapes at a time from a list of options (Python Turtle Graphics)?从选项列表(Python Turtle Graphics)中一次绘制多个形状?
【发布时间】:2016-09-25 22:12:47
【问题描述】:

所以,首先,这是要求:

  1. 用户从 6 个列表中选择 3 个形状;
  2. 用户选择大小、填充颜色和线条颜色;
  3. 用户不能两次选择相同的形状
  4. 形状应均匀分布,各占屏幕的 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...等等,这将永远需要,看起来很可怕,而且那么我每次都必须编写乌龟命令。如您所见,我尝试使用 def,但在我较小的测试代码中它根本不起作用,所以我也不确定我是否理解正确。

除此之外,我如何确保所有形状或它们应该在哪里?我能看到的唯一方法是使用不同的“goto”为每个结果编写单独的代码。

有没有办法让用户同时输入所有三个选项(“123”、“231”等),然后让程序遍历每个数字并依次绘制?有没有办法将每个数字分配给一组绘制形状的代码?我对这一切都很陌生。我很感激你能给我的任何帮助。谢谢!

【问题讨论】:

    标签: python turtle-graphics


    【解决方案1】:

    下面是一个示例框架,它从(递减的)形状列表中提示用户,划分画布并绘制它们。只实现了圆形,其他的形状都需要填充,代码还远未完成,需要添加错误检查等收尾工作:

    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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-22
      • 1970-01-01
      • 2019-08-08
      • 1970-01-01
      • 2019-03-05
      • 1970-01-01
      • 1970-01-01
      • 2018-03-24
      相关资源
      最近更新 更多