admin管理员组文章数量:1794759
python爱心代码
在本文中,我们将用python编写几行代码来绘制心形并在其中编写文本。为了画一颗心,我们将使用一个名为“Turtle”的python库之一。
以下是用于绘图的函数:
代码:
import turtle as t pen = t.Turtle() t.bgcolor('#9966cc') t.delay(8) pen.color('#ffe4e1') pen.begin_fill() pen.left(40) pen.forward(120) pen.circle(80, 190) pen.right(100) pen.circle(80, 180) pen.forward(160) pen.left(90) pen.forward(50) pen.setpos(-60, 100) pen.end_fill() def txt(): pen.up() pen.setpos(-60, 100) pen.color('red') pen.write('I LoVe PyThON', font=("Comic Sans MS", 16)) txt() pen.end_fill() t.exitonclick()输出:
Turtle是Python中的一个内置模块。它提供:
要在屏幕上绘制一些东西,我们需要移动(笔)并移动,有一些功能,如 forward()、back() 等
代码:
# Import turtle package import 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() # Defining method to write text def txt(): # Move turtle to air pen.up() # Move turtle to a given position pen.setpos(-68, 95) # Move the turtle to the ground pen.down() # Set the text color to lightgreen pen.color('lightgreen') # Write the specified text in # specified font style and size pen.write("GeeksForGeeks", font=( "Verdana", 12, "bold")) # Draw a heart heart() # Write text txt() # To hide turtle pen.ht()输出
代码
import turtle turtle.pensize(3) def draw_heart_curve(): for i in range(200): turtle.right(1) turtle.forward(1) turtle.color("pink", "pink") turtle.begin_fill() turtle.left(140) turtle.forward(111.65) draw_heart_curve() turtle.left(120) draw_heart_curve() turtle.forward(111.65) turtle.end_fill() turtle.color('deep pink') style = ('Comic Sans MS', 30, 'italic') turtle.write('Happy Valentine\\'s Day', font=style, align='center') turtle.hideturtle() turtle.done()版权声明:本文标题:python爱心代码 内容由林淑君副主任自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.xiehuijuan.com/baike/1686864744a112003.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论