-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraw_th_flag_turtle.py
85 lines (71 loc) · 2.26 KB
/
draw_th_flag_turtle.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
## GitHub: dark-teal-coder
## First Published Date: 2021-09-21
## Program Input(s):
### (1) taking a number representing the width of the flag
## Program Process(es):
### (1) setting up the screen and using the turtle module to draw the flag line by line
## Program Output(s):
### (1) a new pop-up window with the flag of Thailand
## Program Description: This program draws a flag of Thailand using Python built-in Turtle module.
####################################################################################################
import turtle
def set_up_screen():
## Create a turtle window object
screen = turtle.Screen()
## Maximize the screen
screen.setup(width=0.5, height=1.0)
## Change the title of the turtle window
screen.title("See Python turtle draw flag of Thailand! ~Ree")
def draw_stripe(t, fill_color: str, flag_wid: int, flag_len: int) -> None:
"""This function draws a color-filled stripe."""
t.pendown()
t.color(fill_color, fill_color)
t.begin_fill()
## Set turtle's orientation to east
t.setheading(0)
t.forward(flag_len)
t.right(90)
## Each stripe on the TH flag is one sixth of its width
t.forward(flag_wid/6)
t.right(90)
t.forward(flag_len)
t.right(90)
t.forward(flag_wid/6)
t.end_fill()
## Set turtle's orientation to south
t.setheading(270)
t.forward(flag_wid/6)
t.penup()
def draw_flag(width):
## Create a turtle object
t = turtle.Turtle()
## Change pen thickness
t.pensize(1)
## Set drawing speed
t.speed(100)
## Flag ratio
flag_wid = width
flag_len = (9/6) * flag_wid
## Pen up and go to the top left corner of the flag
t.penup()
t.goto(-(flag_len/2), flag_wid/2)
## Start drawing
colors = ["red", "white", "blue"]
for color in colors:
draw_stripe(t, color, flag_wid, flag_len)
for color in colors[::-1]:
draw_stripe(t, color, flag_wid, flag_len)
## Make turtle invisible
t.hideturtle()
if __name__ == '__main__':
set_up_screen()
draw_flag(width=200)
## Listen to events before continuing
# turtle.mainloop()
try:
turtle.exitonclick()
except:
print("The program didn't exit properly!")
####################################################################################################
## REFERENCES:
### Thailand's flag ratio: https://asean.org/wp-content/uploads/2012/05/Flag-of-the-Kingdom-of-Thailand.pdf