14.2.4 更改背景颜色
import pygame
pygame.init()
# 窗口的宽和高
W = 800
H = 600
size = (W, H)
# 和显示相关的使用display 创建Windows 对象
windows = pygame.display.set_mode(size)
# 设置 标题
pygame.display.set_caption('简单贪吃蛇')
# 定义是否退出
quit = True
# 游戏时间钟
clock = pygame.time.Clock()
# 1.解决:一闪而过:Pygame自上而下执行
# 使用while 解决问题
while quit:
# 2.解决卡死问题
# 游戏退出事件
events = pygame.event.get() # 获取事件
for event in events: # 遍历所有的事件
print(event) # 当退出时,显示QUIT
if event.type == pygame.QUIT: # 如果点了退出
quit = False
# 渲染:画出来
# 渲染:画出来
# (向哪里画,画什么颜色,(位置:你们那开始,w,h))
#颜色——RGB
#red=(255,0,0)
#white=(255,255,255)
#black=(0,0,0)
pygame.draw.rect(windows,(255,255,255),(0,0,W,H))
pygame.display.flip()
# 设置帧数
clock.tick(20)
# 退出
pygame.quit()(https://www.daowen.com)