python3 pygame學習(3):pygame.Surface

上面我們說到pygame.image.load()返回的其實是一個surface對象,而pygame中專門有一個Surface類,並且這個類中還有許多的方法。


pygame.surface.blit 畫一個圖像到另一個
pygame.surface.convert 改變圖片的像素格式
pygame.surface.convert_alpha 改變圖像的每個像素的像素格式包括阿爾法
pygame.surface.copy 創建一個新的表面複製
pygame.surface.fill 表面用純色填充
pygame.surface.scroll 在地方的表面形象轉換
pygame.surface.set_colorkey 設置透明色鍵
pygame.surface.get_colorkey 獲取當前透明色鍵
pygame.surface.set_alpha 設置爲全表面圖像的alpha值
pygame.surface.get_alpha 獲取當前表面透明度值
pygame.surface.lock 像素訪問表面內存鎖
pygame.surface.unlock 從像素的訪問解鎖記憶
pygame.surface.mustlock 測試如果表面需要鎖定
pygame.surface.get_locked 測試如果表面電流鎖定
pygame.surface.get_locks 獲取表面的鎖
pygame.surface.get_at 在單個像素的顏色值的獲得
pygame.surface.set_at 設置爲一個像素的顏色值
pygame.surface.get_at_mapped 在一個單一的像素顏色值的映射
pygame.surface.get_palette 得到一個8位的表面顏色索引調色板
pygame.surface.get_palette_at 得到在調色板的顏色單一入口
pygame.surface.set_palette 對於一個8位的表面設置的調色板
pygame.surface.set_palette_at 設置在一個8位面單索引的顏色調色板
pygame.surface.map _ RGB 將一個顏色映射的顏色值
pygame.surface.unmap_rgb 將一個整數的顏色值映射成一個顏色
pygame.surface.set_clip 設置當前剪輯區域的表面
pygame.surface.get_clip 獲取當前剪輯區域的表面
pygame.surface.subsurface 創建一個新表,參考其母
pygame.surface.get_parent 找到一個地下的父母
pygame.surface.get_abs_parent 找到一個地下的頂級父
pygame.surface.get_offset 發現在父母的孩子地下的位置
pygame.surface.get_abs_offset 發現在其最高水平的孩子地下的絕對位置
pygame.surface.get_size 得到表面的尺寸
pygame.surface.get_width 得到表面的寬度
pygame.surface.get_height 得到表面高度
pygame.surface.get_rect 得到表面的矩形區域
pygame.surface.get_bitsize 得到表面的像素格式的位深度
pygame.surface.get_bytesize 習慣每面像素字節
pygame.surface.get_flags 用於表面附加標誌
pygame.surface.get_pitch 得到每面行的字節數
pygame.surface.get_masks 該掩碼需要顏色和映射的整數之間的轉換
pygame.surface.set_masks 組需要一種顏色和一個映射的整數之間的轉換的掩碼
pygame.surface.get_shifts 位的變化需要一種顏色和一個映射的整數之間的轉換
pygame.surface.set_shifts 設置位移所需顏色和映射的整數之間的轉換
pygame.surface.get_losses 有位用於顏色和映射的整數之間的轉換
pygame.surface.get_bounding_rect 找到最小的矩形包含數據
pygame.surface.get_view 返回一個表面的像素緩存視圖。
pygame.surface.get_buffer 獲取表面的像素緩衝區對象。
_pixels_address pygame。表面。 像素緩衝區地址


blit,convert,convert_alpah,這幾個比較有印象吧!

這裏我們着重介紹一下blit,fill。

我們在寫一個簡單的窗口程序。


創建一個600x400的窗口

screen=pygame.display.set_mode((600,400),0,32)


爲窗口填充顏色
screen.fill(color=(255,255,0))
fill填充顏色必須使用RGB顏色序列例如(255,0,0)是紅色,(255,255,0)是黃色等。


完整代碼

# -*- conding:utf-8 -*-
import pygame
import sys
pygame.init()
screen=pygame.display.set_mode((600,400),0,32)
while True:
	for event in pygame.event.get():
		if event.type == pygame.QUIT:
			sys.exit()
	screen.fill(color=(255,255,0))
	pygame.display.update()

運行效果


這裏着重講一下  for event in pygame.event.get()

這是一個實施事件循環,該循環會創建當前等待處理的事件的一個列表,然後使用for循環來遍歷裏面的事件。這樣,我們將會根據事件產生的順序依次地進行不同的操作。常見的事件是按鍵按下,按鍵釋放以及鼠標移動。通常需要最先處理QUIT事件(在用戶關閉窗口的時候會產生該事件。)

如果不加這個事件循環的話,那麼窗口程序運行會出現一點卡頓的現象(ps在我的電腦上!)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章