pygame memory puzzle,pygamepuzzle,import pygam


import pygame,random,sysfrom pygame.locals import *FPS = 30boxwidth = 3boxheight = 2assert boxwidth*boxheight%2 == 0,'board number needs to be even'winx = 640winy = 480between = 10boxsize = 40xmargin = int((winx-(boxsize+between)*boxwidth)/2)ymargin = int((winy-(boxsize+between)*boxheight)/2)speed = 8#color inusenavyblue = (60,60,100)#dispfillwhite = (255,255,255)#boardcoverblue = (0,0,255) #highlighrect#icons colorred = (255,0,0)yellow = (255,255,0)purple = (255,0,255)green = (0,255,0)gray = (0,255,255)#icons shaperectan = 'rectan'lines = 'lines'circ = 'circ'poly = 'poly'allcolor = (red,yellow,purple,green)allshape = (rectan,lines,circ,poly)assert len(allcolor)*len(allshape*2)>= boxwidth*boxheight,'board is too big for the number of color and shape'def main():    global fpsclock,disp    pygame.init()    disp = pygame.display.set_mode((winx,winy))    pygame.display.set_caption('memory puzzle')    fpsclock = pygame.time.Clock()    disp.fill(navyblue)    reveal = getrevealed(False)    mainboard = randomboard()    drawboard(mainboard,reveal)    startanimation(mainboard)    first = None    mousex = 0    mousey = 0    while True:        mouseclicked = False        disp.fill(navyblue)        drawboard(mainboard,reveal)        for event in pygame.event.get():            if event.type == QUIT or (  event.type == KEYUP and event.key == K_ESCAPE):                pygame.quit()                sys.exit()            elif event.type == MOUSEMOTION:                mousex,mousey = event.pos            elif event.type == MOUSEBUTTONUP:                mousex,mousey = event.pos                mouseclicked = True        boxx,boxy = getpixel(mousex,mousey)        if boxx!=None and boxy!= None:            if not reveal[boxx][boxy]:                highlighrect(boxx,boxy)            if not reveal[boxx][boxy] and mouseclicked:                revealboardanimation(mainboard,[(boxx,boxy)])                reveal[boxx][boxy] = True                if first == None:                    first = (boxx,boxy)                else:                    icon1shape,icon1color = getshapeandcolor(mainboard,first[0],first[1])                    icon2shape,icon2color = getshapeandcolor(mainboard,boxx,boxy)                    if icon1shape!= icon2shape or icon1color!= icon2color:                        pygame.time.wait(1000)                        coverboardanimation(mainboard,[(first[0],first[1]),(boxx,boxy)])                        reveal[first[0]][first[1]] = False                        reveal[boxx][boxy] = False                    elif haswon(reveal):                        wonanimation(mainboard)                        pygame.time.wait(2000)                        mainboard = randomboard()                        startanimation(mainboard)                        reveal = getrevealed(False)                        drawboard(mainboard,reveal)                    first = None        pygame.display.update()        fpsclock.tick(FPS)def leftop(boxx,boxy):    left = xmargin + boxx*(boxsize + between)    top = ymargin + boxy*(boxsize + between)    return left,topdef highlighrect(boxx,boxy):    left,top = leftop(boxx,boxy)    pygame.draw.rect(disp,blue,(left-between/2,top-between/2,boxsize+between,boxsize+between),4)def randomboard():    box = []    for shape in allshape:        for color in allcolor:            box.append((shape,color))    random.shuffle(box)    iconsnum = boxwidth*boxheight/2    icons = box[:iconsnum]*2    random.shuffle(icons)    board = []    for boxx in range(boxwidth):        column = []        for boxy in range(boxheight):            column.append(icons[0])            del icons[0]        board.append(column)    return boarddef getrevealed(val):    revealedbox = []    for i in range(boxwidth):        revealedbox.append([val]*boxheight)    return revealedboxdef getpixel(x,y):    for boxx in range(boxwidth):        for boxy in range(boxheight):            left,top = leftop(boxx,boxy)                b = pygame.Rect((left,top,boxsize,boxsize))            if b.collidepoint(x,y):                return(boxx,boxy)    return(None,None)def split(group,thelist):    result = []    for i in range(0,len(thelist),group):        result.append(thelist[i:group+i])    random.shuffle(result)    return resultdef getshapeandcolor(board,boxx,boxy):    return board[boxx][boxy][0],board[boxx][boxy][1]def drawicon(shape,color,boxx,boxy):    left,top = leftop(boxx,boxy)    if shape == rectan:        pygame.draw.rect(disp,color,(left+5,top+5,boxsize-10,boxsize-10))    elif shape == lines:        for i in range(0,boxsize,10):            pygame.draw.line(disp,color,(left+i,top),(left,top+i))            pygame.draw.line(disp,color,(left+boxsize-i,top+boxsize),(left+boxsize,top+boxsize-i))    elif shape == circ:        pygame.draw.circle(disp,color,(left+boxsize/2,top+boxsize/2),boxsize/2-5)    elif shape == poly:        pygame.draw.polygon(disp,color,((left+boxsize/2,top),(left+boxsize,top+boxsize/2),(left+boxsize/2,top+boxsize),(left,top+boxsize/2)))def drawoneboard(board,boxes,coverage):    for box in boxes:        shape,color = getshapeandcolor(board,box[0],box[1])        left,top = leftop(box[0],box[1])        pygame.draw.rect(disp,navyblue,(left,top,boxsize,boxsize))        drawicon(shape,color,box[0],box[1])        if coverage>0:            pygame.draw.rect(disp,white,(left,top,coverage,boxsize))    pygame.display.update()    fpsclock.tick(FPS)def coverboardanimation(board,boxcover):    for coverage in range(0,boxsize+speed,speed):        drawoneboard(board,boxcover,coverage)def revealboardanimation(board,boxreveal):    for coverage in range(boxsize,-speed-1,-speed):        drawoneboard(board,boxreveal,coverage)def drawboard(board,revealbox):    for boxx in range(boxwidth):                for boxy in range(boxheight):            left,top = leftop(boxx,boxy)            if not revealbox[boxx][boxy]:                pygame.draw.rect(disp,white,(left,top,boxsize,boxsize))            else:                shape,color = getshapeandcolor(board,boxx,boxy)                drawicon(shape,color,boxx,boxy)def startanimation(board):    boxes = []    for boxx in range(boxwidth):        for boxy in range(boxheight):            boxes.append((boxx,boxy))            random.shuffle(boxes)    revealbox = getrevealed(False)        drawboard(board,revealbox)    boxgroups = split(8,boxes)    for boxgroup in boxgroups:        revealboardanimation(board,boxgroup)        pygame.time.wait(300)        coverboardanimation(board,boxgroup)    pygame.display.update()    fpsclock.tick(FPS)def wonanimation(board):    color1 = gray    color2 = navyblue    revealbox = getrevealed(True)    for i in range(13):        color1,color2 = color2,color1        drawboard(board,revealbox)        disp.fill(color1)        pygame.time.wait(300)        pygame.display.update()def haswon(reveal):    for i in reveal:        if False in i:            return False    return Trueif __name__ == '__main__':    main()

评论关闭