当前位置: 首页 > article >正文

Pygame实现记忆拼图游戏10

5 调用函数实现图案的渐变显示和渐变遮盖

startGameAnimation()函数中,在“1 将70个图案进行随机分组”中提到的代码将70个图案进行分组后,调用“3 图案的渐变显示”和“4 图案的渐变遮盖”中提到的两个函数实现图案的渐变显示和渐变遮盖,代码如图12所示。

图12 实现图案的渐变显示和渐变遮盖的函数

其中,boxGroups为将70个图案分成9组后的数据;使用for循环分别对这9组数据进行处理,实现这9组数据对应图案的渐变显示和渐变遮盖,效果如图1所示。

6 完整代码

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

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

def main():
    global DISPLAYSURF
    global FPSCLOCK
    FPSCLOCK = pygame.time.Clock()
    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)

    boxes = []
    for x in range(BOARDWIDTH):
        for y in range(BOARDHEIGHT):
            boxes.append( (x, y) )
    random.shuffle(boxes)
    boxGroups = splitIntoGroupsOf(8, boxes)
    for boxGroup in boxGroups:
        revealBoxesAnimation(board, boxGroup)
        coverBoxesAnimation(board, boxGroup)
                                    
def revealBoxesAnimation(board, boxesToReveal):
    for coverage in range(BOXSIZE, (-REVEALSPEED) - 1, -REVEALSPEED):
        drawBoxCovers(board, boxesToReveal, coverage)

def coverBoxesAnimation(board, boxesToCover):
    for coverage in range(0, BOXSIZE + REVEALSPEED, REVEALSPEED):
        drawBoxCovers(board, boxesToCover, coverage)
        
def drawBoxCovers(board, boxes, coverage):
    for box in boxes:
        left, top = leftTopCoordsOfBox(box[0], box[1])
        pygame.draw.rect(DISPLAYSURF, BGCOLOR, (left, top, BOXSIZE, BOXSIZE))
        shape, color = getShapeAndColor(board, box[0], box[1])
        drawIcon(shape, color, box[0], box[1])
        if coverage > 0: 
            pygame.draw.rect(DISPLAYSURF, BOXCOLOR, \
                             (left, top, coverage, BOXSIZE))
    pygame.display.update()
    FPSCLOCK.tick(FPS)
    
def splitIntoGroupsOf(groupSize, theList):
    result = []
    for i in range(0, len(theList), groupSize):
        result.append(theList[i:i + groupSize])
    return result

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))
            else:
                shape, color = getShapeAndColor(board, boxx, boxy)
                drawIcon(shape, color, boxx, boxy)
                
def generateRevealedBoxesData(val):
    revealedBoxes = []
    for i in range(BOARDWIDTH):
        revealedBoxes.append([val] * BOARDHEIGHT)
    return revealedBoxes

def getShapeAndColor(board, boxx, boxy):
    return board[boxx][boxy][0], board[boxx][boxy][1]
def drawIcon(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 == SQUARE:
        pygame.draw.rect(DISPLAYSURF, color, \
            (left + quarter, top + quarter, BOXSIZE - half, BOXSIZE - half))
    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 == 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))
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
REVEALSPEED = 8
FPS = 5
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()


http://www.kler.cn/a/590760.html

相关文章:

  • 【Deepseek进阶篇】--4.科研运用
  • Softmax 函数简介及其Python实现
  • 【Matlab GUI】封装matlab GUI为exe文件
  • Nuxt2 vue 给特定的页面 body 设置 background 不影响其他页面
  • vue3中如何实现路由导航跳转
  • 蓝桥杯 矩形拼接 10分题
  • R语言——变量
  • ESP32中的内存架构
  • 【TVM教程】使用自定义调度规则(Sketch Rule)在 CPU 上自动调度稀疏矩阵乘法
  • 基于gitea的本地仓库创建
  • 总结 kotlin中的关键字和常用方法
  • 计算机操作系统(6) (经典进程同步问题)
  • 工厂能耗系统完整解决方案 ——安科瑞企业能源管控平台
  • 穆迪暖色调人像静物摄影后期Lr调色教程,手机滤镜PS+Lightroom预设下载!
  • PSI5接口
  • Web3游戏行业报告
  • 手写发布订阅模式
  • 大数据分析方法(65页PPT)
  • mac npm run dev报错 error:0308010C:digital envelope routines::unsupported
  • Java 多线程编程简介