1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 5, 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 11 9 120 121 |
Import Pygame, sys, random from pygame.locals import * # some constants WindowWidth = WindowHeight = + BackgroundColor = (255 , 255, 255) BLUE = (0, 0, 255) Black = (0, 0, 0) FPS = vhnums = 3 Cellnums = vhnums*vhnums maxrandtime = 100 # exit Def t Erminate (): Pygame.quit () sys.exit () # randomly generated game panel def Newgameboard (): board = [] for I in range (cellnums): Board.append (i) b Lackcell = 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, b Lackcell # If the blank image block is not leftmost, move the block to the left of the blank block to the blank block position def moveright (board, Blackcell): if Blackcell% vhnums = 0:return Blackcell boa RD[BLACKCELL-1], Board[blackcell] = Board[blackcell], board[blackcell-1] return BLACKCELL-1 # If the blank image block is not on the far right, Then move the block to the right of the blank block to the blank block position def moveLeft (board, BLAckcell): If Blackcell% vhnums = = VHNUMS-1: Return Blackcell board[blackcell+1], Board[blackcell] = Board[blackcell], boa Rd[blackcell+1] Return blackcell+1 # If the blank image block is not at the top, move the block above the blank block to the blank block position def movedown (board, Blackcell): if Blackcell < Vhnu Ms: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 blank block position def moveUp (board, Blackcell): If Blackcell >= cellnums-vhnums: Return Blackcell Board[blackcell+vhnums], Board[blackcell] = Board[blackcell], Board[blackcell+vhnums] return Blackcell+vhnums # Complete Def isfinished (board, Blackcell): For I in Range (CELLNUMS-1): if Board[i]!= i:return False retur N True # Initialize Pygame.init () Mainclock = Pygame.time.Clock () # load Picture gameimage = Pygame.image.load (' pic.bmp ') Gamerect = game Image.get_rect () # set 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 () # game main Loop while True:for Eve NT 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 (Gameboa Rd, 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 = = Blackce Ll+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.blit (Gameimage, rectDst, RectArea) For I in Range (vhnums+1): Pygame.draw.line (Windowsurface, Black, (i*cellwidth, 0), (i*cellwidth, gamerect.height)) for I I N Range (vhnums+1): Pygame.draw.line (Windowsurface, Black, (0, I*cellheight), (Gamerect.width, I*cellheight)) Pygame.display.update () Mainclock.tick (FPS) |