在上一节中,我们创建了一个随机组合的图形组, 如何绘制呢?
(1)我们有图形的行列位置, 有形状, 有颜色, 首先, 我们需要一个 函数, 能够根据这些信息来绘制一个图形:
def drawIcon(displaysurf, shape, color, boxx, boxy):
# 根据位置,形状,颜色,绘制一个图形
quarter = int(BOXSIZE * 0.25)
half = int(BOXSIZE * 0.5)
left, top = leftTopCoordsOfBox(boxx, boxy)
if shape == DONUT:
pygame.draw.circle(displaysurf, color, (left+half, top+half), half-5)
pygame.draw.circle(displaysurf, BGCOLOR, (left+half, top+half), quarter-5)
elif shape == DIAMOND:
pygame.draw.polygon(displaysurf, color, ((left+half, top),
(left+BOXSIZE-1, top+half),
(left+half, top+BOXSIZE-1),
(left, top+half)))
elif shape == SQUARE:
pygame.draw.rect(displaysurf, color, (left+quarter, top+quarter,
BOXSIZE-half, BOXSIZE-half))
elif shape == LINES:
for i in range(0, BOXSIZE, 4):
pygame.draw.line(displaysurf, color, (left, top+i), (left + i, top))
pygame.draw.line(displaysurf, color, (left+i, top+BOXSIZE-1),
(left+BOXSIZE-1, top+i))
elif shape == OVAL:
pygame.draw.ellipse(displaysurf, color, (left, top+quarter, BOXSIZE, half))
(2)然后,再创建一个函数,来绘制所有的随机图形的组合
def drawBoard(displaysurf, board):
# 绘制所有的图形组
for boxx in range(BOARD_WIDTH):
for boxy in range(BOARD_HEIGHT):
shape, color = getShapeAndColor(board, boxx, boxy)
drawIcon(displaysurf, shape, color, boxx, boxy)
(3)在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)
# 创建图形组合
board = getRandomizedBoard()
while True:
displaysurf.fill(BGCOLOR)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
# 绘制图形组合
drawBoard(displaysurf, board
pygame.display.update()
fpsclock.tick(FPS)
if __name__ == '__main__':
main()
结果显示如下:
下面是到目前为止的完整代码:
# Memory Puzzle
import random, pygame, sys
from pygame.locals import *
# 配置基础 ------------------------------------------------------------
FPS = 30 # 设置帧数为30
WINDOW_WIDTH = 640 # 窗口宽度640像素
WINDOW_HEIGHT = 480 # 窗口告诉480像素
BOARD_WIDTH = 3 # 每行有3个图形
BOARD_HEIGHT = 2 # 每行有2个图形
BOXSIZE = 40 # 图形范围
GAPSIZE = 10 # 每个图形之间的间隔
# 左边边缘位置
XMARGIN = int((WINDOW_WIDTH - BOARD_WIDTH*(BOXSIZE+GAPSIZE)) / 2)
# 顶部边缘位置
YMARGIN = int((WINDOW_HEIGHT - BOARD_HEIGHT*(BOXSIZE+GAPSIZE)) /2 )
# 游戏中需要用到的颜色设置 -----------------------------------------------
# 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
ALLCOLORS = (RED, GREEN, BLUE, YELLOW, ORANGE, PUPPLE, CYAN)
# 游戏中用到的5种形状 ---------------------------------------------------
DONUT = 'donut'
SQUARE = 'square'
DIAMOND = 'diamond'
LINES = 'lines'
OVAL = 'oval'
ALLSHAPES = (DONUT, SQUARE, DIAMOND, LINES, OVAL)
# 工具函数 -------------------------------------------------------------
def leftTopCoordsOfBox(boxx, boxy):
# 将图形行列位置转换为屏幕上的像素坐标
left = boxx * (BOXSIZE + GAPSIZE) + XMARGIN
top = boxy * (BOXSIZE + GAPSIZE) + YMARGIN
return left, top
def getRandomizedBoard():
# 生成随机的图形颜色组合
icons = [] # 用列表保存
for color in ALLCOLORS:
for shape in ALLSHAPES:
icons.append((shape, color))
random.shuffle(icons) # 打乱序列
numIconsUsed = int(BOARD_WIDTH * BOARD_HEIGHT / 2) # 计算要使用的图形数
icons = icons[:numIconsUsed] * 2 # 根据要使用的图形数截取出来图形, 并翻倍配对
random.shuffle(icons) # 再次打乱图形
# 将创建好的图形放入图形组列表
board = []
for x in range(BOARD_WIDTH):
column = []
for y in range(BOARD_HEIGHT):
column.append(icons[0])
del icons[0]
board.append(column)
return board
def getShapeAndColor(board, boxx, boxy):
# 根据行列信息返回形状和颜色
return board[boxx][boxy][0], board[boxx][boxy][1]
def drawIcon(displaysurf, shape, color, boxx, boxy):
# 根据位置,形状,颜色,绘制一个图形
quarter = int(BOXSIZE * 0.25)
half = int(BOXSIZE * 0.5)
left, top = leftTopCoordsOfBox(boxx, boxy)
if shape == DONUT:
pygame.draw.circle(displaysurf, color, (left+half, top+half), half-5)
pygame.draw.circle(displaysurf, BGCOLOR, (left+half, top+half), quarter-5)
elif shape == DIAMOND:
pygame.draw.polygon(displaysurf, color, ((left+half, top),
(left+BOXSIZE-1, top+half),
(left+half, top+BOXSIZE-1),
(left, top+half)))
elif shape == SQUARE:
pygame.draw.rect(displaysurf, color, (left+quarter, top+quarter,
BOXSIZE-half, BOXSIZE-half))
elif shape == LINES:
for i in range(0, BOXSIZE, 4):
pygame.draw.line(displaysurf, color, (left, top+i), (left + i, top))
pygame.draw.line(displaysurf, color, (left+i, top+BOXSIZE-1),
(left+BOXSIZE-1, top+i))
elif shape == OVAL:
pygame.draw.ellipse(displaysurf, color, (left, top+quarter, BOXSIZE, half))
def drawBoard(displaysurf, board):
# 绘制所有的图形组
for boxx in range(BOARD_WIDTH):
for boxy in range(BOARD_HEIGHT):
shape, color = getShapeAndColor(board, boxx, boxy)
drawIcon(displaysurf, shape, color, boxx, boxy)
# 定义main函数 ---------------------------------------------------------
def main():
pygame.init() # pygame初始化
fpsclock = pygame.time.Clock()
displaysurf = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption("Memory Game") # 设置窗口标题
displaysurf.fill(BGCOLOR)
# 创建图形组合
board = getRandomizedBoard()
while True: # 游戏主题循环
displaysurf.fill(BGCOLOR) # 用背景色填充窗口
for event in pygame.event.get(): # 获取游戏事件
if event.type == QUIT:
pygame.quit()
sys.exit()
# 绘制图形组合
drawBoard(displaysurf, board)
pygame.display.update()
fpsclock.tick(FPS)
if __name__ == '__main__':
main()