Pygame 设置背景色教程:使用 fill() 方法
在 Pygame 中,可以使用 pygame.Surface.fill() 方法来设置背景色。该方法接受一个 RGB 元组作为参数,用于指定颜色。
以下是一个设置背景色的例子:
import pygame
# 初始化 Pygame
pygame.init()
# 设置窗口尺寸
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
# 设置背景色
background_color = (255, 255, 255) # 白色
screen.fill(background_color)
# 更新屏幕显示
pygame.display.flip()
# 保持窗口打开,直到用户关闭窗口
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# 关闭 Pygame
pygame.quit()
在上述代码中,我们使用 screen.fill() 方法将背景色设置为白色。你可以将 RGB 元组 (255, 255, 255) 替换为你想要的颜色值。
原文地址: http://www.cveoy.top/t/topic/bGqZ 著作权归作者所有。请勿转载和采集!