2021年9月8日 星期三

第四堂課 turtle繪圖(三)世界國旗版

中華民國(國徽、國旗)、新加坡、歐盟、英國、中國 (待增...)

一、中華民國國徽

"""
subject: draw a nation flag
@author: wuhinco
"""
import turtle
turtle.setup(600, 700)
wn = turtle.Screen()
wn.title("Draw Nation flag")
t = turtle.Turtle()
t.pensize(2)
color = ["red", "gold", "green", "blue"]
#wn.bgcolor("white")
t.pencolor("black")
t.speed(5)
r1 = 75
r2 = 85
r3 = 150
r4 = r1 * 3
r1_pos = []
r3_pos = []
# draw r4
t.penup()
t.home()
t.color("blue", "blue")
t.begin_fill()
t.right(90)
t.pensize(1)
t.forward(r1 * 2)
t.setheading(0)
t.pendown()
t.circle(r4)
t.end_fill()


#draw r1
t.color("white", "white")
t.penup()
t.home()
t.begin_fill()
t.circle(r1, 15)
r1_pos.append(t.pos())
for i in range(11):
    t.circle(r1, 30)
    r1_pos.append(t.pos())
t.circle(r1, 15)
t.end_fill()

#draw r3
t.pensize(1)
t.penup()
t.home()
t.right(90)
t.forward(r1)
t.setheading(0)
#t.pendown()
r3_pos.append(t.pos())
for i in range(12):
    t.circle(r3, 30)
    r3_pos.append(t.pos())
#draw star
t.penup()
t.color("white", "white")
t.begin_fill()
for x in range(12):
    #t.pencolor(color[x % 4])
    #t.setpos()
    t.pendown()
    t.goto(r3_pos[x])
    t.goto(r1_pos[x])
t.goto(r3_pos[-1])
t.end_fill()
#draw r2
t.pencolor("blue")
t.penup()
t.home()旗
t.right(90)
x = r1 * 2 / 15
t.pensize(x)
t.forward(x / 2)
t.setheading(0)
t.pendown()
t.circle(r1 + x / 2)
t.hideturtle()

wn.mainloop()

二、中華民國國旗

"""
subject: 
    1. draw a nation flag by circle's center and become functions
    2. draw entire flag
@author: wuhinco
"""
import turtle 
def draw_medal(radius):
    
    global wn
    turtle.setup(radius * 16, radius * 32/3)
    wn = turtle.Screen()
    wn.title("Draw Nation flag")
    wn.bgcolor("red")
    
    # new home position    
    x = -radius * 4
    y = radius * 5 / 3 
    t = turtle.Turtle()
    t.pensize(2)
    t.pencolor("black")
    t.speed(4)
    r1 = radius
    #r2 = r1 + (r1 * 2)/15
    r3 = r1 * 2
    r4 = r1 * 3   #225
    
    r1_pos = []
    r3_pos = []
    
    
    """
    # draw r4
    t.penup()
    #t.home()
    t.goto(x, y)
    
    t.setheading(0)
    t.color("blue", "blue")
    t.begin_fill()
    
    t.right(90)
    t.pensize(1)
    t.forward(r1 * 2)
    t.setheading(0)
    t.pendown()
    
    t.circle(r4)
    t.end_fill()
    """
    # draw square
    t.penup()
    t.home()  
    t.color("blue", "blue")
    t.begin_fill()
    t.pendown()
    for i in range(2):
        t.left(90)
        t.forward(r1 * 16 / 3)
        t.left(90)
        t.forward(r1 * 8)    
    t.end_fill()
    
    
    #draw r1
    
    t.color("white", "white")
    t.penup()
    #t.home()
    t.goto(x, y)
    t.setheading(0)
    
    t.begin_fill()
    t.circle(r1, 15)
    r1_pos.append(t.pos())
    for i in range(11):
        t.circle(r1, 30)
        r1_pos.append(t.pos())
    t.circle(r1, 15)
    t.end_fill()
    
    
    #draw r3
    t.pensize(1)
    t.penup()
    #t.home()
    t.goto(x, y)
    t.setheading(0)
    
    t.right(90)
    t.forward(r1)
    t.setheading(0)
    #t.pendown()
    r3_pos.append(t.pos())
    for i in range(12):
        t.circle(r3, 30)
        r3_pos.append(t.pos())
    
    #draw star
    t.penup()
    t.color("white", "white")
    t.penup()
    t.begin_fill()
    for i in range(12):
        t.pendown()
        t.goto(r3_pos[i])
        t.goto(r1_pos[i])
    t.goto(r3_pos[-1])
    t.end_fill()
    
    #draw r2
    t.pencolor("blue")
    t.penup()
    #t.home()
    t.goto(x, y)
    t.setheading(0)
    
    t.right(90)
    x = r1 * 2 / 15
    t.pensize(x)
    t.forward(x / 2)
    t.setheading(0)
    t.pendown()
    t.circle(r1 + x / 2)
    t.hideturtle()
    
