欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python使用turtle模塊繪制愛心圖案

 更新時間:2021年09月02日 11:29:08   作者:toMontain  
這篇文章主要為大家詳細介紹了Python使用turtle模塊繪制愛心圖案,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

程序員的浪漫,你懂嗎?

今天使用python小海龜實現(xiàn)愛心圖案的繪制,代碼如下:

import turtle
import time
 
 
# 清屏函數(shù)
def clear_all():
    turtle.penup()
    turtle.goto(0, 0)
    turtle.color('white')
    turtle.pensize(800)
    turtle.pendown()
    turtle.setheading(0)
    turtle.fd(300)
    turtle.bk(600)
 
 
# 重定位海龜?shù)奈恢?
def go_to(x, y, state):
    turtle.pendown() if state else turtle.penup()
    turtle.goto(x, y)
 
 
# 畫線
# state為真時海龜回到原點,為假時不回到原來的出發(fā)點
def draw_line(length, angle, state):
    turtle.pensize(1)
    turtle.pendown()
    turtle.setheading(angle)
    turtle.fd(length)
    turtle.bk(length) if state else turtle.penup()
    turtle.penup()
 
 
# 畫箭羽
def draw_feather(size):
    angle = 30                          # 箭的傾角
    feather_num = size//6               # 羽毛的數(shù)量
    feather_length = size // 3          # 羽毛的長度
    feather_gap = size//10              # 羽毛的間隔
    for i in range(feather_num):
        draw_line(feather_gap, angle+180, False)            # 箭柄,不折返
        draw_line(feather_length, angle + 145, True)        # 羽翼,要折返
    draw_line(feather_length, angle + 145, False)
    draw_line(feather_num*feather_gap, angle, False)
    draw_line(feather_length, angle + 145 + 180, False)
    for i in range(feather_num):
        draw_line(feather_gap, angle+180, False)            # 箭柄,不折返
        draw_line(feather_length, angle - 145, True)        # 羽翼,要折返
    draw_line(feather_length, angle - 145, False)
    draw_line(feather_num*feather_gap, angle, False)
    draw_line(feather_length, angle - 145 + 180, False)
 
 
# 畫愛心
def draw_heart(size):
    turtle.color('red', 'pink')
    turtle.pensize(2)
    turtle.pendown()
    turtle.setheading(150)
    turtle.begin_fill()
    turtle.fd(size)
    turtle.circle(size * -3.745, 45)
    turtle.circle(size * -1.431, 165)
    turtle.left(120)
    turtle.circle(size * -1.431, 165)
    turtle.circle(size * -3.745, 45)
    turtle.fd(size)
    turtle.end_fill()
 
 
# 畫箭
def draw_arrow(size):
    angle = 30
    turtle.color('black')
    draw_feather(size)
    turtle.pensize(4)
    turtle.setheading(angle)
    turtle.pendown()
    turtle.fd(size*2)
 
 
# 一箭穿心
# 箭的頭沒有畫出來,而是用海龜來代替
def arrow_heart(x, y, size):
    go_to(x, y, False)
    draw_heart(size*1.15)
    turtle.setheading(-150)
    turtle.penup()
    turtle.fd(size*2.2)
    draw_heart(size)
    turtle.penup()
    turtle.setheading(150)
    turtle.fd(size * 2.2)
    draw_arrow(size)
 
 
# 畫出發(fā)射愛心的小人
def draw_people(x, y):
    turtle.penup()
    turtle.goto(x, y)
    turtle.pendown()
    turtle.pensize(2)
    turtle.color('black')
    turtle.setheading(0)
    turtle.circle(60, 360)
    turtle.penup()
    turtle.setheading(90)
    turtle.fd(75)
    turtle.setheading(180)
    turtle.fd(20)
    turtle.pensize(4)
    turtle.pendown()
    turtle.circle(2, 360)
    turtle.setheading(0)
    turtle.penup()
    turtle.fd(40)
    turtle.pensize(4)
    turtle.pendown()
    turtle.circle(-2, 360)
    turtle.penup()
    turtle.goto(x, y)
    turtle.setheading(-90)
    turtle.pendown()
    turtle.fd(20)
    turtle.setheading(0)
    turtle.fd(35)
    turtle.setheading(60)
    turtle.fd(10)
    turtle.penup()
    turtle.goto(x, y)
    turtle.setheading(-90)
    turtle.pendown()
    turtle.fd(40)
    turtle.setheading(0)
    turtle.fd(35)
    turtle.setheading(-60)
    turtle.fd(10)
    turtle.penup()
    turtle.goto(x, y)
    turtle.setheading(-90)
    turtle.pendown()
    turtle.fd(60)
    turtle.setheading(-135)
    turtle.fd(60)
    turtle.bk(60)
    turtle.setheading(-45)
    turtle.fd(30)
    turtle.setheading(-135)
    turtle.fd(35)
    turtle.penup()
 
 
