Python有趣的小案例-美國隊長盾牌

# 導入 turtle 模塊

# 模塊是 python 自帶的工具箱,這里將工具箱導入就能使用了

# turtle 模塊是 python 用來畫圖的工具箱

import turtle


# 將 turtle 里的工具拿出來,賦給 t 變量

# 照貓畫虎用就是了,這些東西要到很后面才能理解

t =

turtle.Turtle()


# 這一行用來加快畫筆速度,從 1~9 依次變快,但 0 是最快

t.speed(0)


# 這是向前走,單位是像素

t.forward(100)

# 這是轉(zhuǎn)彎,單位是角度

t.right(120)

t.forward(100)

t.right(120)

t.forward(100)

t.right(120)

# 復(fù)制三次,就畫了一個三角形


# 正方形

# 長方形


# 如果我們需要改變?nèi)切蔚倪呴L怎么辦?

# 這就要用到變量了,到時候只需改變變量就能改變長度

# 如果有相同的變量,后面定義的會覆蓋前面的

l = 200

t.forward(l)

t.right(120)

t.forward(l)

t.right(120)

t.forward(l)

t.right(120)


# for 循環(huán)

# 循環(huán)還有 while 循環(huán),考慮到用不著就不講了

# 循環(huán)用來處理重復(fù)的事情


# range() 是一個區(qū)間

# range(3)相當于 0 1

2

# range(5)相當于 0 1

2 3 4


# i 取的是 range() 里的值,一次取一個,取一次就循環(huán)一次

# 冒號后面必有縮進,縮進的代表是同一個代碼塊

# 照著用就行了,注意一個字符都不能敲錯,不能用中文符號

for i in range(3):


?t.forward(l)


?t.right(120)



# 如果想畫兩個三角形怎么辦,再復(fù)制一個 for 循環(huán)?

# 我們用函數(shù)將代碼封裝起來,到時候直接調(diào)用就好了

# def 關(guān)鍵字用來定義函數(shù), triangle 是函數(shù)名

# 必須要有冒號接縮進,函數(shù)里面也是一個代碼塊

def triangle():


?for i in range(3):


? ? ?t.forward(l)


? ? ?t.right(120)



# 函數(shù)的調(diào)用

#

triangle()



# 函數(shù)可以傳遞參數(shù)進去

def triangle2(l):


?for i in range(3):


? ? ?t.forward(l)


? ? ?t.right(120)



# 需要傳遞個參數(shù)進去才能調(diào)用這個函數(shù)

#

triangle2(250)


# 定一個函數(shù)畫長方形


# 四則運算

# ? +

? 加

# ? -

? 減

# ? *

? 乘

# ? /

? 除

# ?

// ?整除

# ? %

? 取余


# 寫一個畫 n 邊形的通用函數(shù)

def polygon(l, n):


?angle = 360 / n


?for i in range(n):


? ? ?t.forward(l)


? ? ?t.right(angle)



#

polygon(100, 6)



# 畫一個五角星

def five_star(l):


?for i in range(5):


? ? ?t.forward(l)


? ? ?t.right(144)



#

five_star(100)



# 畫一個圓

# 邊長在 36 以上就是個圓

def circle():


?for i in range(36):


? ? ?t.forward(10)


? ? ?t.right(15)



# circle()



# 在指定的坐標畫圖

# 比如要在坐標為 (100, 150) 的位置畫個正方形

def square(x, y, l):


?t.penup()


?t.goto(x, y)


?t.pendown()


?for i in range(4):


? ? ?t.forward(l)


? ? ?t.right(90)



#

square(100, 150, 100)


# 將畫筆定位封裝成函數(shù)使用,就能有效去除重復(fù)代碼

def setpen(x, y):


?t.penup()


?t.goto(x, y)


?t.pendown()


?t.setheading(0)



def square(x, y, l):


?setpen(x, y)


?for i in range(4):


? ? ?t.forward(l)