draw_medal(75)
wn.mainloop()



三、新加坡國旗

"""
subject: 
    1. draw singapore's flag 
    2. draw entire flag
@author: wuhinco
"""
import turtle 
import random
turtle.setup(900, 600)
wn = turtle.Screen()
wn.title("Draw Singapore flag")
wn.bgcolor("white")
def draw_star(center_pos, line, color, rate=0):
    x, y = center_pos     
    y -= 0.5 * line
    t = turtle.Turtle()
    t.hideturtle()
    t.speed(rate)
    t.penup()
    t.goto(x, y)  
    t.pendown()
    #t.setheading(300)
    t.color(color, color) 
    t.right(36)
    t.begin_fill()
    for i in range(5):  
        t.forward(line)
        t.left(144)
        t.forward(line)
        t.right(72)
    t.end_fill()
    
def draw_rect(pos, f_color, b_color, rate=5):
    t3 = turtle.Turtle() 
    t3.hideturtle()
    t3.speed(rate)
    t3.color(f_color, b_color)
    t3.pendown()
    t3.begin_fill()
    t3.goto(pos[-1])
    start_pos = end_pos = t3.pos()
    for i in pos:
        t3.goto(i)
    t3.goto(end_pos)
    t3.end_fill()
# draw red block and white block( function version )
draw_rect([(450,0),(450,300),(-450,300),(-450,0)], "red", "red")
draw_rect([(450,0),(450,-300),(-450,-300),(-450,0)], "white", "white")

t2 = turtle.Turtle() 
t2.hideturtle()
"""
# draw red block and white block( non function version )
t2.speed(10)
t2.color("red", "red")
t2.pendown()
t2.begin_fill()
t2.forward(wn.window_width()/2)
t2.left(90)
t2.forward(wn.window_height()/2)
t2.left(90)
t2.forward(wn.window_width())
t2.left(90)
t2.forward(wn.window_height()/2)
t2.left(90)
t2.forward(wn.window_width()/2)
t2.end_fill()
# draw white block
t2.color("white", "white")
t2.pendown()
t2.begin_fill()
t2.forward(wn.window_width()/2)
t2.right(90)
t2.forward(wn.window_height()/2)
t2.right(90)
t2.forward(wn.window_width())
t2.right(90)
t2.forward(wn.window_height()/2)
t2.right(90)
t2.forward(wn.window_width()/2)
t2.end_fill()
t2.penup()
"""
t2.speed(1)
t2.penup()
t2.color("white", "white")
pos_x = - wn.window_width()/4 -20
pos_y =  wn.window_height()/4
radius = wn.window_height()/4
t2.goto(pos_x , pos_y)
t2.pendown()
radius = radius *3 /2
t2.dot(radius, "white")
pos_x +=45
t2.penup()
t2.goto(pos_x, pos_y)
t2.pendown()
t2.dot(radius, "red")
pos_y -=radius/4
t2.penup()
t2.sety(pos_y)
t2.speed(0)
radius = radius / 4
t2.circle(radius,36)
for i in range(5):
    draw_star(t2.pos(), 18, "white")
    t2.circle(radius+5,72)
wn.mainloop()




四、英國國旗

"""
subject: 
    1. draw a UK flag 
    2. draw entire flag
@author: wuhinco
"""
import turtle 
turtle.setup(900, 600)
wn = turtle.Screen()
wn.title("Draw UK flag")
wn.bgcolor("blue")
top_mid_x = 0
top_mid_y =  wn.window_height() / 2
top_left_x = - wn.window_width() / 2
top_left_y = wn.window_height() / 2
left_mid_x = - wn.window_width() / 2
left_mid_y = 0
bottom_left_x = - wn.window_width() / 2
bottom_left_y = - wn.window_height() / 2
bottom_mid_x = 0
bottom_mid_y = - wn.window_height() / 2
bottom_right_x = wn.window_width() / 2
bottom_right_y = - wn.window_height() / 2
right_mid_x = wn.window_width() / 2
right_mid_y = 0
top_right_x = wn.window_width() / 2
top_right_y = wn.window_height() / 2
shift = 20
t = turtle.Turtle()
t.hideturtle()
# draw x  white
t.pencolor("white")
t.pensize(80)
t.speed(5)
t.penup()
t.goto(top_left_x , top_left_y)
t.pendown()
t.goto(bottom_right_x , bottom_right_y)
t.penup()
t.goto(top_right_x , top_right_y)
t.pendown()
t.goto(bottom_left_x , bottom_left_y)