# 第一個畫面,顯示文字
def page0():
    turtle.penup()
    turtle.goto(-350, 0)
    turtle.color('black')
    turtle.write('專屬于我們的情人節(jié)', font=('宋體', 60, 'normal'))
    time.sleep(3)
 
 
# 第二個畫面,顯示發(fā)射愛心的小人
def page1():
    turtle.speed(10)
    draw_people(-250, 20)
    turtle.penup()
    turtle.goto(-150, -30)
    draw_heart(14)
    turtle.penup()
    turtle.goto(-20, -60)
    draw_heart(25)
    turtle.penup()
    turtle.goto(250, -100)
    draw_heart(45)
    turtle.hideturtle()
    time.sleep(3)
 
 
# 最后一個畫面,一箭穿心
def page2():
    turtle.speed(1)
    turtle.penup()
    turtle.goto(-200, -200)
    turtle.color('blue')
    turtle.pendown()
    turtle.write('ZBT       CJH', font=('wisdom', 50, 'normal'))
    turtle.penup()
    turtle.goto(0, -180)
    draw_heart(10)
    arrow_heart(20, -60, 51)
    turtle.showturtle()
 
 
def main():
    turtle.setup(900, 500)
    page0()
    clear_all()
    page1()
    clear_all()
    page2()
    turtle.done()
 
 
main()

運行截圖如下圖所示,其中的姓名縮寫可以自行修改。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python中可復(fù)用函數(shù)的6種實踐

    Python中可復(fù)用函數(shù)的6種實踐

    為了實現(xiàn)可維護性,我們的Python函數(shù)應(yīng)該:小型、只做一項任務(wù);沒有重復(fù);有一個層次的抽象性;有一個描述性的名字和有少于四個參數(shù),下面我們就來看看這6個特性的實踐吧
    2023-08-08
  • Python?argparse庫的基本使用步驟

    Python?argparse庫的基本使用步驟

    argparse庫是python下的一個命令行參數(shù)管理庫,支持int、str、float、bool、數(shù)組等5種基本數(shù)據(jù)類型,這篇文章主要介紹了Python?argparse庫的基本使用,需要的朋友可以參考下
    2022-07-07
  • python?Socket無限發(fā)送接收數(shù)據(jù)方式

    python?Socket無限發(fā)送接收數(shù)據(jù)方式

    這篇文章主要介紹了python?Socket無限發(fā)送接收數(shù)據(jù)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-06-06
  • Python tkinter padx參數(shù)詳解

    Python tkinter padx參數(shù)詳解

    這篇文章主要介紹了tkinter padx參數(shù),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下,希望能夠給你帶來幫助
    2021-10-10
  • Python嵌入C/C++進行開發(fā)詳解

    Python嵌入C/C++進行開發(fā)詳解

    在本篇文章里小編給大家分享了關(guān)于Python嵌入C/C++進行開發(fā)的相關(guān)知識點內(nèi)容,有興趣的朋友們可以參考下。
    2020-06-06
  • 使用Pytorch實現(xiàn)Swish激活函數(shù)的示例詳解

    使用Pytorch實現(xiàn)Swish激活函數(shù)的示例詳解

    激活函數(shù)是人工神經(jīng)網(wǎng)絡(luò)的基本組成部分,他們將非線性引入模型,使其能夠?qū)W習(xí)數(shù)據(jù)中的復(fù)雜關(guān)系,Swish 激活函數(shù)就是此類激活函數(shù)之一,在本文中,我們將深入研究 Swish 激活函數(shù),提供數(shù)學(xué)公式,探索其相對于 ReLU 的優(yōu)勢,并使用 PyTorch 演示其實現(xiàn)
    2023-11-11
  • 使用pth文件添加Python環(huán)境變量方式

    使用pth文件添加Python環(huán)境變量方式

    這篇文章主要介紹了使用pth文件添加Python環(huán)境變量方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-05-05
  • Python并查集Disjoint?Set的具體使用

    Python并查集Disjoint?Set的具體使用

    本文主要介紹了Python并查集Disjoint?Set的具體使用,包括并查集的基本概念、實現(xiàn)方式、路徑壓縮和應(yīng)用場景,并使用代碼示例演示并查集的操作,感興趣的可以了解一下
    2024-01-01
  • python訪問抓取網(wǎng)頁常用命令總結(jié)

    python訪問抓取網(wǎng)頁常用命令總結(jié)

    這篇文章主要介紹了python訪問抓取網(wǎng)頁常用命令的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • GPU狀態(tài)監(jiān)測?nvidia-smi?命令的用法詳解

    GPU狀態(tài)監(jiān)測?nvidia-smi?命令的用法詳解

    這篇文章主要介紹了GPU狀態(tài)監(jiān)測?nvidia-smi?命令的用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-11-11

最新評論