简体   繁体   中英

How to draw shapes in Python/turtle using a list of coordinates

I'm new to Python and have a question. I want to move the turtle to a specific start location, and from there draw a shape. The shape has pre-determined coordinates, so I need to connect the points to make the shape.

I have to make 2 functions such that the following code calls those 2 functions and draws three shapes:

 def testPolyLines():
    # First square
    squareShape = [(50, 0), (50, 50), (0, 50), (0, 0)]
    drawPolyLine((200, 200), squareShape)
    # Second square
    drawPolyLine((-200, 200), squareShape, lineColour="green")
    biggerSquareShape = generateSquarePoints(100)
    # A triangle
    triangleShape = [(200, 0), (100, 100), (0, 0)]
    drawPolyLine((100, -100), triangleShape, fillColour="green")

def main():
    testPolyLines()
main()

I made the first function to generate points for a square of any size:

def generateSquarePoints(i):
    squareShape = [(i, 0), (i, i), (0, i), (0, 0)]

But I get stuck when it comes to actually drawing the shape. I can make the turtle go to the start position, but I don't know how to make it go through a list of points and connect them to form a shape. This is what I have:

def drawPolyLine(start, squareShape, lineColour="black", fillColour = "white"):
    pencolor(lineColour)
    fillcolor(fillColour)
    penup()
    goto(start)
    pendown()
    begin_fill()
    goto(squareShape)
    end_fill()

This is obviously not right...the part I'm confused about is how to tell the turtle to go to the list of points, and connect them along the way to form the shape. My program now only goes to the start positions but doesn't draw the shape.

I would really appreciate any help or advice! Thanks in advance.

Issues with your code: you can't just goto() the points, you need to adjust them to the starting position, treating the x & y in the point as more of a delta-x, delta-y; generateSquarePoints() needs to return its list of points, not assign it; obviously a for loop is needed as others have mentioned; you need to explicitly draw back to the starting point to close the shape.

Try the following rework of your code to see if it does what you want:

import turtle

def generateSquarePoints(i):
    """ generate points for a square of any size """
    return [(i, 0), (i, i), (0, i), (0, 0)]

def drawPolyLine(start, points, lineColour="black", fillColour="white"):
    """ draw shapes using a list of coordinates """
    turtle.pencolor(lineColour)
    turtle.fillcolor(fillColour)

    turtle.penup()

    turtle.goto(start)  # make the turtle go to the start position

    turtle.pendown()
    turtle.begin_fill()

    x, y = start

    for point in points:  # go through a list of (relative) points
        dx, dy = point
        turtle.goto(x + dx, y + dy)
    turtle.goto(start)  # connect them to start to form a closed shape

    turtle.end_fill()
    turtle.penup()

if __name__ == "__main__":

    def testPolyLines():
        """ test shapes shape drawing functions """
        # First square
        squareShape = [(50, 0), (50, 50), (0, 50), (0, 0)]
        drawPolyLine((200, 200), squareShape)

        # Second square
        biggerSquareShape = generateSquarePoints(100)
        drawPolyLine((-200, 200), biggerSquareShape, lineColour="green")

        # A triangle
        triangleShape = [(200, 0), (100, 100), (0, 0)]
        drawPolyLine((100, -100), triangleShape, fillColour="green")


    def main():
        testPolyLines()
        turtle.done()

    main()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM