用Python画一颗心、小人发射爱心
源码:
1 #!/usr/bin/env python 2 3 # -*- coding:utf-8 -*- 4 5 import turtle 6 import time 7 8 # 画心形圆弧 9 10 def hart_arc(): 11 12 for i in range(200): 13 14 turtle.right(1) 15 16 turtle.forward(2) 17 18 19 def move_pen_position(x, y): 20 21 turtle.hideturtle() # 隐藏画笔(先) 22 23 turtle.up() # 提笔 24 25 turtle.goto(x, y) # 移动画笔到指定起始坐标(窗口中心为0,0) 26 27 turtle.down() # 下笔 28 29 turtle.showturtle() # 显示画笔 30 31 32 # 初始化 33 34 turtle.setup(width=800, height=500) # 窗口(画布)大小 35 36 turtle.color('red', 'pink') # 画笔颜色 37 38 turtle.pensize(3) # 画笔粗细 39 40 turtle.speed(1) # 描绘速度 41 42 # 初始化画笔起始坐标 43 44 move_pen_position(x=0,y=-180) # 移动画笔位置 45 46 turtle.left(140) # 向左旋转140度 47 48 turtle.begin_fill() # 标记背景填充位置 49 50 # 画心形直线( 左下方 ) 51 turtle.forward(224) # 向前移动画笔,长度为224 52 53 # 画爱心圆弧 54 55 hart_arc() # 左侧圆弧 56 turtle.left(120) # 调整画笔角度 57 hart_arc() # 右侧圆弧 58 59 # 画心形直线( 右下方 ) 60 61 turtle.forward(224) 62 63 turtle.end_fill() # 标记背景填充结束位置 64 65 # 点击窗口关闭程序 66 67 window = turtle.Screen() 68 69 window.exitonclick()
效果图:
源码:
1 import turtle as t 2 from time import sleep 3 def go_to(x, y): 4 t.up() 5 t.goto(x, y) 6 t.down() 7 def head(x, y, r): 8 go_to(x, y) 9 t.speed(20) 10 t.circle(r) 11 leg(x, y) 12 def leg(x, y): 13 t.right(90) 14 t.forward(180) 15 t.right(30) 16 t.forward(100) 17 t.left(120) 18 go_to(x, y - 180) 19 t.forward(100) 20 t.right(120) 21 t.forward(100) 22 t.left(120) 23 hand(x, y) 24 def hand(x, y): 25 go_to(x, y - 60) 26 t.forward(100) 27 t.left(60) 28 t.forward(100) 29 go_to(x, y - 90) 30 t.right(60) 31 t.forward(100) 32 t.right(60) 33 t.forward(100) 34 t.left(60) 35 eye(x, y) 36 def eye(x, y): 37 go_to(x - 50, y + 130) 38 t.right(90) 39 t.forward(50) 40 go_to(x + 40, y + 130) 41 t.forward(50) 42 t.left(90) 43 def big_Circle(size): 44 t.speed(20) 45 for i in range(150): 46 t.forward(size) 47 t.right(0.3) 48 def line(size): 49 t.speed(20) 50 t.forward(51 * size) 51 def small_Circle(size): 52 t.speed(20) 53 for i in range(210): 54 t.forward(size) 55 t.right(0.786) 56 def heart(x, y, size): 57 go_to(x, y) 58 t.left(150) 59 t.begin_fill() 60 line(size) 61 big_Circle(size) 62 small_Circle(size) 63 t.left(120) 64 small_Circle(size) 65 big_Circle(size) 66 line(size) 67 t.end_fill() 68 def main(): 69 t.pensize(2) 70 t.color('red', 'pink') 71 head(-120, 100, 100) 72 heart(250, -80, 1) 73 go_to(100, -300) 74 t.write("To: 智慧与美貌并存的", move=True, align="left", font=("楷体", 20, "normal")) 75 t.done() 76 main()