如何在Pygame中使用滑鼠縮放和旋轉影像?


Pygame是一個強大的庫,用於在Python中建立2D遊戲和圖形應用程式。它提供了廣泛的功能,包括操作和轉換影像的能力。在本文中,我們將探討如何使用滑鼠在Pygame中縮放和旋轉影像。

先決條件

在瞭解縮放和旋轉影像的過程之前,務必瞭解Pygame及其事件處理機制的基礎知識。另外,請確保已在您的Python環境中安裝Pygame。您可以使用以下命令使用pip安裝它:

pip install pygame

設定Pygame視窗

首先,讓我們建立一個Pygame視窗來顯示我們的影像並處理滑鼠事件。我們可以從匯入Pygame模組開始:

import pygame
import sys

初始化Pygame並建立一個具有特定寬度和高度的視窗:

我們將寬度和高度變數分別設定為800和600,但您可以根據需要調整這些值。現在,我們需要處理主遊戲迴圈,該迴圈將持續更新視窗:

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

遊戲迴圈使視窗保持開啟狀態,直到使用者單擊關閉按鈕將其關閉。如果使用者單擊關閉按鈕,我們將透過呼叫pygame.quit()和sys.exit()來退出程式。

載入和顯示影像

要載入和顯示影像,我們需要將其匯入到我們的Pygame程式中。將影像檔案放在與Python指令碼相同的目錄中。在本例中,我們假設影像檔名為“image.png”。我們可以使用以下程式碼載入和顯示影像:

pygame.image.load()函式載入影像檔案,而image.get_rect()返回一個矩形物件,表示影像的尺寸和位置。我們使用screen.blit()函式將影像繪製到螢幕表面上。最後,pygame.display.flip()更新顯示以顯示影像。

image = pygame.image.load("image.png")
image_rect = image.get_rect()
screen.blit(image, image_rect)
pygame.display.flip()

縮放影像

現在顯示影像後,讓我們繼續使用滑鼠縮放它。我們將使用滑鼠滾輪來控制縮放因子。

示例

在下面的示例中,我們使用scale_factor變數來跟蹤影像的縮放比例。當滑鼠滾輪向上滾動(event.button == 4)時,我們將scale_factor增加0.1。相反,當滾輪向下滾動(event.button == 5)時,我們將scale_factor減少0.1。

然後,我們使用pygame.transform.scale()函式根據scale_factor調整原始影像的大小。縮放影像的寬度和高度是透過將原始尺寸乘以scale_factor計算得出的。

scaled_image_rect被更新以確保影像在縮放後保持居中。我們使用screen.fill((0, 0, 0))清除螢幕以刪除之前的影像,然後使用screen.blit()繪製縮放後的影像。

import pygame
import sys

# Initialize Pygame
pygame.init()

# Set up the Pygame window
width, height = 800, 600
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Image Scaling Demo")

# Load and display the image
image = pygame.image.load("image.png")
image_rect = image.get_rect()
screen.blit(image, image_rect)
pygame.display.flip()

# Set up scaling variables
scale_factor = 1.0

# Game loop
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 4:  # Scrolling up
                scale_factor += 0.1
            elif event.button == 5:  # Scrolling down
                scale_factor -= 0.1

    scaled_image = pygame.transform.scale(
        image, (int(image_rect.width * scale_factor), int(image_rect.height * scale_factor))
    )
    scaled_image_rect = scaled_image.get_rect(center=image_rect.center)

    screen.fill((0, 0, 0))  # Clear the screen
    screen.blit(scaled_image, scaled_image_rect)
    pygame.display.flip()

輸出

注意:執行此程式碼時,它將在Pygame視窗中顯示影像。您可以使用滑鼠滾輪向上和向下縮放影像。向上滾動將比例增加0.1,向下滾動將比例減少0.1。縮放後的影像將持續更新並在Pygame視窗中顯示。

旋轉影像

現在,讓我們使用滑鼠實現旋轉功能。我們將使用滑鼠移動來控制旋轉角度。

示例

在下面的示例中,我們使用rotation_angle變數來跟蹤影像的旋轉。當滑鼠在按下左鍵時移動(event.type == pygame.MOUSEMOTION and pygame.mouse.get_pressed()[0]),我們將根據相對的x座標移動(event.rel[0])調整rotation_angle。將其除以10可以提供更流暢的旋轉體驗。

pygame.transform.rotate()函式用於根據rotation_angle旋轉縮放後的影像。與縮放類似,我們更新rotated_image_rect以使影像在旋轉後保持居中。

import pygame
import sys

# Initialize Pygame
pygame.init()

# Set up the Pygame window
width, height = 800, 600
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Image Rotation Demo")

# Load and display the image
image = pygame.image.load("image.png")
image_rect = image.get_rect()
screen.blit(image, image_rect)
pygame.display.flip()

# Set up rotation variables
rotation_angle = 0.0

# Game loop
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 4:  # Scrolling up
                rotation_angle += 10
            elif event.button == 5:  # Scrolling down
                rotation_angle -= 10

    rotated_image = pygame.transform.rotate(image, rotation_angle)
    rotated_image_rect = rotated_image.get_rect(center=image_rect.center)

    screen.fill((0, 0, 0))  # Clear the screen
    screen.blit(rotated_image, rotated_image_rect)
    pygame.display.flip()

輸出

結論

在本文中,我們討論瞭如何在Pygame中使用滑鼠縮放和旋轉影像。透過使用滑鼠滾輪,我們可以動態調整縮放因子並即時檢視影像變換。類似地,透過跟蹤滑鼠移動和點選,我們可以輕鬆旋轉影像,提供互動性和引人入勝的使用者體驗。

更新於:2023年10月13日

379 次瀏覽

啟動您的職業生涯

透過完成課程獲得認證

開始
廣告