? ? ?t.right(90)



#

square(100, 150, 100)



# 畫一排正方形,共五個,間隔 10

# 蠢方法

#

square(100, 150, 30)

#

square(140, 150, 30)

#

square(180, 150, 30)

#

square(220, 150, 30)

#

square(260, 150, 30)



# 使用 for 循環(huán)、函數(shù)


def square_line(x, y, l, n,

dis):


?for i in range(n):


? ? ?inner_x = x + (l + dis) * i


? ? ?square(inner_x, y, l)



#

square_line(100, 150, 30, 6, 10)



# 畫一個正方形方陣

def square_matrix(x, y, l,

n, dis, m):


?for i in range(m):


? ? ?inner_y = y - (l + dis) * i


? ? ?square_line(x, inner_y, l, n, dis)



#

square_matrix(100, 150, 30, 5, 10, 6)



# 填充顏色,給圖形上色

def five_star(l):


?t.fillcolor('yello')


?t.begin_fill()


?for i in range(5):


? ? ?t.forward(l)


? ? ?t.right(144)


?t.end_fill()


#

five_star(100)


# 字典的簡單用法


# 抽象畫

# for i in

range(500):

# ?

? t.forward(i)

# ?

? t.left(90)


# for i in

range(500):

# ?

? t.forward(i)

# ?

? t.left(91)


colors = ['red', 'yellow', 'blue', 'green']



# for i in

range(500):

# ?

? t.pencolor(colors[i % 4])

# ?

? t.circle(i)

# ?

? t.left(91)


# sides =

5

# colors =

['red', 'yellow', 'blue', 'orange', 'green', 'purple']

# for i in

range(360):

# ?

? t.pencolor(colors[i % sides])

# ?

? t.forward(i * 3 / sides + i)

# ?

? t.left(360 / sides + 1)

# ?

? t.width(i * sides / 200)


# 美隊盾牌

def circle(x, y, r, color):


?n = 36


?angle = 360 / n


?pi = 3.1415926


?c = 2 * pi * r


?l = c / n


?start_x = x - l / 2


?start_y = y + r


?setpen(start_x, start_y)


?t.pencolor(color)


?t.fillcolor(color)


?t.begin_fill()


?for i in range(n):


? ? ?t.forward(l)


? ? ?t.right(angle)


?t.end_fill()



def five_star(l):


?setpen(0, 0)


?t.setheading(162)


?t.forward(150)


?t.setheading(0)


?t.fillcolor('WhiteSmoke')


?t.begin_fill()


?t.hideturtle()


?t.penup()


?for i in range(5):


? ? ?t.forward(l)


? ? ?t.right(144)


?t.end_fill()



def sheild():


?circle(0, 0, 300, 'red')


?circle(0, 0, 250, 'white')


?circle(0, 0, 200, 'red')


?circle(0, 0, 150, 'blue')


?five_star(284)



sheild()


# 結(jié)尾這一行必須有,照著用就行了

turtle.done()

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,847評論 0 10
  • The Inner Game of Tennis W Timothy Gallwey Jonathan Cape ...
    網(wǎng)事_79a3閱讀 12,910評論 3 20
  • 昨晚不小心打爛了媽媽心愛的杯子,她嘟囔了好一陣子,雖然是“碎碎平安”,畢竟覺得過意不去,今天去商場買了新杯子...
    風來自友方閱讀 297評論 0 0
  • 《如何改變習慣》 ?習慣原理。 就是在沒有路的地方踏出一條路。建立習慣最初的30天。 1.起跑(1到3天)。動力足...
    可可兒媽閱讀 254評論 0 1
  • 文/月明紅紅 世間最詩意的地方 不在深山也不在鬧市 深山之中難免清寂無人煙 鬧市里煙火世俗又太深 不喜歡不流連 最...
    月明紅紅閱讀 1,035評論 49 96

友情鏈接更多精彩內(nèi)容