| Import pygame, sys, random From pygame. locals import * # Constants Required wwidth = 500 Required wheight = 500 BACKGROUNDCOLOR = (255,255,255) BLUE = (0, 0,255) BLACK = (0, 0, 0) FPS = 40 VHNUMS = 3 CELLNUMS = VHNUMS * VHNUMS MAXRANDTIME = 100 # Exit Def terminate (): Pygame. quit () Sys. exit () # Randomly generate game disks Def newGameBoard (): Board = [] For I in range (CELLNUMS ): Board. append (I) BlackCell = CELLNUMS-1 Board [blackCell] =-1 For I in range (MAXRANDTIME ): Direction = random. randint (0, 3) If (direction = 0 ): BlackCell = moveLeft (board, blackCell) Elif (direction = 1 ): BlackCell = moveRight (board, blackCell) Elif (direction = 2 ): BlackCell = moveUp (board, blackCell) Elif (direction = 3 ): BlackCell = moveDown (board, blackCell) Return board, blackCell # If the blank image block is not at the leftmost, move the block on the left of the blank block to the position of the blank block. Def moveRight (board, blackCell ): If blackCell % VHNUMS = 0: Return blackCell Board [blackCell-1], board [blackCell] = board [blackCell], board [blackCell-1] Return blackCell-1 # If the blank image block is not on the rightmost side, move the block on the right of the blank block to the position of the blank block. Def moveLeft (board, blackCell ): If blackCell % VHNUMS = VHNUMS-1: Return blackCell Board [blackCell + 1], board [blackCell] = board [blackCell], board [blackCell + 1] Return blackCell + 1 # If the blank image block is not at the top, move the block above the blank block to the position of the blank block. Def moveDown (board, blackCell ): If blackCell <VHNUMS: Return blackCell Board [blackCell-VHNUMS], board [blackCell] = board [blackCell], board [blackCell-VHNUMS] Return blackCell-VHNUMS # If the blank image block is not at the bottom, move the block below the blank block to the position of the blank block. Def moveUp (board, blackCell ): If blackCell> = CELLNUMS-VHNUMS: Return blackCell Board [blackCell + VHNUMS], board [blackCell] = board [blackCell], board [blackCell + VHNUMS] Return blackCell + VHNUMS # Completed or not Def isFinished (board, blackCell ): For I in range (CELLNUMS-1 ): If board [I]! = I: Return False Return True # Initialization Pygame. init () MainClock = pygame. time. Clock () # Loading Images GameImage = pygame.image.load('pic.bmp ') GameRect = gameImage. get_rect () # Setting window WindowSurface = pygame. display. set_mode (gameRect. width, gameRect. height )) Pygame. display. set_caption ('jigsaw Puzzle ') CellWidth = int (gameRect. width/VHNUMS) CellHeight = int (gameRect. height/VHNUMS) Finish = False GameBoard, blackCell = newGameBoard () # Main game loop While True: For event in pygame. event. get (): If event. type = QUIT: Terminate () If finish: Continue If event. type = KEYDOWN: If event. key = K_LEFT or event. key = ord ('A '): BlackCell = moveLeft (gameBoard, blackCell) If event. key = K_RIGHT or event. key = ord ('D '): BlackCell = moveRight (gameBoard, blackCell) If event. key = K_UP or event. key = ord ('W '): BlackCell = moveUp (gameBoard, blackCell) If event. key = K_DOWN or event. key = ord ('s '): BlackCell = moveDown (gameBoard, blackCell) If event. type = MOUSEBUTTONDOWN and event. button = 1: X, y = pygame. mouse. get_pos () Col = int (x/cellWidth) Row = int (y/cellHeight) Index = col + row * VHNUMS If (index = blackCell-1 or index = blackCell + 1 or index = blackCell-VHNUMS or index = blackCell + VHNUMS ): GameBoard [blackCell], gameBoard [index] = gameBoard [index], gameBoard [blackCell] BlackCell = index If (isFinished (gameBoard, blackCell )): GameBoard [blackCell] = CELLNUMS-1 Finish = True WindowSurface. fill (BACKGROUNDCOLOR) For I in range (CELLNUMS ): RowDst = int (I/VHNUMS) ColDst = int (I % VHNUMS) RectDst = pygame. Rect (colDst * cellWidth, rowDst * cellHeight, cellWidth, cellHeight) If gameBoard [I] =-1: Continue RowArea = int (gameBoard [I]/VHNUMS) ColArea = int (gameBoard [I] % VHNUMS) RectArea = pygame. Rect (colArea * cellWidth, rowArea * cellHeight, cellWidth, cellHeight) WindowSurface. bsurface (gameImage, rectDst, rectArea) For I in range (VHNUMS + 1 ): Pygame. draw. line (windowSurface, BLACK, (I * cellWidth, 0), (I * cellWidth, gameRect. height )) For I in range (VHNUMS + 1 ): Pygame. draw. line (windowSurface, BLACK, (0, I * cellHeight), (gameRect. width, I * cellHeight )) Pygame. display. update () MainClock. tick (FPS) |