pygame.mouse函數
pygame.mouse.get_pressed() —— 獲取鼠標按鍵的情況(是否被按下)
import pygame pygame.init() screen = pygame.display.set_mode((196, 100)) pygame.display.set_caption("pygame.mouse函數") while True: event=pygame.event.wait() if event.type == pygame.QUIT: exit() b=pygame.mouse.get_pressed() #獲取鼠標按鍵的情況(是否被按下) #返回一個由布爾值組成的列表,代表所有鼠標按鍵被按下的情況。True意味着在調用此方法時該鼠標按鍵正被按下 #(1, 0, 1) 表示左鍵和右鍵被按下(左鍵、中鍵、右鍵) #這條語句沒有體現滾輪的滾動
#必須先調用pygame.event.wait等語句后才能工作
print(b) pygame.display.update()
pygame.mouse.get_pos() —— 獲取鼠標光標的位置
import pygame pygame.init() screen = pygame.display.set_mode((196, 100)) pygame.display.set_caption("pygame.mouse函數") while True: event=pygame.event.wait() if event.type == pygame.QUIT: exit() b=pygame.mouse.get_pos() #獲取鼠標光標的位置 #返回鼠標光標的坐標 (x, y)。這個坐標以窗口左上角為基准點 #必須先調用pygame.event.wait等語句后才能准確獲取 print(b) pygame.display.update()
pygame.mouse.get_rel() —— 獲取鼠標與前一位置的偏移量
pygame.mouse.set_pos() —— 設置鼠標光標的位置
import pygame pygame.init() screen = pygame.display.set_mode((196, 100)) pygame.display.set_caption("pygame.mouse函數") while True: event=pygame.event.wait() if event.type == pygame.QUIT: exit() pygame.mouse.set_pos(50,10) #設置鼠標光標的位置 #如果鼠標光標是可視的,則光標將會跳到新的坐標上。移動鼠標將會產生一個新的 pygame.MOUSEMOTION 事件 print('a') pygame.display.update()
pygame.mouse.set_visible() —— 隱藏或顯示鼠標光標
b=pygame.mouse.set_visible(False) #設置鼠標是否可見 #True 可見,False 不可見 #返回值:返回在設置之前的狀態
pygame.mouse.get_focused() —— 檢查程序界面是否獲得鼠標焦點
b=pygame.mouse.get_focused() #pygame窗口是否在接收鼠標事件 #當 pygame 正在接受鼠標輸入事件(或者用專業術語說,鼠標正在處於“active”或“focus”狀態)返回值為 True
pygame.mouse.set_cursor() —— 設置鼠標光標在程序內的顯示圖像
pygame.mouse.get_cursor() —— 獲取鼠標光標在程序內的顯示圖像
b=pygame.mouse.get_cursor() #獲取鼠標光標在程序內的顯示圖像 #((16, 16), (0, 0), (0, 0, 64, 0, 96, 0, 112, 0, 120, 0, 124, 0, 126, 0, 127, 0, 127, 128, 124, 0, 108, 0, 70, 0, 6, 0, 3, 0, 3, 0, 0, 0), (192, 0, 224, 0, 240, 0, 248, 0, 252, 0, 254, 0, 255, 0, 255, 128, 255, 192, 255, 224, 254, 0, 239, 0, 207, 0, 135, 128, 7, 128, 3, 0))