pygame.color是返回顏色變量的api。
Color(name) -> Color
Color(r, g, b, a) -> Color
Color(rgbvalue) -> Color
例子:
blue = pygame.Color(0, 0, 255)
red = pygame.Color(255,0,0)
也可以用The HTML format is ‘#rrggbbaa’, where rr, gg, bb, and aa are 2-character hex numbers. The alpha aa is optional. A hex number string has the form ‘0xrrggbbaa’, where aa is optional:
red= pygame.Color(0xFF000000)
如果需要用大量的顏色,又不想定義太多,可以直接使用預定義好的顏色:
from pygame.color import THECOLORS
使用時,直接使用顏色的宏定義:
THECOLORS["red1"]
宏都定義在:mvim lib/python3.7/site-packages/pygame/colordict.py
例子:
red = pygame.Color(0xFF000000)
SCREEN.fill(THECOLORS[blue])
square = pygame.Rect(pos[0],pos[1],10,10)
pygame.draw.rect(SCREEN, red , square, 0)
pygame.display.update()
在一個藍色背景畫一個紅色矩形