# draw x  red
t.pencolor("red")
t.pensize(30)
t.speed(5)
t.penup()
t.goto(top_left_x , top_left_y-shift)
t.pendown()
t.goto(0, -shift)
t.goto(0, shift)
t.goto(bottom_right_x , bottom_right_y + shift)
t.penup()
t.goto(top_right_x, top_right_y + shift)
t.pendown()
t.goto(0, shift)
t.goto(0, -shift)
t.goto(bottom_left_x , bottom_left_y-shift)
# draw + white
t.pencolor("white")
t.penup()
t.pensize(140)
t.goto(top_mid_x , top_mid_y)
t.pendown()
t.goto(bottom_mid_x , bottom_mid_y)
t.penup()
t.pensize(170)
t.goto(left_mid_x , left_mid_y)
t.pendown()
t.goto(right_mid_x , right_mid_y)
# draw + red
t.penup()
t.pencolor("red")
t.pensize(80)
t.goto(top_mid_x , top_mid_y)
t.pendown()
t.goto(bottom_mid_x , bottom_mid_y)
t.penup()
t.pensize(100)
t.goto(left_mid_x , left_mid_y)
t.pendown()
t.goto(right_mid_x , right_mid_y)
  
wn.mainloop()



五、歐盟

"""

subject: 
    1. draw a EU flag by circle's center and become functions
    2. draw entire flag
@author: wuhinco
"""
import turtle 
turtle.setup(900, 600)
wn = turtle.Screen()
wn.title("Draw Europe Union flag")
wn.bgcolor("blue")
def draw_star(base_pos, line):
    x, y = base_pos     
    t = turtle.Turtle()
    t.hideturtle()
    t.pencolor("black")
    t.speed(0)
    t.penup()
    t.goto(x, y)  
    t.pendown()
    #t.setheading(300)
    t.color("gold", "gold") 
    t.right(36)
    t.begin_fill()
    for i in range(5):  
        t.forward(line)
        t.left(144)
        t.forward(line)
        t.right(72)
    t.end_fill()
    
#draw_star((0, 0), 100)
t2 = turtle.Turtle() 
t2.hideturtle()
t2.speed(0)
pos = []
t2.penup()
t2.home()
radius = wn.window_height()/3
t2.goto(0,-radius)
for i in range(12):
    t2.circle(radius , 30)
    draw_star(t2.pos(), 15)
    
wn.mainloop()




六、中國

"""
subject: 
    1. draw a china flag 
    2. draw entire flag
@author: wuhinco
"""
import turtle 
turtle.setup(900, 600)
wn = turtle.Screen()
wn.title("Draw china flag")
wn.bgcolor("red")
def draw_star(center_pos, line, color):
    x, y = center_pos     
    y -= 0.5 * line
    t = turtle.Turtle()
    t.hideturtle()
    t.speed(0)
    t.penup()
    t.goto(x, y)  
    t.pendown()
    #t.setheading(300)
    t.color(color, color) 
    t.right(36)
    t.begin_fill()
    for i in range(5):  
        t.forward(line)
        t.left(144)
        t.forward(line)
        t.right(72)
    t.end_fill()
    
pos_x = -wn.window_width()/3
pos_y =  wn.window_height()/4
line = 70
draw_star((pos_x, pos_y), line, "yellow")

# draw 45 degrees radian and four stars
t2 = turtle.Turtle() 
t2.hideturtle()
t2.speed(6)
pos = []
t2.penup()
t2.goto(pos_x, pos_y)
radius = line * 5/2
t2.goto(pos_x + line/2+40, pos_y-radius+15)
#t2.goto(0,-radius)
#t2.circle(radius-40 , 10)
for i in range(4):
    t2.circle(radius-40 , 36)
    draw_star(t2.pos(), 18, "yellow")
wn.mainloop()





沒有留言:

張貼留言

習題1. 畫Pokemon 精靈球

任務:參考影片的繪圖步驟,自行完成本頁的範例程式,繪製一顆Pokemon精靈球 解答:在本頁的底部。 """ subject: draw a Pokemon Ball 程式設計:wuhinco@gmai.com  """...