这两天在学习Python的turtle,昨天下午用 goto() 语句画了一个火柴人,今天改进了一下,并加了两个爱心。
#改进前代码
from turtle import * #color("red", "yellow") bgcolor('azure') pensize(10)
pencolor("yellow") goto(0,100) color("yellow","pink") begin_fill() circle(30)
end_fill() goto(0,0) goto(-70.7,-70.7) goto(0,0) goto(70.7,-70.7) goto(0,0) goto
(0,50) goto(42.5,75) pencolor("pink") goto(42.5,90) goto(42.5,75) pencolor(
"yellow") goto(0,50) goto(-42.5,25) pencolor("pink") goto(-42.5,10) hideturtle()
done()
运行效果

改进后的代码
'''小小爱心火柴人''' from turtle import * from time import sleep bgcolor('azure')
pensize(6) pencolor("yellow") def hand_leg_color(): pencolor("pink") forward(20)
pencolor("yellow") def go_to(x, y): up() goto(x, y) down() def head(x, y, r):
go_to(x, y) fillcolor("pink") begin_fill() circle(r) end_fill() leg(x, y) def
leg(x, y): right(90) forward(100) right(45) forward(60) hand_leg_color() go_to(x
, y-100) left(90) forward(60) hand_leg_color() hand(x, y) def hand(x, y): go_to(
x, y-50) left(75) forward(75) left(60) hand_leg_color() go_to(x, y-50)
#left(120) left(60) forward(75) #left(60) right(60) hand_leg_color() #left(90)
right(90) def big_circle(size): speed(0) for i in range(100): forward(size)
right(0.45) def line(size): speed(0) forward(63 * size) def small_circle(size):
speed(0) for i in range(180): forward(size) right(0.917) def heart(x, y, size):
go_to(x, y) left(120) pencolor('red') pensize(2) fillcolor("pink") begin_fill()
line(size) big_circle(size) small_circle(size) left(120) small_circle(size)
big_circle(size) line(size) end_fill() def main(): head(0, 100, 30) heart(150,
50, 1) right(120) heart(-150, 50, 1) go_to(-195, -150) pensize(2) write("To:
the fairy with wisdom and beauty!", move=True, align="left", font=("华文行楷", 20,
"normal")) hideturtle() done() main()
运行效果

七夕节,想着写一个什么复习一下前几天学的东西,看文章有一个漫天星的想法很不错,并且也切合这个主题,于是就写了一个漫天心的代码
'''七夕,满天心''' import turtle as t import random t.bgcolor('azure') #画板颜色,浅蓝 def
go_to(x, y): t.up() t.goto(x, y) t.down() #画心 def draw_heart(size, color_):
#color('red', color_) t.speed(0) t.colormode(255) t.color(color_) t.begin_fill()
#t.left(120) 效果不同 t.setheading(120) #爱心正 t.forward(30 * size) t.circle(-8.66*
size, 180) t.left(120) t.circle(-8.66*size, 180) t.forward(30 * size) t.end_fill
() #漫天心 def draw_pic(): # color 三原色 colors_1 = random.randint(0, 255) colors_2 =
random.randint(0, 255) colors_3 = random.randint(0, 255) t.penup() # pos x =
random.randint(-400, 400) y = random.randint(-200, 200) go_to(x, y) # size size
= random.randint(0, 4) draw_heart(size, (colors_1, colors_2, colors_3)) def main
(): t.hideturtle() t.setup(900, 700) #画屏大小 # number for i in range(20): draw_pic
() go_to(-100, 0) t.pencolor('red') t.write("七夕,在一起!", move=True, align="left",
font=("华文行楷", 20, "normal")) go_to(-445, 315) t.write("To:My dear!", move=True,
align="left", font=("Comic Sans MS", 20, "normal")) t.done() main()
运行的效果:

技术
下载桌面版
GitHub
百度网盘(提取码:draw)
Gitee
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:766591547
关注微信