[英]Drawing multiple shapes at a time from a list of options (Python Turtle Graphics)?
因此,首先是以下要求:
到目前为止,这是我的代码:
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”的代码。
有没有一种方法可以让用户一次放置所有三个选项(“ 123”,“ 231”等),然后让程序遍历每个数字并依次绘制呢? 有没有一种方法可以将每个数字分配给一组绘制形状的代码? 所有这些我都是新手。 感谢您能给我的任何帮助。 谢谢!
下面是一个示例框架,该框架从(递减的)形状列表中提示用户,划分画布并绘制它们。 它仅实现圆,您需要填写其他形状,并且距完成的代码很远,您需要添加错误检查和其他修饰:
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.