Python基本圖形繪制實例


一、使用turtle庫,繪制一個如下圖所示正方形。

import turtle
turtle.pendown()
turtle.pensize(5)
for i in range(4):
    turtle.seth(90*i)
    turtle.forward(200)

二、使用turtle庫,繪制一個如下圖所示六邊形。

import turtle
turtle.pendown()
turtle.pensize(3)
turtle.forward(100)
turtle.seth(45)
turtle.forward(100)
turtle.seth(135)
turtle.forward(100)
turtle.seth(180)
turtle.forward(100)
turtle.seth(225)
turtle.forward(100)
turtle.seth(315)
turtle.forward(100)

三、使用turtle庫進行創意繪畫,主題不限制,代碼行數100-200行左右。

import turtle as t
t.pendown()
t.pensize(3)
for i in range(4):
    t.seth(90*i)
    if (i%2==0):
        t.fd(160)
    else:
        t.fd(90)
t.penup()
t.seth(225)
t.fd(13)
t.pendown()
for i in range(4):
    t.seth(90*i)
    if (i%2==0):
        t.fd(180)
    else:
        t.fd(110)
t.seth(0)
t.fd(80)
t.seth(270)
t.fd(40)
t.seth(0)
t.fd(20)
t.seth(90)
t.fd(40)
t.penup()
t.fd(40)
t.seth(180)
t.fd(25)
t.pendown()
t.pencolor("blue")
for j in range(15):
    for i in range(4):
        t.seth(90*i)
        t.fd(j)
t.seth(0)
t.fd(17)
t.pencolor("yellow")
for j in range(15):
    for i in range(4):
        t.seth(90*i)
        t.fd(j)
t.seth(90)
t.fd(17)
t.pencolor("green")
for j in range(15):
    for i in range(4):
        t.seth(90*i)
        t.fd(j)
t.seth(180)
t.fd(17)
t.pencolor("red")
for j in range(15):
    for i in range(4):
        t.seth(90*i)
        t.fd(j)
t.penup()
t.fd(85)
t.pendown()
t.pencolor("black")
t.seth(180)
t.fd(10)
t.seth(270)
t.fd(20)
t.seth(0)
t.fd(50)
t.seth(90)
t.fd(20)
t.seth(180)
t.fd(15)
t.penup()
t.fd(20)
t.pendown()
t.fd(5)
t.penup()
t.seth(270)
t.fd(30)
t.seth(180)
t.fd(40)
t.seth(270)
t.pendown()
t.fd(30)
t.seth(0)
t.fd(110)
t.seth(90)
t.fd(30)
t.seth(180)
t.fd(110)

 


免責聲明!

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



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