(1) 导入需要的模块
import random
import pygame
import sys
from pygame.locals import *
(2) 基础配置
FPS = 30 # 设置帧数为30
WINDOW_WIDTH = 640
WINDOW_HEIGHT = 480
# 设置游戏画面为640 * 480像素
(3) 游戏中需要的颜色
# R G B
GRAY = (100,100,100)
NAVYBLUE = (60,60,100)
WHITE = (255,255,255)
RED = (255,0,0)
GREEN = (0,255,0)
BLUE = (0,0,255)
YELLOW = (255,255,0)
ORANGE = (255,128,0)
PUPPLE = (255,0,255)
CYAN = (0,255,255)
BGCOLOR = NAVYBLUE # 背景色设置为navyblue
(4) 定义main函数
def main():
pygame.init()
fpsclock = pygame.time.Clock()
displaysurf = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption("Memory Game")
displaysurf.fill(BGCOLOR)
while True:
displaysurf.fill(BGCOLOR)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
fpsclock.tick(FPS)
if __name__ == '__main__':
main()
运行程序, 显示一个深蓝色的背景
Memory Puzzle下一节:绘制图形