如何用python画一个爱心
如何用python画一个爱心
回答:
使用 Python 标准库 turtle
模块。
代码来自 GeeksforGeeks:https://www.geeksforgeeks.org...
python"># Import turtle packageimport turtle
# Creating a turtle object(pen)
pen = turtle.Turtle()
# Defining a method to draw curve
def curve():
for i in range(200):
# Defining step by step curve motion
pen.right(1)
pen.forward(1)
# Defining method to draw a full heart
def heart():
# Set the fill color to red
pen.fillcolor('red')
# Start filling the color
pen.begin_fill()
# Draw the left line
pen.left(140)
pen.forward(113)
# Draw the left curve
curve()
pen.left(120)
# Draw the right curve
curve()
# Draw the right line
pen.forward(112)
# Ending the filling of the color
pen.end_fill()
# Draw a heart
heart()
# To hide turtle
pen.ht()
回答:
哈哈,既然楼上回答了一个,那我就拓展补充,我这里用c再来实现一个,方便遇到这个问题或者准备表白的朋友
#include <stdio.h>int main()
{
int i, j, k, l, m;
char c=3; //ASCII码里面 3 就是一个字符小爱心
for (i=1; i<=5; i++) printf("\n"); //开头空出5行
for (i=1; i<=3; i++) { //前3行中间有空隙分开来写
for (j=1; j<=32-2*i; j++) printf(" "); //左边的空格,每下一行左边的空格比上一行少2个 //8*n-2*i
for (k=1; k<=4*i+1; k++) printf("%c", c);//输出左半部分字符小爱心
for (l=1; l<=13-4*i; l++) printf(" "); //中间的空格,每下一行的空格比上一行少4个
for (m=1; m<=4*i+1; m++) printf("%c", c);//输出右半部分字符小爱心
printf("\n"); //每一行输出完毕换行
}
for (i=1; i<=3; i++) { //下3行中间没有空格
for (j=1; j<=24+1; j++) printf(" "); //左边的空格 //8*(n-1)+1
for (k=1; k<=29; k++) printf("%c", c);//输出字符小爱心
printf("\n"); //每一行输出完毕换行
}
for (i=7; i>=1; i--) { //下7行
for (j=1; j<=40-2*i; j++) printf(" "); //左边的空格,每下一行左边的空格比上一行少2个//8*(n+1)-2*i
for (k=1; k<=4*i-1; k++) printf("%c", c);//每下一行的字符小爱心比上一行少4个(这个循环是i--)
printf("\n"); //每一行输出完毕换行
}
for (i=1; i<=39; i++) printf(" "); //最后一行左边的空格
printf("%c\n", c); //最后一个字符小爱心
for (i=1; i<=5; i++) printf("\n"); //最后空出5行
return 0;
}
以上是 如何用python画一个爱心 的全部内容, 来源链接: utcz.com/p/938488.html