1. 程式人生 > >Pygame詳解(十六):cursors 模組

Pygame詳解(十六):cursors 模組

pygame.cursors

Pygame 中使用游標資源的模組。

函式

  • pygame.cursors.compile()  ——  由純字串建立二進位制游標資料
  • pygame.cursors.load_xbm()  ——  由一個xbm 檔案載入游標資料

Pygame 提供對系統硬體游標的控制,並且只支援白色和黑色游標格式。你可以通過使用 pygame.mouse 內的方法控制游標。

cursors 模組包含載入和解碼各種游標格式的方法。這些方法允許你簡便地將你的游標儲存成擴充套件檔案,或者直接作為編碼後的 python 字串存在。

這個模組包含若干個標準游標。pygame.mouse.set_cursor() 方法能夠接收若干個引數。所有的引數以單個元組的形式儲存,你可以用如下方式呼叫此方法:

pygame.mouse.set_cursor(*pygame.cursors.arrow)

這個模組也包含了一些格式化字串形式的游標。在你使用到這些游標之前,你需要把相應字串傳遞給 pygame.cursors.compile() 方法。你可以參照如下示例來呼叫:

>>> cursor = pygame.cursors.compile(pygame.cursors.textmarker_strings)
>>> pygame.mouse.set_cursor(*cursor)

以下變數是可以被用作游標的點陣圖:

  • pygame.cursors.arrow
  • pygame.cursors.diamond
  • pygame.cursors.broken_x
  • pygame.cursors.tri_left
  • pygame.cursors.tri_right

以下字串可以通過 pygame.cursors.compile() 函式轉換成游標點陣圖:

  • pygame.cursors.thickarrow_strings
  • pygame.cursors.sizer_x_strings
  • pygame.cursors.sizer_y_strings
  • pygame.cursors.sizer_xy_strings

函式詳解

pygame.cursors.compile()

由純字串建立二進位制游標資料。

compile(strings, black=’X’, white=’.’, xor=’o’) -> data, mask

一串連續的字串可以被用於建立對應系統游標的二進位制游標資料。返回值要和 pygame.mouse.set_cursor() 所需要的引數格式相同。

如果你正在建立自己的游標字串,你可使用任何值來代表白色和黑色畫素。一些系統允許你根據系統顏色自己設定一種特殊的切換色,也被稱為 xor  色。如果系統不支援 xor 游標,則游標顏色將會變為純黑色。

字串的長度必須全部相等,而且可以被 8 整除。一個游標字串設定示例,如下所示:

thickarrow_strings = (               #sized 24x24
  "XX                      ",
  "XXX                     ",
  "XXXX                    ",
  "XX.XX                   ",
  "XX..XX                  ",
  "XX...XX                 ",
  "XX....XX                ",
  "XX.....XX               ",
  "XX......XX              ",
  "XX.......XX             ",
  "XX........XX            ",
  "XX........XXX           ",
  "XX......XXXXX           ",
  "XX.XXX..XX              ",
  "XXXX XX..XX             ",
  "XX   XX..XX             ",
  "     XX..XX             ",
  "      XX..XX            ",
  "      XX..XX            ",
  "       XXXX             ",
  "       XX               ",
  "                        ",
  "                        ",
  "                        ")

pygame.cursors.load_xbm()

由一個xbm 檔案載入游標資料。

load_xbm(cursorfile) -> cursor_args

load_xbm(cursorfile, maskfile) -> cursor_args

該方法將根據 XBM 檔案的某一個簡單子集載入游標。XBM 檔案從傳統上是被用於儲存 UNIX 系統內游標,它們是被用於代表一些簡單影象的 ASCII 碼。

一些時候,白色和黑色值將會分開在兩個獨立的 XBM 檔案中。你可以通過傳遞第二個 maskfile 引數將兩個影象載入到同一個游標中。

Cursorfile 和 maskfile 引數可以是帶有 readlines 方法的 filenames 或者 filelike 物件。

返回值 cursor_args 可以被直接傳遞給 pygame.mouse.set_cursor() 方法。