1. 程式人生 > >python繪製心形影象

python繪製心形影象

一、列印心形

print('\n'.join([''.join([('Lovezhuzhu'[(x-y)%8]if((x*0.05)**2+(y*0.1)**2-1)**3-(x*0.05)**2*(y*0.1)**3<=0 else' ')for x in range(-30,30)])for y in range(15,-15,-1)]))

'Lovezhuzhu':可以換成想換的字。

二、簡易動態心形

from turtle import *
def curvemove():
    for i in range(200):
        right(1)
        forward(1)
color('red','pink')        
begin_fill()
left(140)
forward(111.65)
curvemove()
left(120)
curvemove()
forward(111.65)
end_fill()
done()

三、動態心形

import turtle
import datetime
 
def love():
    def func(x, y):
        main()
    turtle.title('豬豬')
    lv=turtle.Turtle()
    lv.hideturtle()
    lv.getscreen().bgcolor('white')
    lv.color('yellow','red')
    lv.pensize(1)
    lv.speed(1)
    lv.up()
    lv.goto(0,-150)
    #開始畫愛心
    lv.down()
    lv.begin_fill()
    lv.goto(0, -150)
    lv.goto(-175.12, -8.59)
    lv.left(140)
    pos = []
    for i in range(19):
        lv.right(10)
        lv.forward(20)
        pos.append((-lv.pos()[0], lv.pos()[1]))
    for item in pos[::-1]:
        lv.goto(item)
    lv.goto(175.12, -8.59)
    lv.goto(0, -150)
    lv.left(50)
    lv.end_fill()
    #寫字
    lv.up()
    lv.goto(0, 80)
    lv.down()
    lv.write("小豬豬",font=(u"方正舒體",36,"normal"),align="center")
    lv.up()
    lv.goto(0, 0)
    lv.down()
    lv.write("早安快樂",font=(u"方正舒體",48,"normal"),align="center")
    lv.up()
    lv.goto(100, -210)
    lv.down()
    lv.write("麼麼噠",font=(u"華文琥珀",26,"bold"),align="right")
    lv.up()
    lv.goto(160, -190)
    lv.resizemode('user')
    lv.shapesize(4, 4, 10)
    lv.color('red', 'red')
    lv.onclick(func)
    lv.showturtle()
 
 
def main():
    pass
 
if __name__ == '__main__':
    if datetime.date.today() == datetime.date(2018, 12, 3): #YYYY年,MM月,DD日
        love()
    else:
        main()