在 Python 中建立 turtle 形狀

Preet Sanghavi 2022年5月18日
在 Python 中建立 turtle 形狀

本教程解釋瞭如何使用 turtle 庫並在 Python 中建立不同的形狀。我們首先匯入 turtle 庫。

在 Python 中使用 turtle.shape() 函式建立形狀

進口烏龜

import turtle

Turtle 庫需要安裝 Python 版本,支援 Tkinter 庫,因為它使用 Tk 作為底層圖形。

我們將在這裡使用 turtle.shape() 函式將 turtle 形狀設定為具有給定名稱的形狀。給定的形狀名稱必須存在於 Turtle Screen 的形狀庫中。

我們可以在圓形、方形、烏龜、箭頭、三角形和經典之間進行選擇。

現在讓我們開始使用 turtle.shape() 函式。

turtle.forward(70)

turtle.shape("circle")
turtle.right(60)
turtle.forward(100)

turtle.shape("arrow")
turtle.right(60)
turtle.forward(100)

在上面的程式碼中,我們首先建立了預設形狀。然後分別是圓形和箭頭形狀。

讓我們在下面看看上面程式碼的輸出。

在 Python 1 中建立 turtle 形狀

在 Python 2 中建立 turtle 形狀

我們已經使用上面的程式碼成功地建立了 turtle 形狀。因此,我們可以使用上述方法成功建立形狀。

Preet Sanghavi avatar Preet Sanghavi avatar

Preet writes his thoughts about programming in a simplified manner to help others learn better. With thorough research, his articles offer descriptive and easy to understand solutions.

LinkedIn GitHub

相關文章 - Python Turtle