Simple puzzle game instance implemented by Python and pyGame

Source: Internet
Author: User
This article mainly introduces simple jigsaw puzzle games implemented by Python and pyGame, and analyzes the skills related to operating pictures in the pyGame module in the form of a complete example, for more information about how to use Python and pyGame, see the following example. Share it with you for your reference. The specific implementation method is as follows:

Import pygame, sys, randomfrom pygame. locals import * # some constants export wwidth = 500 export wheight = 500 BACKGROUNDCOLOR = (255,255,255) BLUE = (0, 0,255) BLACK = (0, 0, 0) FPS = 40 VHNUMS = 3 CELLNUMS = VHNUMS * VHNUMSMAXRANDTIME = 100 # exit def terminate (): pygame. quit () sys. exit () # randomly generate the game disk def newGameBoard (): board = [] for I in range (CELLNUMS): board. append (I) blackCell = CELLNUMS-1 board [blackCell] =-1 for I N 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 on the far left, move the block on the left of the blank block to the blank block position 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 blank block position 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 blank block position 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 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 # Whether def isFinished (board, B LackCell): for I in range (CELLNUMS-1): if board [I]! = I: return False return True # initialize pygame. init () mainClock = pygame. time. clock () # load the image gameImage = pygame.image.load('pic.bmp ') gameRect = gameImage. 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 = FalsegameBoard, blackCell = newGameBoard () # main loop of the game 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. BITs (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)

I hope this article will help you with Python programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.