【Python】畫一個心形


 

 

#!/usr/bin/env python

# -*- coding:utf-8 -*- 

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()     # 顯示畫筆

    

# 初始化

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()       # 標記背景填充結束位置

  

# 點擊窗口關閉程序

window = turtle.Screen()

window.exitonclick()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM