Skip to content

Instantly share code, notes, and snippets.

@pablovv72
Last active June 4, 2018 22:24
Show Gist options
  • Save pablovv72/75ec2e2aa155cb64ad921427709f5a9d to your computer and use it in GitHub Desktop.
Save pablovv72/75ec2e2aa155cb64ad921427709f5a9d to your computer and use it in GitHub Desktop.
tortuga.py
# Dibuja aleatoriamente polígonos con efecto caleidoscópico.
# Esta versión solo funciona correctamente desde la app Pythonista.
import turtle
from random import *
# Configuramos la pizarra para la tortuga.
t = turtle
clon = t.clone()
clon.penup()
clon.hideturtle()
clon.speed(0)
t.setup(500, 500)
t.colormode(1)
t.pencolor(0, 0, 0)
t.pensize(2)
t.speed(9)
def rectangulo(px, py, ancho, alto):
for c in range(1, 5): # c = cuadrante cartesiano (1-4).
# Nos posicionamos en la esquina y con la orientación correspondiente
# para el rectángulo que vamos a dibujar sin dejar rastro.
t.penup() # Levantamos el boli.
x = (-1 if c == 2 or c == 3 else 1) * (px + ancho / 2)
y = (-1 if c == 3 or c == 4 else 1) * (py + alto / 2)
t.seth(t.towards(x, -y)) # Tanto towards como distance trabajan
t.forward(t.distance(x, -y)) # con el eje y invertido.
t.seth(180 if c == 1 or c == 4 else 0)
t.pendown() # Bajamos el boli.
# Dibujamos el rectángulo
t.begin_fill()
t.forward(ancho)
if c == 1 or c == 3:
t.left(90)
t.forward(alto)
t.left(90)
t.forward(ancho)
t.left(90)
else:
t.right(90)
t.forward(alto)
t.right(90)
t.forward(ancho)
t.right(90)
t.forward(alto)
t.end_fill()
def poligono_regular(px, py, radio, lados):
vertices = [] # Aquí guardaremos los vértices del polígono.
t.penup()
for i in range(lados): # Buscamos y guardamos los vertices.
clon.goto(px, py) # Usamos un clon invisible de la tortuga
clon.seth(360 / lados * (i + 1)) # para que no se vea el movimiento
clon.forward(radio) # mientras va buscando los vértices.
vertices.append(clon.pos())
for c in range(1, 5): # Dibujamos los poligonos.
x = (1 if c == 1 or c == 4 else -1) * (px + radio)
y = (1 if c == 1 or c == 2 else -1) * py
t.seth(t.towards(x, -y))
t.forward(t.distance(x, -y))
t.pendown()
t.begin_fill()
for vertice in vertices:
x = (1 if c == 1 or c == 4 else -1) * vertice[0]
y = (1 if c == 1 or c == 2 else -1) * vertice[1]
t.seth(t.towards(x, -y))
t.forward(t.distance(x, -y))
t.end_fill()
t.penup()
# Loop principal
while True:
t.fillcolor(random(), random(), random(), 0.5)
if choice((True, False)):
rectangulo(
randint(0, 250), randint(0, 250),
randint(25, 125), randint(25, 125)
)
else:
poligono_regular(
randint(0, 250), randint(0, 250),
randint(13, 63), randint(3, 10)
)
t.done()
@pablovv72
Copy link
Author

0a83d0d6-a31a-4bbf-a27f-bbf037755a99

@pablovv72
Copy link
Author

Por lo visto este código solo funciona en la app Pythonista para iOS, porque la versión modificada del módulo turtle admite un cuarto parámetro para el color (el alpha o transparencia), ya publicaré una versión más compatible para quien no use este intérprete

@pablovv72
Copy link
Author

Versión compatible con otros interpretes: https://gist.github.com/pablovv72/ca18b8510e737ee711333c2a6ad1b460

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment