Pygame实现记忆拼图游戏5

2.2 绘制图案函数drawBoard()

绘制图案函数drawBoard()的代码如图8所示。

图8 绘制图案函数drawBoard()的代码

其中,drawBoard()的第一个参数表示游戏用到的70个图案,第二个参数表示图案对应的状态数据;第53-54行通过for循环遍历图案的行和列;第55行通过自定义函数leftTopCoordsOfBox()获取每个图案左上角的坐标;第57行判断该图案的状态,如果处于“扣下”状态,则通过57-58行代码通过pygame.draw.rect()绘制该图案的背面,BOXCOLOR是之前定义好的白色。

相关链接1 pygame.draw.rect()函数的相关资料,请参考

《Pygame实现方块反弹》

3 main函数中调用startGameAnimation()函数

startGameAnimation()函数根据图案状态绘制了图案,而在main()函数中需要调用该函数才能实现该功能,代码如图9所示。

图9 main函数中调用startGameAnimation()函数

第11行代码通过getRandomizedBoard(),随机生成了游戏所需的70个图案;第12行代码产生这70个图案对应的状态数据;第13行代码通过startGameAnimation()函数绘制了这70个图案。从图9中可以看出,以上三行代码在while True循环之外。位于while True内部的第21行代码,调用drawBoard()函数,根据图案对应的状态数据revealedBoxes绘制图案。运行代码即可得到图5所示效果。

4 完整代码

以上提到的完整代码如下所示。

import pygame
import os
from pygame.locals import *
import random

def main():
    global DISPLAYSURF
    DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH,WINDOWHEIGHT))
    pygame.display.set_caption('Memory Puzzle')
    
    mainBoard = getRandomizedBoard()
    revealedBoxes = generateRevealedBoxesData(False)
    startGameAnimation(mainBoard)
    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                os.sys.exit()

        DISPLAYSURF.fill(BGCOLOR)
        drawBoard(mainBoard, revealedBoxes)
        pygame.display.update()

def getRandomizedBoard():
    icons = []
    for color in ALLCOLORS:
        for shape in ALLSHAPES:
            icons.append( (shape, color) )
            
    random.shuffle(icons) 
    numIconsUsed = int(BOARDWIDTH * BOARDHEIGHT / 2) 
    icons = icons[:numIconsUsed] * 2 
    random.shuffle(icons)

    board = []
    for x in range(BOARDWIDTH):
        column = []
        for y in range(BOARDHEIGHT):
            column.append(icons[0])
            del icons[0] 
        board.append(column)
    return board

def startGameAnimation(board):
    coveredBoxes = generateRevealedBoxesData(False)
    drawBoard(board, coveredBoxes)

def leftTopCoordsOfBox(boxx, boxy):
    left = boxx * (BOXSIZE + GAPSIZE) + XMARGIN
    top = boxy * (BOXSIZE + GAPSIZE) + YMARGIN
    return (left, top)

def drawBoard(board, revealed):
    for boxx in range(BOARDWIDTH):
        for boxy in range(BOARDHEIGHT):
            left, top = leftTopCoordsOfBox(boxx, boxy)
            if not revealed[boxx][boxy]:
                pygame.draw.rect(DISPLAYSURF, BOXCOLOR, \
                                 (left, top, BOXSIZE, BOXSIZE))
                
def generateRevealedBoxesData(val):
    revealedBoxes = []
    for i in range(BOARDWIDTH):
        revealedBoxes.append([val] * BOARDHEIGHT)
    return revealedBoxes

pygame.init()
WINDOWWIDTH = 640 
WINDOWHEIGHT = 480

BOARDWIDTH = 10 
BOARDHEIGHT = 7
assert (BOARDWIDTH * BOARDHEIGHT) % 2 == 0, \
           'Board needs to have an even number of boxes for pairs of matches.'

BOXSIZE = 40
GAPSIZE = 10
XMARGIN = int((WINDOWWIDTH - (BOARDWIDTH * (BOXSIZE + GAPSIZE))) / 2)
YMARGIN = int((WINDOWHEIGHT - (BOARDHEIGHT * (BOXSIZE + GAPSIZE))) / 2)

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)
PURPLE   = (255,   0, 255)
CYAN     = (  0, 255, 255)

DONUT = 'donut'
SQUARE = 'square'
DIAMOND = 'diamond'
LINES = 'lines'
OVAL = 'oval'

BGCOLOR = NAVYBLUE
BOXCOLOR = WHITE

ALLCOLORS = (RED, GREEN, BLUE, YELLOW, ORANGE, PURPLE, CYAN)
ALLSHAPES = (DONUT, SQUARE, DIAMOND, LINES, OVAL)
assert len(ALLCOLORS) * len(ALLSHAPES) * 2 >= BOARDWIDTH * BOARDHEIGHT, \
                "Board is too big for the number of shapes/colors defined."

if __name__ == '__main__':
    main()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值