阿爾法python 第二章 Python基本圖形繪製

Python基本圖形繪製

蟒蛇繪製

使用turtle庫,繪製一個蟒蛇形狀的圖形。‬
在這裏插入圖片描述

#PythonDraw.Py
import turtle
turtle.setup(650,350,200,200)
turtle.penup()
turtle.fd(-250)
turtle.pendown()
turtle.pensize(25)
turtle.pencolor("purple")
turtle.seth(-40)
for i in range(4):
    turtle.circle(40,80)
    turtle.circle(-40,80)
turtle.circle(40,80/2)
turtle.fd(40)
turtle.circle(16,180)
turtle.fd(40*2/3)
turtle.done()


正方形繪製

通過之前 alpha 繪圖的效果我們瞭解到:
alpha.color(‘red’)是讓 alpha 的顏色變成 紅 色;
alpha.forward(100)是讓 alpha 前進 100 個單位;
alpha.left(90)是讓 alpha 左轉 90 度;
現在使用這些代碼讓 alpha 畫出一個完整的正方形吧。

import turtle

alpha = turtle.Turtle()
alpha.color('red')
alpha.forward(100)
alpha.left(90)

# 完善代碼,畫一個正方形。
alpha.forward(100)
alpha.left(90)
alpha.forward(100)
alpha.left(90)
alpha.forward(100)
alpha.left(90)


在這裏插入圖片描述


八邊形

使用turtle庫,繪製一個八邊形。‬‬

import turtle as t
t.pensize(2)
for i in range(8):
    t.fd(200)
    t.left(45)

在這裏插入圖片描述


八角形

使用turtle庫,繪製一個八角圖形。‬

import turtle as t
t.pensize(2)
for i in range(8):
    t.fd(150)
    t.left(135)

在這裏插入圖片描述


疊邊形繪製

使用turtle庫,繪製一個疊邊形,其中,疊邊形內角爲100度。‬

import turtle as t
t.pensize(2)
for i in range(9):
    t.fd(150)
    t.left(80)

在這裏插入圖片描述


使用循環繪圖

循環能將大量重複的代碼,縮減到只用幾行代碼,就能得到與之前相同的效果。
現在使用循環來簡化右側畫五角星的代碼,圖形如下所示:
在這裏插入圖片描述

import turtle as t
t.pensize(2)
for i in range(5):
    t.fd(150)
    t.right(720/5)

風輪繪製

使用turtle庫,繪製一個風輪效果,其中,每個風輪內角爲45度,風輪邊長150像素。‬
在這裏插入圖片描述

import turtle as t
t.pensize(2)
for i in range(4):
    t.fd(150)
    t.right(90)
    t.circle(-150,45)
    t.right(90)
    t.fd(150)
    t.left(135)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章