使用 Python 的 Arcade 模組建立雪花效果
我們都想為簡報或影片新增額外的效果。這些效果有助於我們更好地展示產品或提升使用者體驗。在本教程中,您將學習如何使用 Arcade 模組實現雪花效果。
您可以在遊戲中使用它來建立下雪或下雨的效果。您甚至可以將其設定為螢幕超時效果。
話不多說,讓我們開始吧!
開始
在本教程中,我們將使用 Arcade 模組,該模組幫助使用者輕鬆建立遊戲相關功能。
此模組並非 Python 預裝模組。這意味著我們將使用 pip 包管理器來安裝它。
為此,請使用以下命令。
pip3 install arcade
安裝 Arcade 模組後,我們將各種方法匯入到我們的指令碼中。
為此,我們在 Python 中使用 **import** 關鍵字後跟模組名稱。
import arcade
就是這樣!您現在可以開始著手進行任務了。
使用 Arcade 模組建立雪花效果
我們需要匯入 random 和 math 模組以及 Arcade 模組。您很快就會明白原因。
import random import math import arcade
現在,首先我們必須定義每個雪花。在這個指令碼中,每個雪花都是螢幕上的一個點。因此,我們將考慮其在輸出螢幕上的位置的 x 和 y 變數。
我們還定義了一個 reset_pos 函式,我們將使用它來定義雪花到達視窗末端後的位置。
class Snow: def __init__(self, height, width): self.x = 0 self.y = 0 def reset_pos(self, height, width): self.y = random.randrange(height, height + 100) self.x = random.randrange(width)
就是這樣。我們現在定義了一個雪花教程。現在,我們需要開始處理輸出視窗以及雪花教程下降的模式。
首先,我們定義使用 Arcade 模組繪製輸出螢幕的基本函式。
class MyGame(arcade.Window): def __init__(self, width, height): super().__init__(width, height) self.stream = None
現在,我們定義一個在啟動指令碼時執行的 start 函式。
此函式將負責描述這些教程的每一個的運動。它們下降的角度、下降的速度、教程的大小以及它相對於輸出視窗在螢幕上的位置。
這就是 random 匯入發揮作用的地方。我們使用 random 模組來調節這些教程的隨機生成。
def start(self, height, width): self.stream = [] for i in range(100): # Increase to increase number of snow ptutorials. snow = Snow(800, 600) snow.x = random.randrange(width) snow.y = random.randrange(height + 200) snow.size = random.randrange(2) snow.speed = random.randrange(20, 100) snow.angle = random.uniform(math.pi, math.pi * 2) self.stream.append(snow) # define bg colour arcade.set_background_color(arcade.color.BLACK)
現在,我們需要使用預設的 on_draw 函式來在輸出視窗上生成教程。
def on_draw(self): # This is a default function. arcade.start_render() for snow in self.stream: arcade.draw_circle_filled(snow.x, snow.y, snow.size, arcade.color.WHITE)
我們還需要定義 on_update 函式,該函式將定義教程在 y 方向上越過輸出視窗邊界限制後的位置。
def on_update(self, delta_time): # This is a default function. for snow in self.stream: snow.y = snow.y - snow.speed * delta_time if snow.y < 0: snow.reset_pos(800, 600) snow.x = snow.x + snow.speed * math.cos(snow.angle) * delta_time snow.angle = snow.angle + delta_time
就是這樣!您現在已經準備好完整的指令碼了!讓我們使用 main 函式並定義視窗的邊界並執行 Arcade 函式。
if __name__ == "__main__": window = MyGame(800, 600) window.start(800, 600) arcade.run()
它將產生以下 **輸出** −
完整指令碼
以下是完整程式碼 −
import random import math import arcade class Snow: def __init__(self, height, width): self.x = 0 self.y = 0 def reset_pos(self, height, width): self.y = random.randrange(height, height + 100) self.x = random.randrange(width) class MyGame(arcade.Window): def __init__(self, width, height): super().__init__(width, height) self.stream = None def start(self, height, width): self.stream = [] for i in range(100): snow = Snow(800, 600) snow.x = random.randrange(width) snow.y = random.randrange(height + 200) snow.size = random.randrange(2) snow.speed = random.randrange(20, 100) snow.angle = random.uniform(math.pi, math.pi * 2) self.stream.append(snow) arcade.set_background_color(arcade.color.BLACK) def on_draw(self): # This is a default function. arcade.start_render() for snow in self.stream: arcade.draw_circle_filled(snow.x, snow.y, snow.size, arcade.color.WHITE) def on_update(self, delta_time): # This is a default function. for snow in self.stream: snow.y = snow.y - snow.speed * delta_time if snow.y < 0: snow.reset_pos(800, 600) snow.x = snow.x + snow.speed * math.cos(snow.angle) * delta_time snow.angle = snow.angle + delta_time if __name__ == "__main__": window = MyGame(800, 600) window.start(800, 600) arcade.run()
結論
您現在知道如何使用 Python 中的 Arcade 模組在 GUI 輸出螢幕上建立雪花效果。您可以透過更改某些值來繼續試驗它。