├── 01.py
├── 02.py
├── 03.py
├── 04.py
├── 05.py
├── README.md
└── resources
├── sunflowers.json
└── tulipanes.docx
/01.py:
--------------------------------------------------------------------------------
1 | from turtle import *
2 | from math import *
3 |
4 | speed(0)
5 | bgcolor("black")
6 | goto(0,-40)
7 |
8 | for i in range(16):
9 | for j in range(18):
10 | color('#FFA216'), rt(90)
11 | circle(150-j*6,90), lt(90)
12 | circle(150-j*6,90), rt(180)
13 | circle(40,24)
14 |
15 | color('black')
16 | shape('circle')
17 | shapesize(0.5)
18 | fillcolor('#8B4513')
19 | golden_ang=137.508
20 | phi = golden_ang*(pi/180)
21 |
22 | for i in range(140):
23 | r = 4*sqrt(i)
24 | theta = i*phi
25 | x = r*cos(theta)
26 | y = r*sin(theta)
27 | penup(), goto(x,y)
28 | setheading(i*golden_ang)
29 | pendown(), stamp()
30 |
31 | def circulo(x, y):
32 | penup(), goto(x, y), pendown()
33 | color('black'), fillcolor('#FFA216')
34 | begin_fill(), circle(3), end_fill()
35 |
36 | def dibujar_M(x, y):
37 | pos_m = [(x,y), (x, y+4), (x, y+8), (x, y+12), (x, y+16),
38 | (x, y+20), (x, y+24), (x+3, y+21), (x+6, y+18),
39 | (x+9, y+15), (x+12, y+12), (x+15, y+15), (x+18, y+18),
40 | (x+21, y+21), (x+24, y+24), (x+24, y+20), (x+24, y+16),
41 | (x+24, y+12), (x+24, y+8), (x+24, y+4), (x+24, y)]
42 |
43 | for pos in pos_m:
44 | circulo(*pos)
45 |
46 | def dibujar_A(x, y):
47 | pos_a = [(x, y), (x+2, y+4), (x+4, y+8), (x+6, y+12),
48 | (x+8, y+16), (x+10, y+20), (x+12, y+24), (x+14, y+20),
49 | (x+16, y+16), (x+18, y+12), (x+20, y+8), (x+22, y+4),
50 | (x+24, y), (x+8, y+8), (x+12, y+8), (x+16, y+8)]
51 |
52 | for pos in pos_a:
53 | circulo(*pos)
54 |
55 | dibujar_M(-28, 4), dibujar_A(10, 4)
56 | dibujar_M(-28, -30), dibujar_A(10, -30)
57 | circulo(32, -10), circulo(34, -6)
58 |
59 | hideturtle()
60 | done()
--------------------------------------------------------------------------------
/02.py:
--------------------------------------------------------------------------------
1 | from turtle import *
2 | import re
3 | import docx
4 |
5 | data = docx.Document("resources/tulipanes.docx")
6 | coordenadas = []
7 | color = []
8 |
9 | for i in data.paragraphs:
10 | try:
11 | patron = r'[-+]?\d*\.\d*(?:[eE][-+]?\d+)?'
12 | patron_coord = r'\(' + patron + r' ?\, ?' + patron
13 | patron_color = patron_coord + r' ?\, ?' + patron + r'\)'
14 |
15 | coord_stg_tup = re.findall(patron_coord + r'\)', i.text)
16 | color_stg_tup = re.findall(patron_color, i.text)
17 |
18 | coord_num_tup = []
19 | color_val = re.findall(r'[-+]?\d*\.\d*',color_stg_tup[0])
20 | color_val_lst = [float(k) for k in color_val]
21 | color.append(tuple(color_val_lst))
22 |
23 | for j in coord_stg_tup:
24 | coord_pos = re.findall(r'[-+]?\d*\.\d*',j)
25 | coord_num_lst = [float(k) for k in coord_pos]
26 | coord_num_tup.append(tuple(coord_num_lst))
27 | coordenadas.append(coord_num_tup)
28 | except:
29 | pass
30 |
31 | pen = Turtle()
32 | screen = Screen()
33 |
34 | tracer(2)
35 | hideturtle()
36 | pen.speed(10)
37 | screen.getcanvas().winfo_toplevel().attributes("-fullscreen", True)
38 |
39 | for i in range(len(coordenadas)):
40 | draw = 1
41 | path = coordenadas[i]
42 | col = color[i]
43 | pen.color(col)
44 | pen.begin_fill()
45 | for order_pair in path:
46 | x,y = order_pair
47 | y = -1*y
48 | if draw:
49 | pen.up()
50 | pen.goto(x,y)
51 | pen.down()
52 | draw = 0
53 | else:
54 | pen.goto(x,y)
55 | pen.end_fill()
56 | pen.hideturtle()
57 | screen.mainloop()
--------------------------------------------------------------------------------
/03.py:
--------------------------------------------------------------------------------
1 | from turtle import *
2 |
3 | speed(0)
4 | bgcolor("black")
5 | setheading(45)
6 |
7 | for i in range(235):
8 | color('#ff8fab')
9 | circle(270-i,90), lt(90)
10 | circle(270-i,90), lt(18)
11 |
12 | mainloop()
--------------------------------------------------------------------------------
/04.py:
--------------------------------------------------------------------------------
1 | from turtle import *
2 | from math import *
3 |
4 | speed(0)
5 | bgcolor("black")
6 | goto(0,-40)
7 |
8 | # Draw leaves
9 | for i in range(16):
10 | for j in range(18):
11 | color('#FFA216'), rt(90)
12 | circle(150-j*6, 90), lt(90)
13 | circle(150-j*6, 90), rt(180)
14 | circle(40,24)
15 |
16 | # Draw flower center
17 | color('black')
18 | shape('circle')
19 | shapesize(0.5)
20 | fillcolor('#8B4513')
21 | golden_ang = 137.508
22 | phi = golden_ang*(pi/180)
23 |
24 | for i in range(140):
25 | r = 4*sqrt(i)
26 | theta = i*phi
27 | x = r*cos(theta)
28 | y = r*sin(theta)
29 | penup(), goto(x, y)
30 | setheading(i*golden_ang)
31 | pendown(), stamp()
32 |
33 | # Define points to draw letters
34 | def point(x, y):
35 | penup(), goto(x, y), pendown()
36 | color('black'), fillcolor('#FFA216')
37 | begin_fill(), circle(4), end_fill()
38 |
39 | # Function to draw 'T'
40 | def draw_T(x, y):
41 | positions_t = [(x, y+30), (x+6, y+30), (x+12, y+30), (x+18, y+30), (x+24, y+30),
42 | (x+12, y+30), (x+12, y+24), (x+12, y+18), (x+12, y+12), (x+12, y+6), (x+12, y)]
43 |
44 | for pos in positions_t:
45 | point(*pos)
46 |
47 | # Function to draw 'Ú'
48 | def draw_U(x, y):
49 | positions_u = [(x, y+30), (x, y+24), (x, y+18), (x, y+12), (x, y+6),
50 | (x+3, y+3), (x+6, y), (x+12, y-1), (x+18, y), (x+21, y+3),
51 | (x+24, y+6), (x+24, y+12), (x+24, y+18), (x+24, y+24), (x+24, y+30),
52 | (x+12, y+36), (x+16, y+40)]
53 |
54 | for pos in positions_u:
55 | point(*pos)
56 |
57 | # Draw 'TÚ'
58 | draw_T(-27, -20)
59 | draw_U(7, -20)
60 |
61 | hideturtle()
62 | done()
--------------------------------------------------------------------------------
/05.py:
--------------------------------------------------------------------------------
1 | import turtle
2 | import json
3 |
4 | def draw_from_json(json_file):
5 | # Configurar turtle
6 | screen = turtle.Screen()
7 | screen.bgcolor("black")
8 | screen.setup(800, 800)
9 | t = turtle.Turtle()
10 | t.hideturtle()
11 | t.speed(0)
12 | screen.tracer(0)
13 |
14 | # Cargar regiones
15 | with open(json_file) as f:
16 | regions = json.load(f)
17 |
18 | # Calcular límites para centrar el dibujo
19 | all_points = [(p[0], p[1]) for r in regions
20 | for p in r['contour']]
21 | min_x = min(p[0] for p in all_points)
22 | max_x = max(p[0] for p in all_points)
23 | min_y = min(p[1] for p in all_points)
24 | max_y = max(p[1] for p in all_points)
25 |
26 | # Calcular escala y centro
27 | width = max_x - min_x
28 | height = max_y - min_y
29 | scale = min(600 / width, 600 / height)
30 | center_x = (min_x + max_x) / 2
31 | center_y = (min_y + max_y) / 2
32 |
33 | # Dibujar cada región
34 | for region in regions:
35 | # Configurar color
36 | color = '#{:02x}{:02x}{:02x}'.format(
37 | int(region['color'][0]),
38 | int(region['color'][1]),
39 | int(region['color'][2])
40 | )
41 | t.color(color, color)
42 |
43 | # Dibujar contorno
44 | points = region['contour']
45 | t.begin_fill()
46 | t.penup()
47 |
48 | # Primer punto
49 | x = (points[0][0] - center_x) * scale
50 | y = (center_y - points[0][1]) * scale
51 | t.goto(x, y)
52 | t.pendown()
53 |
54 | # Resto de puntos
55 | for point in points[1:]:
56 | x = (point[0] - center_x) * scale
57 | y = (center_y - point[1]) * scale
58 | t.goto(x, y)
59 |
60 | # Cerrar forma
61 | t.goto((points[0][0] - center_x) * scale,
62 | (center_y - points[0][1]) * scale)
63 | t.end_fill()
64 | screen.update()
65 |
66 | screen.mainloop()
67 |
68 | if __name__ == "__main__":
69 | draw_from_json("resources/sunflowers.json")
70 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # turtle-python
2 | This repository is a collection of [Python](https://www.python.org/) 🐍 scripts that use the [Turtle](https://docs.python.org/3/library/turtle.html) library to foster creative programming through digital art. Based on the book [Fun With Python: Using Turtle Graphics](https://amzn.to/3Y3rpG1) 📕.
3 |
4 | ## 🧑💻 You can create...
5 |
6 | ### • [01](https://www.instagram.com/p/C6w5b-MvVob/):
7 |
8 |
9 |
10 |
11 | ### • [02](https://www.instagram.com/p/C0zd1HNvfCR/):
12 |
13 | > Input: *resources/tulipanes.docx*
14 |
15 |
16 |
17 | ### • [03](https://www.instagram.com/p/C9igLLzyX_s/):
18 |
19 |
20 |
21 |
22 | ### • [04](https://www.instagram.com/p/C_1eAukyWOu/):
23 |
24 |
25 |
26 |
27 | ### • [05](https://www.instagram.com/p/DKhzj7fAbSV/):
28 |
29 | > Input: *resources/sunflowers.json*
30 |
31 |
32 |
--------------------------------------------------------------------------------
/resources/tulipanes.docx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Sandreke/turtle-python/b4ef1a47feb2c621f0923e96a666f769c001f06e/resources/tulipanes.docx
--------------------------------------------------------------------------------