使用 Python3 中的 Arcade 繪製圓形


Arcade 是一個 Python 庫,用於建立 2D 遊戲和應用程式。它是一個易於使用的庫,提供各種功能來建立介面,以便在螢幕上繪製形狀和影像。在本文中,我們將使用 Arcade 並用 Python3 繪製一個圓形。

安裝 Arcade

在開始繪製圓形之前,我們需要安裝 Arcade 庫。您可以使用 pip(Python 的包管理器)來安裝它:

Pip install arcade

在 Arcade 中繪製圓形

方法 1:使用 arcade.draw_circle() 方法

我們可以使用 Arcade 模組的 draw_circle 方法在螢幕上繪製圓形。下面演算法解釋了繪製圓形的步驟。

語法

arcade.draw_circle_filled(x, y, radius, color)

傳遞給 arcade.draw_circle 函式的引數如下:

  • x - 圓形中心點的 x 座標。

  • y - 圓形中心點的 y 座標。

  • radius - 圓形的半徑。

  • color - 圓形的顏色,指定為 arcade.color 常量、RGB 值元組或 RGBA 值元組。例如,您可以使用 arcade.color.RED 指定紅色圓形,或使用 (255, 0, 0) 使用 RGB 值指定相同的顏色。

演算法

  • 匯入 arcade 庫。

  • 設定視窗寬度和高度。

  • 使用 open_window 函式建立一個視窗,傳入視窗的寬度、高度和標題。

  • 使用 set_background_color 函式指定視窗的背景顏色。在本例中,我們將其設定為白色。

  • 使用 start_render 函式開始渲染過程。

  • 定義我們要繪製的圓形的中心點、半徑和顏色。

  • 使用 draw_circle_filled 函式繪製圓形,傳入中心點、半徑和顏色。

  • 使用 finish_render 函式完成渲染過程。

  • 使用 run 函式啟動事件迴圈,該函式顯示視窗並等待使用者輸入。

示例

在下面的示例中,arcade 庫用於建立視窗並在視窗中心繪製一個紅色圓形。首先,視窗的寬度和高度分別設定為 640 和 480 畫素。背景顏色設定為白色,並開始渲染過程。然後使用 arcade.draw_circle_filled() 函式繪製一個紅色圓形,指定中心座標、半徑和顏色。最後,渲染過程完成,視窗顯示,直到使用者使用 arcade.run() 關閉它。

import arcade

# Set up the window
WIDTH = 640
HEIGHT = 480
window = arcade.open_window(WIDTH, HEIGHT, "Drawing a Circle")

# Set the background color
arcade.set_background_color(arcade.color.WHITE)

# Start the render process
arcade.start_render()

# Draw a red circle in the center of the screen
x = WIDTH / 2
y = HEIGHT / 2
radius = 100
color = arcade.color.RED
arcade.draw_circle_filled(x, y, radius, color)

# Finish the render process and display the window
arcade.finish_render()
arcade.run()

輸出

方法 2:使用 arcade.create_ellipse_filled() 方法

arcade.create_ellipse_filled() 函式可用於在螢幕上繪製填充的橢圓(可用於繪製圓形)。

語法

arcade.draw_ellipse_filled(x, y, width, height, color)

傳遞給 arcade.draw_ellipse_filled() 函式的引數如下:

  • x - 橢圓中心點的 x 座標。

  • y - 橢圓中心點的 y 座標。

  • width - 橢圓的寬度。

  • height - 橢圓的高度。

  • color - 橢圓的顏色,指定為 arcade.color 常量、RGB 值元組或 RGBA 值元組。

演算法

  • 使用 import arcade 語句匯入 Arcade 庫。

  • 透過建立 WIDTH 和 HEIGHT 常量來設定視窗的寬度和高度。

  • 使用 arcade.open_window() 函式建立一個新視窗,並將 WIDTH、HEIGHT 和視窗標題作為引數傳入。

  • 使用 arcade.set_background_color() 函式並傳入 Arcade 顏色常量來設定視窗的背景顏色。

  • 使用 arcade.start_render() 函式開始渲染過程。

  • 透過將 x 座標計算為 WIDTH / 2,將 y 座標計算為 HEIGHT / 2 來定義橢圓的中心點。

  • 將橢圓的寬度和高度定義為 width = 100 和 height = 100。

  • 將橢圓的顏色定義為 arcade.color.BLUE。

  • 使用 arcade.draw_ellipse_filled() 函式並傳入中心點、寬度、高度和顏色作為引數來繪製填充的橢圓。

  • 使用 arcade.finish_render() 函式結束渲染過程。

  • 使用 arcade.run() 函式啟動事件迴圈,該函式將使視窗保持開啟狀態,直到使用者關閉它。

示例

在下面的示例中,arcade 庫用於建立視窗並在視窗中心繪製一個藍色橢圓。首先,視窗的寬度和高度分別設定為 640 和 480 畫素。背景顏色設定為白色,並開始渲染過程。然後使用 arcade.draw_ellipse_filled() 函式繪製一個藍色橢圓,指定中心座標、寬度、高度和顏色。最後,渲染過程完成,視窗顯示,直到使用者使用 arcade.run() 關閉它。

import arcade

# Set up the window
WIDTH = 640
HEIGHT = 480
window = arcade.open_window(WIDTH, HEIGHT, "Drawing a Circle")

# Set the background color
arcade.set_background_color(arcade.color.WHITE)

# Start the render process
arcade.start_render()

# Draw a red circle using the create_ellipse_filled function
x = WIDTH / 2
y = HEIGHT / 2
width = 100
height = 100
color = arcade.color.BLUE
arcade.draw_ellipse_filled(x, y, width, height, color)

# Finish the render process and display the window
arcade.finish_render()
arcade.run()

輸出

結論

在本文中,我們討論瞭如何使用 Python 中的 arcade 庫建立圓形。我們需要建立一個視窗和背景顏色,並使用 draw_circle_filled 函式在螢幕上繪製圓形。Arcade 有助於在 Python 中建立 2D 遊戲、圓形和其他形狀。

更新於: 2023-07-10

133 次瀏覽

開啟您的 職業生涯

透過完成課程獲得認證

立即開始
廣告