可能是全网最好的Python教程:绘制国旗
江湖秋水多123:
def draw_line(x1, y1, x2, y2, c):
t.speed(3)
t.color(c)
t.penup()
t.goto(x1 * gw, y1 * gw)
t.pendown()
t.goto(x2 * gw, y2 * gw)# 画五个星星连线
def draw_ligature(c):
for i in range(1, 5):
draw_line(five_stars【0】【0】 - w / 2, h / 2 - five_stars【0】【1】, five_stars【i】【0】 - w / 2,
h / 2 - five_stars【i】【1】, c)def set_dot(x, y, size):
t.penup()
t.goto((x - w / 2) * gw, (h / 2 - y) * gw)
if five_stars【0】【0】 != x:
if y < five_stars【0】【1】:
t.seth(-90 - m.atan(m.fabs((x - five_stars【0】【0】) / (y - five_stars【0】【1】))) * 180 / m.pi)
else:
t.seth(90 + m.atan(m.fabs((x - five_stars【0】【0】) / (y - five_stars【0】【1】))) * 180 / m.pi)
else:
t.seth(90)
t.color('white')
t.pd()
t.fd(gw * size)
江湖秋水多123:
def draw_star(size, c, is_Fill):
t.color(c)
t.pd()
t.right(90 + 72)
if is_Fill:
t.begin_fill()
for i in range(5):
t.fd(m.cos(18 * m.pi / 180) * gw * size * 2)
t.right(144)
if is_Fill:
t.end_fill()def draw_fivestars(is_Fill):
for i in range(5):
if i == 0:
set_dot(five_stars【i】【0】, five_stars【i】【1】, 3)
draw_star(3, 'yellow', is_Fill)
else:
set_dot(five_stars【i】【0】, five_stars【i】【1】, 1)
draw_star(1, 'yellow', is_Fill)# 写字(文字/数字)
def write_sth(x, y, sth, length=100, c='red', ps=16):
moveto(x, y)
t.color(c)
t.pensize(ps)
t.write('{0: ^3}'.format(sth), font=('黑体', ps, 'normal'))# 调用函数部分
init()draw_grid('white')
draw_ligature('black')
t.speed(8)
draw_fivestars(False)t.speed(0)
t.clear()
draw_mianzi()
draw_fivestars(True)write_sth(-5 * gw, -gw * h / 2 - gw * 3, '祖国在我心中', ps=24)t.ht()
t.done()
D_d0S:
很详细,但是建议解释一下代码[热词系列_你币有了]
江湖秋水多123:
import turtle as t
import math as mgw = 15 # 每格子的大小
w = 30 # 宽里的格子数
h = 20 # 高里的格子数
five_stars = 【(5, 5), (10, 2), (12, 4), (12, 7), (10, 9)】 # 五星的座标def init():
# t.setup(gw*w,gw*h,-gw*w/2,w*h/2)
t.speed(0)
draw_mianzi()def moveto(x, y):
t.pu()
t.goto(x, y)
t.pd()def draw_mianzi():
t.color('red')
moveto(-w * gw / 2, h * gw / 2)
t.seth(0)
t.begin_fill()
for i in range(2):
t.fd(w * gw)
t.right(90)
t.fd(h * gw)
t.right(90)
t.end_fill()def draw_grid(c):
t.speed(8)
t.color(c)
# 画横线
for i in range(h // 2):
moveto(-gw * w / 2, gw * 9 - i * gw)
t.fd(gw * w / 2) t.pu()
# 画竖线
t.seth(-90)
for i in range(w // 2):
moveto(-gw * 14 + i * gw, gw * h / 2)
t.fd(gw * h / 2)
江湖秋水多123:
应友友们的需求,我把代码分享给大家,有什么不明白的地方,可以私聊我,记得三连哟!
链接:https://pan.baidu.com/s/1AdU4AokB1U9ujZovnNOIow?pwd=fggh
提取码:fggh
给你一记暴击:
给一下源代码呗,搞不懂代码哪里出问题了,说没没文件
Dovahakiin:
老师啊,您最后的fivestar是不是应该为five_star,另外其他都正常的,为啥我运行出来的是空心的五星线,而不是实心?[脱单doge][笑哭]