用phthon海龟绘图,写出“李博文”三个字,“彭媛裕”三个字,最后写一个“心”字,都分开写,必须附有图片,并给相关代码。不用chatgpt
现在的年轻人整的还挺浪漫。
import turtle
def draw_lbword():
turtle.penup()
turtle.goto(-280,0)
turtle.pendown()
turtle.write("李",font=("Arial", 50, "normal"))
turtle.penup()
turtle.goto(-180,0)
turtle.pendown()
turtle.write("博",font=("Arial", 50, "normal"))
turtle.penup()
turtle.goto(-80,0)
turtle.pendown()
turtle.write("文",font=("Arial", 50, "normal"))
def draw_pyword():
turtle.penup()
turtle.goto(80,0)
turtle.pendown()
turtle.write("彭",font=("Arial", 50, "normal"))
turtle.penup()
turtle.goto(180,0)
turtle.pendown()
turtle.write("媛",font=("Arial", 50, "normal"))
turtle.penup()
turtle.goto(280,0)
turtle.pendown()
turtle.write("裕",font=("Arial", 50, "normal"))
def draw_heart():
turtle.penup()
turtle.goto(380,0)
turtle.pendown()
turtle.begin_fill()
turtle.fillcolor('red')
turtle.left(45)
turtle.forward(100)
turtle.circle(50, 180)
turtle.right(90)
turtle.circle(50, 180)
turtle.forward(100)
turtle.end_fill()
turtle.speed(0)
draw_lbword()
draw_heart()
draw_pyword()
turtle.done()
该回答引用chatgpt:
import turtle
# 设置画布和画笔
canvas = turtle.Screen()
canvas.bgcolor("white")
pen = turtle.Turtle()
pen.speed(3) # 设置绘制速度
# 绘制“李博文”
pen.penup()
pen.goto(-200, 0)
pen.pendown()
pen.write("李", align="left", font=("Arial", 36, "bold"))
pen.penup()
pen.goto(-150, 0)
pen.pendown()
pen.write("博", align="left", font=("Arial", 36, "bold"))
pen.penup()
pen.goto(-100, 0)
pen.pendown()
pen.write("文", align="left", font=("Arial", 36, "bold"))
# 绘制“彭媛裕”
pen.penup()
pen.goto(0, 0)
pen.pendown()
pen.write("彭", align="left", font=("Arial", 36, "bold"))
pen.penup()
pen.goto(50, 0)
pen.pendown()
pen.write("媛", align="left", font=("Arial", 36, "bold"))
pen.penup()
pen.goto(100, 0)
pen.pendown()
pen.write("裕", align="left", font=("Arial", 36, "bold"))
# 绘制“心”
pen.penup()
pen.goto(200, 0)
pen.pendown()
pen.write("心", align="left", font=("Arial", 36, "bold"))
# 隐藏海龟箭头
pen.hideturtle()
# 结束绘制
turtle.done()
# 导入turtle模块
import turtle
# 创建画布
screen = turtle.Screen()
# 设置画布大小
screen.setup(width=800, height=600)
# 创建一个海龟作为画笔
pen = turtle.Turtle()
# 设置海龟速度
pen.speed(1)
# 设置画笔粗细
pen.pensize(3)
# 李
x, y = -200, 0
pen.up()
pen.goto(x, y)
pen.down()
pen.goto(x + 50, y)
pen.goto(x + 50, y + 100)
pen.goto(x, y + 100)
pen.goto(x, y)
pen.up()
# 博
x, y = -80, 0
pen.up()
pen.goto(x, y)
pen.down()
pen.goto(x + 50, y)
pen.goto(x + 50, y + 100)
pen.goto(x, y + 100)
pen.goto(x + 50, y + 50)
pen.goto(x, y)
pen.up()
# 文
x, y = 80, 0
pen.up()
pen.goto(x, y)
pen.down()
pen.goto(x - 50, y + 100)
pen.goto(x, y)
pen.goto(x + 50, y + 100)
pen.goto(x, y)
pen.up()
# 彭
x, y = -200, -150
pen.up()
pen.goto(x, y)
pen.down()
pen.goto(x + 50, y)
pen.goto(x + 50, y + 100)
pen.goto(x, y + 100)
pen.goto(x, y)
pen.up()
# 媛
x, y = -80, -150
pen.up()
pen.goto(x, y + 100)
pen.down()
pen.goto(x + 50, y)
pen.goto(x + 50, y + 100)
pen.up()
pen.goto(x + 50, y + 50)
pen.down()
pen.goto(x, y + 50)
pen.up()
# 裕
x, y = 50, -150
pen.up()
pen.goto(x, y)
pen.down()
pen.goto(x - 25, y + 100)
pen.goto(x + 25, y + 100)
pen.goto(x, y + 50)
pen.up()
# 心
x, y = 150, -150
pen.up()
pen.goto(x, y)
pen.down()
pen.setheading(75)
pen.circle(50, 150)
pen.goto(x, y)
pen.setheading(105)
pen.circle(-50, 150)
# 隐藏画笔
pen.hideturtle()
# 点击窗口退出
screen.exitonclick()
绘制结果如下所示:
from turtle import *
from random import randint
c = turtle.Turtle() # 创建一个画笔对象
c.hideturtle() # 隐藏画笔
c.pensize(3) # 设置画笔粗细
c.speed(0) # 设置画笔速度
t = turtle.Turtle() # 创建一个画笔对象
t.hideturtle() # 隐藏画笔
t.pensize(3) # 设置画笔粗细
t.speed(0) # 设置画笔速度
# 定义一个函数,用来画一个心形
def draw_heart(x, y, color):
t.clear()
t.penup() # 抬起画笔
t.goto(x, y) # 移动到指定位置
t.pendown() # 落下画笔
# generate random RGB values
r = randint(0, 255)
g = randint(0, 255)
b = randint(0, 255)
# set the color mode to 255
colormode(255)
t.color(r,g,b) # 设置画笔颜色
t.setheading(0)#设置方向
t.begin_fill() # 开始填充
t.left(140) # 左转140度
t.forward(113) # 前进113步
t.circle(-70, 200) # 逆时针画一个半径为70,角度为200的圆弧
t.left(120) # 左转120度
t.circle(-70, 200) # 逆时针画一个半径为70,角度为200的圆弧
t.forward(138) # 前进113步
t.end_fill() # 结束填充
# 定义一个函数,用来写一个汉字
def write_chinese(x, y, word):
c.penup() # 抬起画笔
c.goto(x, y) # 移动到指定位置
c.pendown() # 落下画笔
c.write(word, font=("Verdana", 50, "bold")) # 写入汉字,设置字体、大小、样式
colormode(255)
c.color(randint(0, 255), randint(0, 255), randint(0, 255))
c.penup()
def moveHeart():
#clear the previous drawing
t.clear()
#move the turtle to a new position
# setpos(random.randint(-300, 300), random.randint(-300, 300))
#draw a new heart
draw_heart(randint(-300, 300),-250,"red")
#set a timer to call this function again after 1000 milliseconds
ontimer(moveHeart, 1000)
def write_name():
# 调用函数,画出“李博文”三个字
write_chinese(-150, 150, "李")
write_chinese(-50, 150, "博")
write_chinese(50, 150, "文")
# 调用函数,画出“彭媛裕”三个字
write_chinese(-150, -0, "彭")
write_chinese(-50, -0, "媛")
write_chinese(50, -0, "裕")
write_name()
# draw_heart(-50, -250, "red")#固定位置显示爱心
moveHeart()#随机位置显示爱心
turtle.done() # 结束绘图
def draw(t, pos, word, FONT, color = 'black'):
t.up()
t.goto(*pos)
t.pencolor(color)
t.write(word, font = FONT)
import turtle
turtle.speed(0)
turtle.setup(800, 600, 0, 0)
turtle.ht()
t = turtle.Turtle()
draw(t,(-260, 0),'李 博 文',('宋体', 66, 'bold') )
draw(t,(-260, -80),'彭 媛 裕',('宋体', 66, 'bold') )
draw(t,(180, -60),'♥',('宋体', 88, 'bold'),'red' )
draw(t,(150, -60),'心',('宋体', 88, 'bold') )
t.ht()
turtle.done()
以下答案由GPT-4.5Plus大模型(语音)与博主A努力中.......共同编写:
完整的可直接运行的代码如下:
python
import turtle
# 李博文
turtle.penup()
turtle.goto(-400, 200)
turtle.pendown()
turtle.write("李博文", font=("微软雅黑", 80, "bold"))
# 彭媛裕
turtle.penup()
turtle.goto(-400, -200)
turtle.pendown()
turtle.write("彭媛裕", font=("微软雅黑", 80, "bold"))
# 心形
turtle.speed(2)
turtle.penup()
turtle.goto(0, -200)
turtle.pendown()
turtle.pencolor("red")
turtle.fillcolor("red")
# 画心形
turtle.begin_fill()
for i in range(50):
turtle.forward(10)
turtle.right(10)
turtle.circle(40, 180)
turtle.circle(40, -180)
turtle.end_fill()
turtle.done()
def main():
turtle.setup(800, 600) # 设置窗口大小
turtle.shape("turtle")
turtle.color("green")
turtle.pensize(3)
# 绘制
draw_text()
draw_heart()
def draw_text():
global turtle
# 李博文
turtle.penup()
turtle.goto(-400, 200)
turtle.pendown()
turtle.write("李博文", font=("微软雅黑", 80, "bold"))
# 彭媛裕
turtle.penup()
turtle.goto(-400, -200)
turtle.pendown()
turtle.write("彭媛裕", font=("微软雅黑", 80, "bold"))
def draw_heart():
global turtle
# 心形
turtle.speed(2)
turtle.penup()
turtle.goto(0, -200)
turtle.pendown()
turtle.pencolor("red")
turtle.fillcolor("red")
# 画心形
turtle.begin_fill()
for i in range(50):
turtle.forward(10)
turtle.right(10)
turtle.circle(40, 180)
turtle.circle(40, -180)
turtle.end_fill()
if __name__ == "__main__":
main()
添加了main函数,并定义了draw_text和draw_heart两个函数分别绘制文字和心形。在main函数中调用这两个函数完成绘制。
添加__name__ == "main"判断,使得该脚本可以直接运行。
运行后会弹出绘图窗口,显示"李博文","彭媛裕"两个字符串和一个心形图案。
图片如下:
代码分三部分,先写"李博文"和"彭媛裕"两个字符串,然后画一个心形,最后调用turtle.done()显示图像窗口。
文字使用微软雅黑字体,心形通过for循环和arc画出。