how to use turtle with cordinates and lists?

0

I'm doing a program in python where I draw a figure in this case a stickman and a square, instead of writing all the commands I want to take the coordinates of a list for example stickman = [x,y,x,y,x,y] and take those coordinates from the list and draw them but I'm stuck, poor to do but nothing allowed me to use a cordage, stickman = [x,y] someone can help me please. here is the code that I have so far

import turtle
from turtle import *
t = Turtle()

def stickman():
        t.left(60)
        t.forward(50)
        t.right(120)
        t.forward(50)
        t.back(50)
        t.left(150)
        t.forward(50)
        t.right(85)
        t.circle(20)

def cuadrado():
        for i in [1,2,3,4]:
                t.forward(50)
                t.left(90)


def dibujar():
        t.penup()
        t.goto(100,200)
        t.pendown()


stickman()
dibujar()
cuadrado()
    
asked by Microplo 09.11.2017 в 19:17
source

1 answer

0

To draw lines from points in a coordinate system x, y you can do the following:

import turtle
from turtle import *

t = Turtle()

def dibujar_linea(start, puntos):

    t.penup()
    t.goto(start)

    t.pendown()
    x, y = start

    for p in puntos:
        dx, dy = p
        t.goto(x + dx, y + dy)

    t.penup()

triangulo = [(100, 0), (50, 50), (0, 0)]
dibujar_linea(start=(100,100),puntos=triangulo)

cuadrado = [(50, 0), (50, 50), (0, 50), (0, 0)]
dibujar_linea(start=(200,200),puntos=cuadrado)

Some comments:

  • The routine dibujar_linea only draws lines, from one point to another in the list that is passed by a parameter.
  • A start position start is also defined as x, y but of the canvas or canvas. From this position the lines will start to be drawn and the points are relative to this initial point.
  • Using for p in puntos we iterate over the list of points and we move using goto to the next point in the list.
answered by 09.11.2017 / 21:04
source