溫馨提示×

python中怎么用turtle畫正方形

小億
243
2023-12-26 13:35:41
欄目: 編程語言

你可以使用turtle庫中的函數(shù)來畫一個正方形。以下是一個例子:

import turtle

def draw_square():
    turtle.forward(100)  # 向前移動100個像素
    turtle.right(90)  # 右轉(zhuǎn)90度
    turtle.forward(100)
    turtle.right(90)
    turtle.forward(100)
    turtle.right(90)
    turtle.forward(100)

draw_square()
turtle.done()

在這個例子中,我們定義了一個名為draw_square()的函數(shù),用于畫一個正方形。我們使用turtle.forward()函數(shù)向前移動一定距離,使用turtle.right()函數(shù)右轉(zhuǎn)一定角度,從而畫出一個正方形。最后,我們調(diào)用draw_square()函數(shù)來執(zhí)行畫正方形的操作,并使用turtle.done()函數(shù)來保持程序運行,直到用戶關(guān)閉圖形窗口。

0