我最近开始学习pygame。我制作了一个非常基本的程序(它所做的只是改变它自己的背景颜色):
import pygame
from pygame.locals import *
pygame.init()
DISPLAY = pygame.display.set_mode((800,600))
while True:
for event in pygame.event.get():
if event == QUIT:
pygame.quit()
sys.exit()
DISPLAY.fill((3,4,5))
pygame.display.update()
然而,当程序运行时,它所做的只是产生一个空白的黑色 pygame 窗口。我正在使用 Windows 10 64 位家庭版以及 python 3.5.2 和 pygame 1.9.1。请帮我找出为什么该程序无法运行?
最佳答案
效果很好,您只是使用了非常暗的颜色(R=3/255,G=4/255,B=5/255)。尝试此操作以获得蓝屏:
DISPLAY.fill((0,0,255))
来自documentation在Surface.fill
上:
fill(color, rect=None, special_flags=0) -> Rect
...
The color argument can be either a RGB sequence, a RGBA sequence or a mapped color index.
RGB 值的每个分量的范围为 0 到 255,其中 255 表示该分量的最大强度。纯黑色将表示为 (0, 0, 0)
,白色表示为 (255, 255, 255)
。
为了更好地演示(或有趣),您可以使用此程序来了解可以从不同的 RGB 值获得什么背景颜色:
from itertools import product
import pygame
import sys
pygame.init()
DISPLAY = pygame.display.set_mode((200, 200))
for r, g, b in product(range(0, 255, 16), repeat=3):
print('r={}, g={}, b={}'.format(r, g, b))
DISPLAY.fill((r, g, b))
pygame.display.update()
pygame.time.delay(10)
pygame.quit()
sys.exit()
关于python - Pygame Surface.fill() 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41873581/