Seren Seren - 2 months ago 16
Python Question

Drawing multiple shapes at a time from a list of options (Python Turtle Graphics)?

So, first of all, here's the requirements:


  1. user picks 3 shapes from a list of 6;

  2. user chooses size, fill color, and line color;

  3. user cannot pick the same shape twice

  4. shapes should be drawn evenly spaced, taking up 1/3 of the screen each



Here's my code so far:

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.")


Basically, I'm terribly confused. The only way I can find to draw three shapes in a row of the user's choosing is to do every possible outcome - 123, 124, 125, 126, 132, 134...etc etc, which would take forever, look horrible, and then I would have to write the turtle commands each time. As you can see, I tried playing around with def, but in my smaller test code it didn't work at all, so I'm not sure I understand it correctly either.

In addition to all of that, how would I ensure that all of the shapes or where they should be? The only way I can see it is to write separate code for each outcome with different "goto"s.

Is there a way to have the user put all three options at once ("123", "231", etc) and then for the program to go through each number and draw it in turn? Is there a way to assign each number to a set of code drawing the shape? I'm pretty new at all of this. I appreciate any help you can give me. Thank you!

Answer

Below is an example framework that prompts the user from (a diminishing) list of shapes, divides the canvas and draws them. It only implements circles, you need to fill in the other shapes, and it's far from finished code, you need to add error checking and other finishing touches:

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