pygame slide puzzle

pygame slide puzzle,第1张

概述pygame slide puzzle

下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。

内存溢出小编现在分享给大家,也给大家做个参考。

import pygame,sys,randomfrom pygame.locals import*FPS = 30boardnum = 4boardsize = 80winx = 640winy = 480BASICFontSIZE = 20xmargin = int((winx - (boardnum*boardsize+(boardnum-1)))/2)ymargin = int((winy - (boardnum*boardsize+(boardnum-1)))/2)Blank = Nonegreen = (0,204,0)blue = (0,50,255)white = (255,255,255)dark = (3,54,73)up = 'up'down = 'down'L = 'left'R = 'right'def main():    global fpsclock,disp,BASICFont,reset_surf,reset_rect,solved_surf,solved_rect,new_surf,new_rect    pygame.init()    fpsclock = pygame.time.Clock()        disp = pygame.display.set_mode((winx,winy))    pygame.display.set_caption('slIDe puzzle')    BASICFont = pygame.Font.Font('freesansbold.ttf',BASICFontSIZE)    reset_surf,reset_rect = maketext('reset',white,green,winx-120,winy-90)    new_surf,new_rect = maketext('new',winy-60)    solved_surf,solved_rect = maketext('solve',winy-30)    mainboard,seq = newpuzzles(80)    solvedboard = startboard()    allmove = []        while True:        slIDe = None        msg = 'Click tile or press arrow keys to slIDe.'        if mainboard == solvedboard:            msg = 'win'        drawboard(mainboard,msg)        checkforquit()        for event in pygame.event.get():            if event.type == MOUSEbuttonUP:                spotx,spoty = getclicked(mainboard,event.pos[0],event.pos[1])                                if (spotx,spoty)==(None,None):                    if reset_rect.collIDepoint(event.pos):                        resetpuzzles(mainboard,allmove)                        allmove = []                    elif new_rect.collIDepoint(event.pos):                        mainboard,seq = newpuzzles(20)                        allmove = []                    elif solved_rect.collIDepoint(event.pos):                        resetpuzzles(mainboard,seq+allmove)                        allmove = []                                        else:                    blankx,blanky = getblank(mainboard)                    if spotx == blankx and spoty == blanky - 1 :                        slIDe = down                    elif spotx == blankx and spoty == blanky + 1 :                        slIDe = up                                          if spotx == blankx+1 and spoty == blanky :                        slIDe = L                    if spotx == blankx-1 and spoty == blanky :                        slIDe = R            elif event.type == KEYUP:                if event.key in (K_a,K_left) and valIDmove(mainboard,L):                    slIDe == L                elif event.key in (K_d,K_RIGHT) and valIDmove(mainboard,R):                    slIDe == R                elif event.key in (K_w,K_UP) and valIDmove(mainboard,up):                    slIDe == up                elif event.key in (K_s,K_DOWN) and valIDmove(mainboard,down):                    slIDe == down                    if slIDe:            slIDeanimation(mainboard,slIDe,'Click tile or press arrow keys to slIDe.',8)            makemove(mainboard,slIDe)            allmove.append(slIDe)        pygame.display.update()        fpsclock.tick(FPS)            def terminal():    pygame.quit()    sys.exit()def checkforquit():    for event in pygame.event.get(QUIT):        terminal()    for event in pygame.event.get(KEYUP):        if event.key == K_ESCAPE:            terminal        pygame.event.post(event)def startboard():    counter = 1    board = []    for x in range(boardnum):        column = []        for y in range(boardnum):                        column.append(counter)            counter+=boardnum        board.append(column)        counter-=boardnum*(boardnum-1)+boardnum-1    board[boardnum-1][boardnum-1] = Blank          return boarddef getblank(board):    for x in range(boardnum):        for y in range(boardnum):            if board[x][y] == Blank:                return (x,y)def makemove(board,move):    blankx,blanky = getblank(board)        if move == up:        board[blankx][blanky],board[blankx][blanky+1] = board[blankx][blanky+1],board[blankx][blanky]    if move == down:        board[blankx][blanky],board[blankx][blanky-1] = board[blankx][blanky-1],board[blankx][blanky]    if move == L:        board[blankx][blanky],board[blankx+1][blanky] = board[blankx+1][blanky],board[blankx][blanky]    if move == R:        board[blankx][blanky],board[blankx-1][blanky] = board[blankx-1][blanky],board[blankx][blanky] def valIDmove(board,blanky = getblank(board)    return (move == up and blanky != boardnum-1) or (move==down and blanky != 0) or (move == L and blankx != boardnum-1)or (move==R and blankx != 0)def getrandommove(board,lastmove = None):    valIDmoves = [up,down,L,R]    if lastmove == up or not valIDmove(board,down):        valIDmoves.remove(down)    if lastmove == down or not valIDmove(board,up):        valIDmoves.remove(up)    if lastmove == L or not valIDmove(board,R):        valIDmoves.remove(R)    if lastmove == R or not valIDmove(board,L):        valIDmoves.remove(L)    return random.choice(valIDmoves)def leftop(tilex,tiley):    left = xmargin + tilex*boardsize+tilex-1    top = ymargin + tiley*boardsize+tiley-1    return (left,top)def getclicked(board,x,y):    for tilex in range(boardnum):        for tiley in range(boardnum):            left,top = leftop(tilex,tiley)            b = pygame.Rect(left,top,boardsize,boardsize)            if b.collIDepoint(x,y):                return (tilex,tiley)    return(None,None)    def drawtile(tilex,tiley,number,ax = 0,ay = 0):          left,tiley)    pygame.draw.rect(disp,(left+ax,top+ay,boardsize))    textsuf = BASICFont.render(str(number),True,white)    textrect = textsuf.get_rect()    textrect.center = left + int(boardsize/2)+ax,top + int(boardsize/2)+ay    disp.blit(textsuf,textrect)def maketext(text,color,dark,left):    textsuf = BASICFont.render(text,dark)    textrect = textsuf.get_rect()    textrect.topleft = (top,left)    return(textsuf,textrect)        def drawboard(board,message):    disp.fill(dark)    if message:        textsuf,textrect = maketext(message,5,5)        disp.blit(textsuf,textrect)    for tilex in range(boardnum):        for tiley in range(boardnum):            if board[tilex][tiley]:                    drawtile(tilex,board[tilex][tiley])    left,top = leftop(0,0)    size = boardsize*boardnum    pygame.draw.rect(disp,blue,(left-5,top-5,size+11,size+11),4)    disp.blit(reset_surf,reset_rect)    disp.blit(new_surf,new_rect)    disp.blit(solved_surf,solved_rect)def slIDeanimation(board,direction,message,animationspeed):    blankx,blanky = getblank(board)    if direction == up:        movex =blankx        movey = blanky+1    if direction == down:        movex = blankx        movey = blanky-1    if direction == L:        movex = blankx + 1        movey = blanky    if direction == R:        movex = blankx - 1        movey = blanky    drawboard(board,message)    bash = disp.copy()    moveleft,movetop = leftop(movex,movey)    pygame.draw.rect(bash,(moveleft,movetop,boardsize))   # pygame.draw.rect(bash,boardsize))    for i in range(0,animationspeed):                checkforquit()        disp.blit(bash,(0,0))        if direction == up:            drawtile(movex,movey,board[movex][movey],-i)        if direction == down:            drawtile(movex,i)        if direction == L:            drawtile(movex,-i,0)        if direction == R:            drawtile(movex,i,0)        pygame.display.update()        fpsclock.tick(FPS)def newpuzzles(numslIDe):    board = startboard()    seq = []    drawboard(board,'')    pygame.display.update()    pygame.time.wait(500)    lastmove = None    for i in range(numslIDe):        move = getrandommove(board,lastmove)        slIDeanimation(board,move,'generate new game',animationspeed = int(boardsize/3))        makemove(board,move)                seq.append(move)        lastmove = move    return (board,seq)def resetpuzzles(board,allmove):    revallmove = allmove[:]    revallmove.reverse()    for move in revallmove:        if move == up:            oppositemove = down        elif move == down:            oppositemove = up        elif move == L:            oppositemove = R        elif move == R:            oppositemove = L        slIDeanimation(board,oppositemove,'',animationspeed=int(boardsize / 2))        makemove(board,oppositemove)if __name__ == '__main__':    main()            

以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

总结

以上是内存溢出为你收集整理的pygame slide puzzle全部内容,希望文章能够帮你解决pygame slide puzzle所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1199154.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存