domingo, 8 de octubre de 2017

Turtle

Programas con TURTLE


Tamaño y posición de la ventana
from turtle import *
setup(640, 480, 0, 0)
done()
__________________________________________________________________________

Definir titulo
from turtle import *
setup(450, 150, 0, 0)
title("Ejemplo de ventana")}
done()
__________________________________________________________________________
Sin setup la ventana se crea en el centro
from turtle import *
title("Ejemplo de ventana")
done()
__________________________________________________________________________
from turtle import *
setup(250, 100, 0, 0)
title("Ejemplo de ventana")
hideturtle()
dot(10, 0, 0, 0)
setup(450, 150, 0, 0)
done()
__________________________________________________________________________
Screensize define el tamaño del área de dibujo
from turtle import *
setup(450, 150, 0, 0)
screensize(10, 10)
done()
__________________________________________________________________________
Si la ventana es mas chica que el área de dibujo aparecerá una barra de desplazamiento
from turtle import *
setup(150, 150, 0, 0)
screensize(300, 300)
done ()

solo barra vertical:
from turtle import *
setup(450, 150, 0, 0)
screensize(400, 300)
done()
_________________________________________________________________________
Crear un punto
from turtle import *
setup(250, 100, 0, 0)
screensize(100, 100)
hideturtle()
dot(10, 0, 0, 0)
screensize(200, 100)
done()
__________________________________________________________________________
Mostrar cursor
from turtle import *
setup(450, 150, 0, 0)
screensize(300, 150)
showturtle()
done()
__________________________________________________________________________
Crear linea
from turtle import *
setup(450, 150, 0, 0)
screensize(300, 150)
goto(100, 0)
done()
__________________________________________________________________________
Oculta cursor
from turtle import *
setup(450, 150, 0, 0)
screensize(300, 150)
goto(100, 0)
hideturtle()
done()
_________________________________________________________________________
Ocultar tortuga desde el comienzo
from turtle import *

setup(450, 150, 0, 0)
screensize(300, 150)
hideturtle()
goto(100, 0)
done()
__________________________________________________________________________
from turtle import *

setup(450, 200, 0, 0)
screensize(300, 150)
goto(0, 0)
done()
__________________________________________________________________________

from turtle import *

setup(450, 200, 0, 0)
screensize(300, 150)
goto(100, 50)
done()
__________________________________________________________________________
Dibujar segmentos
from turtle import *
setup(450, 200, 0, 0)
screensize(300, 150)
goto(100, 50)
goto(100, -50)
goto(50, -50)
done()
__________________________________________________________________________

from turtle import *

setup(450, 200, 0, 0)
screensize(300, 150)

goto(100, 50)
sety(-50)
setx(50)
done()
__________________________________________________________________________
Levantar y bajar lápiz
from turtle import *

setup(450, 200, 0, 0)
screensize(300, 150)

goto(100, 50)
penup()
goto(100, -50)
pendown()
goto(50, -50)
done()
__________________________________________________________________________
Cambiar grosor de trazo
from turtle import *

setup(450, 200, 0, 0)
screensize(300, 150)

goto(100, 50)
pensize(4)
goto(100, -50)
pensize(8)
goto(50, -50)
done()
__________________________________________________________________________
Color del trazo:
from turtle import *

setup(450, 200, 0, 0)
screensize(300, 150)
colormode(255)

pencolor(255, 0, 0)
goto(100, 50)
pencolor(0, 255, 0)
goto(100, -50)
pencolor(0, 0, 255)
goto(50, -50)
done()
__________________________________________________________________________
from turtle import *

setup(450, 200, 0, 0)
screensize(300, 150)
colormode(1)

pencolor(1, 0, 0)
goto(100, 50)
pencolor(0, 1, 0)
goto(100, -50)
pencolor(0, 0, 1)
goto(50, -50)
done()
__________________________________________________________________________
from turtle import *

setup(450, 200, 0, 0)
screensize(300, 150)
colormode(255)

pencolor(128, 0, 0)
goto(100, 50)
pencolor(0, 128, 0)
goto(100, -50)
pencolor(0, 0, 128)
goto(50, -50)
done()
__________________________________________________________________________
from turtle import *

setup(450, 200, 0, 0)
screensize(300, 150)
colormode(1)

