相关文件
关注小编,私信小编领取哟!
当然别忘了一件三连哟~~
公众号:Python日志
可以关注小编公众号,会不定时的发布一下Python小技巧,还有很多资源可以免费领取哟!!
源码领取:加Python学习交流群:773162165 可以领取哟
开发工具
Python版本:3.7.8
相关模块:
turtle模块;
time模块;
pyttsx3模块;
以及一些python自带的模块。
环境搭建
安装Python并添加到环境变量,pip安装需要的相关模块即可。
一:画爱心
效果展示
代码展示
import turtle
import time
# 画心形圆弧
def hart_arc():
for i in range(200):
turtle.right(1)
turtle.forward(2)
def move_pen_position(x, y):
turtle.hideturtle() # 隐藏画笔(先)
turtle.up() # 提笔
turtle.goto(x, y) # 移动画笔到指定起始坐标(窗口中心为0,0)
turtle.down() # 下笔
turtle.showturtle() # 显示画笔
love = input("请输入表白话语,默认为‘I Love You’:")
signature = input("请签署你的大名,不填写默认不显示:")
if love == '':
love = 'I Love You'
time.sleep(1)
# 初始化
turtle.setup(width=800, height=500) # 窗口(画布)大小
turtle.color('red', 'pink') # 画笔颜色
turtle.pensize(3) # 画笔粗细
turtle.speed(1) # 描绘速度
# 初始化画笔起始坐标
move_pen_position(x=0,y=-180) # 移动画笔位置
turtle.left(140) # 向左旋转140度
turtle.begin_fill() # 标记背景填充位置
# 画心形直线( 左下方 )
turtle.forward(224) # 向前移动画笔,长度为224
# 画爱心圆弧
hart_arc() # 左侧圆弧
turtle.left(120) # 调整画笔角度
hart_arc() # 右侧圆弧
# 画心形直线( 右下方 )
turtle.forward(224)
turtle.end_fill() # 标记背景填充结束位置
# 在心形中写上表白话语
move_pen_position(0,0) # 表白语位置
turtle.hideturtle() # 隐藏画笔
turtle.color('#CD5C5C', 'pink') # 字体颜色
# font:设定字体、尺寸(电脑下存在的字体都可设置) align:中心对齐
turtle.write(love, font=('Arial', 30, 'bold'), align="center")
# 签写署名
if signature != '':
turtle.color('red', 'pink')
time.sleep(2)
move_pen_position(180, -180)
turtle.hideturtle() # 隐藏画笔
turtle.write(signature, font=('Arial', 20), align="center")
# 点击窗口关闭程序
window = turtle.Screen()
window.exitonclick()
二:让女神欲罢不能的套路
效果展示
代码展示
import turtle
import time
# 清屏函数
def clear_all():
turtle.penup()
turtle.goto(0, 0)
turtle.color('white')
turtle.pensize(800)
turtle.pendown()
turtle.setheading(0)
turtle.fd(300)
turtle.bk(600)
# 重定位海龟的位置
def go_to(x, y, state):
turtle.pendown() if state else turtle.penup()
turtle.goto(x, y)
# 画线
# state为真时海龟回到原点,为假时不回到原来的出发点
def draw_line(length, angle, state):
turtle.pensize(1)
turtle.pendown()
turtle.setheading(angle)
turtle.fd(length)
turtle.bk(length) if state else turtle.penup()
turtle.penup()
# 画箭羽
def draw_feather(size):
angle = 30 # 箭的倾角
feather_num = size//6 # 羽毛的数量
feather_length = size // 3 # 羽毛的长度
feather_gap = size//10 # 羽毛的间隔
for i in range(feather_num):
draw_line(feather_gap, angle+180, False) # 箭柄,不折返
draw_line(feather_length, angle + 145, True) # 羽翼,要折返
draw_line(feather_length, angle + 145, False)
draw_line(feather_num*feather_gap, angle, False)
draw_line(feather_length, angle + 145 + 180, False)
for i in range(feather_num):
draw_line(feather_gap, angle+180, False) # 箭柄,不折返
draw_line(feather_length, angle - 145, True) # 羽翼,要折返
draw_line(feather_length, angle - 145, False)
draw_line(feather_num*feather_gap, angle, False)
draw_line(feather_length, angle - 145 + 180, False)
# 画爱心
def draw_heart(size):
turtle.color('red', 'pink')
turtle.pensize(2)
turtle.pendown()
turtle.setheading(150)
turtle.begin_fill()
turtle.fd(size)
turtle.circle(size * -3.745, 45)
turtle.circle(size * -1.431, 165)
turtle.left(120)
turtle.circle(size * -1.431, 165)
turtle.circle(size * -3.745, 45)
turtle.fd(size)
turtle.end_fill()
def hart_arc():
for i in range(200):
turtle.right(1)
turtle.forward(2)
# 画箭
def draw_arrow(size):
angle = 30
turtle.color('black')
draw_feather(size)
turtle.pensize(4)
turtle.setheading(angle)
turtle.pendown()
turtle.fd(size*2)
# 一箭穿心
# 箭的头没有画出来,而是用海龟来代替
def arrow_heart(x, y, size):
go_to(x, y, False)
draw_heart(size*1.15)
turtle.setheading(-150)
turtle.penup()
turtle.fd(size*2.2)
draw_heart(size)
turtle.penup()
turtle.setheading