Sie sind auf Seite 1von 7

Dibujos simples con turtle

(Tortuga)
Introduccin
Turtle es como una pizarra para dibujar.

Tiene funciones como turtle.forward(...) y turtle.left(...) que mueven la


tortuga alrededor.

Before you can use turtle, you have to import it. We recommend playing
around with it in the interactive interpreter first, as there is an extra bit of
work required to make it work from files. Just go to your terminal and type:

import turtle

Nota
Not seeing anything on Mac OS? Try issuing a command
like turtle.forward(0) and looking if a new window opened behind your
command line.

Nota
Do you work with Ubuntu and get the error message No module named
_tkinter? Install the missing package with sudo apt-get install python3-tk

Nota
While it might be tempting to just copy and paste whats written on this
page into your terminal, we encourage you to type out each command.
Typing gets the syntax under your fingers (building that muscle memory!)
and can even help avoid strange syntax errors.

turtle.forward(25)
turtle.left(30)

The turtle.forward(...) function tells the turtle to move forward by the given
distance. turtle.left(...) takes a number of degrees which you want to
rotate to the left. There is also turtle.backward(...) and turtle.right(...), too.

Nota
Want to start fresh? You can type turtle.reset() to clear the drawing that
your turtle has made so far. Well go into more detail on turtle.reset() in just a
bit.

La tortuga estndar es slo un tringulo. Eso no es divertido!.


Transformemos el tringulo en tortuga con el comando turtle.shape():

turtle.shape("turtle")

Mucho mejor!

Si pone los comandos en un archivo, ver que la ventana de la tortuga


desaparece una vez que la tortuga finaliza sus movimientos (Esto sucede
porque Python finaliza una vez que la tortuga termina de moverse. Dado que
la ventana de turtle pertenece a Python, tambin termina). Para prevenir que
esto suceda, slo debe agregar el comando turtle.exitonclick() al final de su
archivo. Ahora la ventana permanecer abierta hasta que haga click en ella:

import turtle

turtle.shape("turtle")

turtle.forward(25)

turtle.exitonclick()
Nota
Python is a programming language where horizontal indenting of text is
important. Well learn all about this in the Functions chapter later on, but for
now just keep in mind that stray spaces or tabs before any line of Python
code can cause an unexpected error.

Dibujando un cuadrado
Nota
No se espera que conozca las respuestas de manera inmediata. Aprenda de
intentos y errores! Experimente, vea lo que python hace cuando usted le
dice diferentes cosas, que da hermosos (aunque a veces inesperados)
resultados y que da errores. Si usted quiere seguir jugando con algo que
haya aprendido que entrega resultados ineteresantes, est BIEN. No tema
intentar y fallar y aprender de ello!

Ejercicio
Dibuje un cuadrado como en la siguiente figura:

Para dibujar un cuadrado probablemente necesite ngulos rectos, que son


90 grados.

Solucin
turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.left(90)

Nota
Note como la tortuga parte y termina en el mismo lugar y mirando en la
misma direccin, antes y despus de dibujar el cuadrado. Esta es una
convencin muy til a seguir, ya que es ms fcil dibujar mltiples figuras
despus.

Hide

Bonus
Si se quiere poner creativo, usted puede modificar la figura con las
funciones turtle.width(...) y turtle.color(...). Cmo se usan estas funciones?
Antes de usar una funcin debe conocer su firma (por ejemplo el nmero de
parmetros y que significan). Para averiguar esto puede
escribir help(turtle.color) en la consola de Python. Si hay mucho texto,
Python pondr el texto de ayuda en un paginador, el cual permitir ir arriba y
abajo en las pginas. Presione la tecla q para salir del paginador.

Truco
Est viendo un error como este:
NameError: name 'turtle' is not defined

cuando intenta ver la ayuda? En Python usted debe importar los nombres
antes de poder referirse a ellos, por lo tanto en una nueva consola
interactiva de Python debe ingresar import turtle antes que
funcione help(turtle.color).

Otra forma de saber sobre funciones en buscar en la documentacin en


lnea.
Prudencia
Si usted se equivoca, puede decirle a la tortuga que borre la pizarra con la
directiva turtle.reset() o deshacer el ltimo paso con turtle.undo().

Truco
Como debe haber visto en la ayuda, puede modificar el color
con turtle.color(colorstring). Los colores pueden ser por ejemplo red (rojo),
green (verde) y violet (violeta). Vea el colours manual (manual de colores)
para una lista ms extensiva.

Dibujando un rectngulo
Ejercicio
Puede dibujar un rectngulo ahora?
Solucin
turtle.forward(100)
turtle.left(90)
turtle.forward(50)
turtle.left(90)
turtle.forward(100)
turtle.left(90)
turtle.forward(50)
turtle.left(90)

Hide

Bonus
Que tal un tringulo? En un tringulo equiltero (tringulo con todos sus
lados del mismo largo) cada esquina tiene un ngulo de 60 grados.

Ms cuadrados
Ejercicio
Ahora, dibuje un cuadrado inclinado. Y otro, y otro. Puede experimentar con
los ngulos entre los diferentes cuadrados.
La figura muestra tres giros de 20 grados. Puede intentar con 20, 30 y 40,
por ejemplo.

Solucin
turtle.left(20)

turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.left(90)

turtle.left(30)

turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.left(90)

turtle.left(40)

turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.left(90)

Das könnte Ihnen auch gefallen