Python - 用 PyGame 顯示影像
Pygame 是一款適用於 Python 的多媒體庫,用於製作遊戲和多媒體應用程式。在本文中,我們將介紹如何使用 pygame 模組在螢幕上繪製一張圖片,同時考慮影像在 pygame 視窗中的高度、寬度和位置。
在下面的程式中,我們初始化 pygame 模組,然後定義影像的模式和標題。接下來,我們載入影像並定義座標。screen.blit 函式繪製螢幕,而 while 迴圈會不斷監聽遊戲是否已結束。
示例
import pygame pygame.init() w = 300; h = 300 screen = pygame.display.set_mode((w, h)) pygame.display.set_caption('Tutorialspoint Logo') TPImage = pygame.image.load("E:\Python3\tutorialspoint.png").convert() # coordinates of the image x = 10; y = 20; screen.blit(TPImage, (x, y)) # paint screen one time pygame.display.flip() running = True while (running): # loop listening for end of game for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # loop over, quite pygame pygame.quit()
輸出
執行以上程式碼,我們得到以下結果 −
廣告