1、創建Surface對象及打印rect屬性
import pygame from pygame.locals import * pygame.init() # 初始化pygame screen = pygame.display.set_mode((200, 200)) # 創建窗口 face = pygame.Surface((50, 50)) # 創建一個50x50的平面 face.fill(color='pink') # 填充橙色 rect = face.get_rect() # 獲取面積和X,Y坐標 print(f'top:{rect.top}') print(f'topleft:{rect.topleft}') print(f'topright:{rect.topright}') print(f'bottom:{rect.bottom}') print(f'bottomleft:{rect.bottomleft}') print(f'bottomright:{rect.bottomright}') print(f'left:{rect.left}') print(f'right:{rect.right}') print(f'midtop:{rect.midtop}') print(f'midbottom:{rect.midbottom}') print(f'midleft:{rect.midleft}') print(f'midright:{rect.midright}') print(f'center:{rect.center}') print(f'centerx:{rect.centerx}') print(f'centery:{rect.centery}') print(f'w:{rect.w}') print(f'width:{rect.width}') print(f'h:{rect.h}') print(f'height:{rect.height}') print(f'x:{rect.x}') print(f'y:{rect.y}') print(f'size:{rect.size}') running = True while running: for event in pygame.event.get(): # 獲取所有事件 if event.type == KEYDOWN: # 獲取按鈕按下事件 if event.key == K_ESCAPE: # 如果是按下了ESC鍵 running = False if event.type == QUIT: # 如果是QUIT事件,如點擊關閉窗口按鈕 running = False pygame.quit() # 退出pygame

2、顯示surface對象
import pygame from pygame.locals import * pygame.init() # 初始化pygame screen = pygame.display.set_mode((300, 300)) # 創建窗口 screen.fill(color='white') # 填充顏色 face = pygame.Surface((50, 50)) # 創建一個50x50的平面 face.fill(color='orange') # 填充顏色 running = True while running: for event in pygame.event.get(): # 獲取所有事件 if event.type == KEYDOWN: # 獲取按鈕按下事件 if event.key == K_ESCAPE: # 如果是按下了ESC鍵 running = False if event.type == QUIT: # 如果是QUIT事件,如點擊關閉窗口按鈕 running = False screen.blit(face, (100, 100)) # 放置平面face pygame.display.flip() # 刷新整個平面 或者 pygame.display.update() pygame.quit() # 退出pygame