pencolor(0.5, 0, 0)
goto(100, 50)
pencolor(0, 0.5, 0)
goto(100, -50)
pencolor(0, 0, 0.5)
goto(50, -50)
done()
__________________________________________________________________________
from turtle import *

setup(450, 200, 0, 0)
screensize(300, 150)

pencolor("red")
goto(100, 50)
pencolor("green")
goto(100, -50)
pencolor("blue")
goto(50, -50)
done()
__________________________________________________________________________
Crear puntos de colores bajando lapiz
from turtle import *

setup(450, 200, 0, 0)
screensize(300, 150)
colormode(255)

goto(100, 50)
dot(10, 255, 0, 0)
goto(100, -50)
dot(10, 0, 255, 0)
goto(50, -50)
dot(10, 0, 0, 255)
goto(0,0)
done()
__________________________________________________________________________
Crear puntos de colores subiendo el lapiz (sin lineas)
from turtle import *

setup(450, 200, 0, 0)
screensize(300, 150)
colormode(255)

penup()
goto(100, 50)
dot(10, 255, 0, 0)
goto(100, -50)
dot(10, 0, 255, 0)
goto(50, -50)
dot(10, 0, 0, 255)
goto(0,0)
done()
__________________________________________________________________________
Rellenar figura
from turtle import *

setup(450, 200, 0, 0)
screensize(300, 150)
title("relleno")
hideturtle()

pensize(5)
fillcolor("red")
begin_fill()
goto(100, 0)
goto(100, 50)
goto(0, 50)
goto(0, 0)
end_fill()
done()
__________________________________________________________________________
Si no se establece color de relleno, el predeterminado es el negro.
from turtle import *

setup(450, 200, 0, 0)
screensize(300, 150)
title("relleno predeterminado")
hideturtle()

pencolor("red")
pensize(5)
begin_fill()
goto(100, 0)
goto(100, 50)
goto(0, 50)
goto(0, 0)
end_fill()
done()
__________________________________________________________________________
Python rellena figuras aunque no estén completas.
from turtle import *

setup(450, 200, 0, 0)
screensize(300, 150)
title("relleno")
hideturtle()

pensize(5)
fillcolor("red")
begin_fill()
goto(100, 0)
goto(100, 50)
goto(0, 50)
end_fill()
done()
__________________________________________________________________________
Si las líneas de la figura se cruzan, Python rellena cada una de las partes cerradas.
from turtle import *

setup(450, 200, 0, 0)
screensize(300, 150)
title("relleno")
hideturtle()

pensize(5)
fillcolor("red")
begin_fill()
goto(50, 50)
goto(100, -50)
goto(150, 0)
goto(0, 0)
end_fill()
done()
__________________________________________________________________________
Otra figura con lineas cruzadas
from turtle import *

setup(450, 200, 0, 0)
screensize(300, 150)
title("relleno")
hideturtle()

pensize(5)
fillcolor("red")
begin_fill()
goto(0, 75)
goto(100, 0)
goto(100, 75)
end_fill()
done()
__________________________________________________________________________
Rectangulo
from turtle import *

setup(450, 200, 0, 0)
screensize(300, 150)
title("relleno")
hideturtle()

pensize(5)
fillcolor("red")
begin_fill()
goto(75, 0)
goto(75, 75)
penup()
goto(-100, 75)
pendown()
goto(-100,0)
goto(-25, 0)
end_fill()
done()
__________________________________________________________________________
Rellenar 2 figuras separadas
from turtle import *

setup(450, 200, 0, 0)
screensize(300, 150)
title("relleno")
hideturtle()

pensize(5)
fillcolor("red")
begin_fill()
goto(75, 0)
goto(75, 75)
end_fill()
penup()
goto(-100, 75)
pendown()
begin_fill()
goto(-100,0)
goto(-25, 0)
end_fill()
done()
__________________________________________________________________________
Se pueden llenar 2 figuras separadas si están cerradas.
from turtle import *

setup(450, 200, 0, 0)
screensize(300, 150)
title("www.mclibre.org")
hideturtle()

pensize(5)
fillcolor("red")
begin_fill()
goto(75, 0)
goto(75, 75)
goto(0,0)
penup()
goto(-100, 75)
pendown()
goto(-100,0)
goto(-25, 0)
goto(-100, 75)
end_fill()
done()

No hay comentarios:

Publicar un comentario