使用 Python 中的 Arcade 庫繪製三角形
Arcade 庫是 Python 的內建庫之一。它廣泛用於建立各種多媒體物件,可用於製作 2D 遊戲和其他需要圖形的應用程式。在本文中,我們將使用 Python 中的 Arcade 庫來完成建立三角形的任務。我們將從簡要介紹 Arcade 庫、其功能和用法開始。
什麼是 Arcade 庫?
Arcade 庫的開發是為了克服 Py.game 模組的限制,因為它是 Python 遊戲程式設計師使用的唯一模組。該庫能夠解決其建立的目的,即為 2D 遊戲開發更逼真的圖形。要使用該庫下的函式,首先需要匯入該庫,然後建立一個視窗,物件將顯示在其中。
對於繪製不同的形狀,Arcade 每個形狀都有一個不同的函式。
arcade.draw_circle_filled
arcade.draw_rectangle_filled
arcade.draw_polygon_filled
arcade.draw_line()
arcade.draw_point()
arcade.draw_triangle_filled()
arcade.draw_ellipse()
使用 Arcade 庫繪製三角形
示例
在以下示例中,我們使用 arcade 庫的 arcade.draw_triangle_filled() 函式繪製了一個三角形。
我們首先使用 'arcade.open_window()' 函式建立了一個視窗,並透過它傳遞了三個引數,分別是螢幕寬度、高度和標題。
接下來,我們設定了背景顏色並啟動了渲染過程。渲染啟動後,我們接下來定義了要繪製的三角形的座標,以及三角形的顏色。
import arcade SCR_WIDTH = 640 SCR_HEIGHT = 480 def draw_triangle(): arcade.open_window(SCR_WIDTH, SCR_HEIGHT, "Drawing a Triangle") arcade.set_background_color(arcade.color.ANTIQUE_WHITE) arcade.start_render() arcade.draw_triangle_filled( 320, 380, # x, y coordinate of point A 220, 180, # x, y coordinate of point B 420, 180, # x, y coordinate of point C arcade.color.ROYAL_BLUE ) arcade.finish_render() arcade.run() draw_triangle()
輸出
繪製空心三角形
類似地,您可以對要建立的物件進行不同的更改。在此示例中,我們沒有填充三角形,而是使用了 arcade.draw_triangle_outline() 函式。
此函式僅繪製三角形的輪廓。您可以向此程式碼新增圖形以使三角形更具吸引力。
還有一種繪製三角形的方法,即使用 arcade.ShapeElementList() 函式。讓我們使用此函式編寫一段程式碼。
注意:要繪製完美的物件,您必須牢記傳遞給函式的座標,因為座標在調整物件的位置方面起著重要作用。
示例
import arcade WIDTH = 640 HEIGHT = 480 TITLE = "Triangle Example" arcade.open_window(WIDTH, HEIGHT, TITLE) arcade.set_background_color(arcade.color.WHITE) arcade.start_render() # Create a ShapeElementList object triangle_list = arcade.ShapeElementList() # Add three line segments to the list to create a triangle triangle_list.append(arcade.create_line(320, 100, 200, 300, arcade.color.BLUE, 3)) triangle_list.append(arcade.create_line(200, 300, 440, 300, arcade.color.BLUE, 3)) triangle_list.append(arcade.create_line(440, 300, 320, 100, arcade.color.BLUE, 3)) # Draw the ShapeElementList object triangle_list.draw() arcade.finish_render() arcade.run()
輸出
示例
讓我們再舉一個例子,我們可以繪製兩個三角形。
import arcade WIDTH = 640 HEIGHT = 480 TITLE = "Two Triangles Example" arcade.open_window(WIDTH, HEIGHT, TITLE) arcade.set_background_color(arcade.color.WHITE) arcade.start_render() # Draw the first triangle arcade.draw_triangle_outline( 320, 100, # Vertex 1 (x, y) 200, 300, # Vertex 2 (x, y) 440, 300, # Vertex 3 (x, y) arcade.color.PURPLE ) # Draw the second triangle arcade.draw_triangle_outline( 200, 100, # Vertex 1 (x, y) 80, 300, # Vertex 2 (x, y) 320, 300, # Vertex 3 (x, y) arcade.color.VIVID_BURGUNDY ) arcade.finish_render() arcade.run()
輸出
使用 Arcade 庫繪製其他形狀
arcade.ShapeElementList 類基本上是一個類,它處理要在視窗上繪製的形狀元素列表。使用此類,您可以透過組合形狀來形成複雜物件,從而建立各種形狀。
要使用此類——
您需要建立該類的例項。
然後使用 append() 方法將形狀元素新增到列表中,並傳遞所需的形狀。
在前面關於形狀元素列表物件的示例中,我們使用了三條線段來建立三角形。
侷限性
但是 Arcade 庫也有一些侷限性。
它不具備支援 3D 圖形的特性,僅限於 2D 圖形。
許多行動網路和網路瀏覽器的平臺不支援它,因此在處理任何相關的專案時可能會成為問題。
因此,該庫在處理較小的專案和初學者學習時更有幫助。
結論
在本文中,我們簡要解釋了什麼是 Arcade 庫。我們討論了在編寫使用 Arcade 繪製三角形的程式碼時使用的函式。提供了三個不同的示例,以便更好地理解在編寫程式碼時應用的概念。
三種不同的方法包括三個不同的函式,分別是 arcade.draw_triangle_filled()、arcade.draw_triangle_outline 和 arcade.ShapeElementList()。每個函式都以不同的方式繪製三角形。