如何建立一個空的Pygame視窗?
Pygame
Pygame是一個用於開發遊戲和多媒體應用程式的Python庫。它提供處理使用者輸入、圖形、聲音和其他多媒體相關任務的功能。
Pygame構建在SDL (Simple DirectMedia Layer)庫之上,提供對音訊、鍵盤、滑鼠、操縱桿和圖形硬體的低階訪問。Pygame為SDL提供了一個高階介面,使Python開發人員更容易使用。
使用Pygame,開發人員可以建立2D遊戲、街機風格的遊戲、教育遊戲、模擬和其他多媒體應用程式。Pygame提供了一套用於處理圖形、音訊、輸入和其他遊戲相關任務的模組。它還包括用於處理影像和聲音檔案以及構建遊戲選單和介面的工具。
Pygame是開源的,並在LGPL (Lesser General Public License)或GPL (General Public License)下提供。可以使用pip(Python包管理器)安裝它,也可以直接從Pygame網站下載。
安裝Pygame
要安裝Pygame,您可以按照以下步驟操作:
步驟1 - 確保您的系統上已安裝Python。Pygame適用於Python 3.5及更高版本。您可以從官方網站下載Python(https://python.club.tw/downloads/)。
步驟1 - 開啟終端/命令提示符視窗。
步驟2 - 使用pip安裝Pygame,輸入以下命令並按Enter鍵:
pip install pygame
步驟3 - 等待安裝完成。根據您的網路速度,這可能需要幾分鐘。
步驟4 - 在程式碼中匯入pygame進行測試。如果您沒有看到任何錯誤訊息,則表示Pygame已成功安裝在您的系統上。
注意 - 根據您的作業系統,您可能需要安裝其他庫或軟體包才能使Pygame正常工作。有關係統特定要求的更多資訊,請檢視Pygame文件。
Pygame視窗
語法
使用者可以按照以下語法建立一個空的Pygame視窗。
import pygame pygame.init() size = (800, 600) # Width, Height screen = pygame.display.set_mode(size) pygame.display.set_caption("My Pygame Window")
要建立一個空的Pygame視窗,您可以按照以下步驟操作:
匯入pygame後,透過呼叫pygame.init()函式對其進行初始化。此函式初始化所有Pygame模組,應在任何其他Pygame函式之前呼叫。
透過建立一個包含寬度和高度(以畫素為單位)的元組來設定視窗的大小。
透過呼叫pygame.display.set_mode()函式建立Pygame視窗,並將大小元組作為引數傳入。此函式將建立一個具有指定大小的新Pygame視窗。
透過呼叫pygame.display.set_caption()函式設定視窗的標題。此函式將視窗的標題設定為指定的字串。
遊戲迴圈:最後,您需要設定一個遊戲迴圈來保持視窗開啟。
遊戲迴圈應包含以下程式碼。此迴圈無限執行,直到使用者單擊關閉按鈕或按Alt+F4關閉視窗。
執行此程式碼後,您應該會看到一個標題為“我的Pygame視窗”的空Pygame視窗。
示例1
此程式建立一個Pygame視窗,幷包含一個主遊戲迴圈,該迴圈使用Pygame的內建繪圖函式處理事件、繪製圓形、矩形和線條,並更新視窗。您可以修改pygame.draw.circle()、pygame.draw.rect()和pygame.draw.line()函式的引數來更改形狀的位置、大小、顏色和粗細。
import pygame pygame.init() window_size = (800, 600) window = pygame.display.set_mode(window_size) pygame.display.set_caption("My Pygame Window") running = True while running: # Handle events for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # Draw shapes window.fill((255, 255, 255)) pygame.draw.circle(window, (255, 0, 0), (150, 200), 50) pygame.draw.rect(window, (0, 200, 0), (100, 300, 300, 200)) pygame.draw.line(window, (0, 0, 100), (100, 100), (700, 500), 5) pygame.display.flip() pygame.quit()
輸出
示例2
此程式建立一個Pygame視窗,並新增一個按鈕,該按鈕具有白色矩形形狀、黑色文字標籤和懸停效果,當滑鼠游標位於按鈕區域上方時,會更改按鈕顏色。該程式還處理滑鼠事件,以檢測何時單擊按鈕並將訊息列印到控制檯。您可以修改button_rect、button_font和button_text物件的屬性來更改按鈕的位置、大小、顏色、字型和標籤。此外,它還顯示一個時鐘,顯示自程式啟動以來經過的秒數。
該程式還使用Pygame的Clock物件將幀速率限制為60 FPS。您可以透過更改clock_font和clock_text_rect物件的屬性來修改時鐘文字的字型大小、顏色和位置。
import pygame pygame.init() window_size = (800, 600) window = pygame.display.set_mode(window_size) pygame.display.set_caption("My Pygame Window") # Define button properties button_color = (255, 255, 255) button_hover_color = (200, 200, 200) button_rect = pygame.Rect(300, 250, 200, 100) button_font = pygame.font.SysFont(None, 36) button_text = button_font.render("Click me!", True, (0, 0, 0)) button_text_rect = button_text.get_rect(center=button_rect.center) # Initialize Pygame clock clock = pygame.time.Clock() running = True while running: # Limit the frame rate to 60 FPS clock.tick(60) # Handle events for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # Handle mouse events if event.type == pygame.MOUSEBUTTONDOWN: mouse_pos = pygame.mouse.get_pos() if button_rect.collidepoint(mouse_pos): print("Button clicked!") if event.type == pygame.MOUSEMOTION: mouse_pos = pygame.mouse.get_pos() if button_rect.collidepoint(mouse_pos): button_color = button_hover_color else: button_color = (255, 255, 255) # Draw button and clock window.fill((128, 128, 128)) pygame.draw.rect(window, button_color, button_rect) window.blit(button_text, button_text_rect) current_time = pygame.time.get_ticks() clock_text = button_font.render(f"Time: {current_time/1000:.2f} s", True, (255, 255, 255)) clock_text_rect = clock_text.get_rect(topright=(780, 10)) window.blit(clock_text, clock_text_rect) pygame.display.flip() pygame.quit()
輸出
結論
在本教程中,我們學習了Pygame,如何安裝Pygame以及如何使用程式碼建立空的Pygame視窗。我們在示例2中學習瞭如何繪製形狀,
還在示例3中學習瞭如何新增按鈕和時鐘。