Example of a simple puzzle game implemented by Python plus pygame

Source: Internet
Author: User
Tags ord
The examples in this paper describe the simple puzzle game implemented by Python plus pygame. Share to everyone for your reference. The implementation method is as follows:

Import Pygame, sys, randomfrom pygame.locals import *# Some constants windowwidth = 500WINDOWHEIGHT = 500BACKGROUNDCOLOR = (255, 255, 255) BLUE = (0, 0, 255) BLACK = (0, 0, 0) FPS = 40VHNUMS = 3CELLNUMS = Vhnums*vhnumsmaxrandtime = 100# exit def terminate (): P  Ygame.quit () Sys.exit () # randomly generated game face 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 (boar D, Blackcell) return board, blackcell# if the blank image block is not on the leftmost side, move the block to the left of the blank block to the blank block position def moveright (board, Blackcell): if Blackcell% vhn UMS = = 0:return Blackcell board[blackcell-1], Board[blackcell] = Board[blackcell], Board[blackcell-1] return blackCe ll-1# if the blank image block is not on the far right, the blank block rightEdge blocks move to blank block position def moveLeft (board, Blackcell): if Blackcell% vhnums = = VHNUMS-1: Return Blackcell board[blackcell+1], b Oard[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[black Cell], 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 to complete def isfinished (board, Blackcell): For I in Range (CELLNUMS-1): If board [i]! = I:return False return true# initialize Pygame.init () Mainclock = Pygame.time.Clock () # load Picture Gameimage = Pygame.image.lo AD (' pic.bmp ') Gamerect = Gameimage.get_rect () # set Window Windowsurface = Pygame.display.set_mode ((gamerect.width, gamerect.height)) pygame.display.set_caption(' jigsaw ') cellwidth = Int (gamerect.width/vhnums) cellheight = Int (gamerect.height/vhnums) finish = Falsegameboard, BlackCel L = Newgameboard () # game main Loop while True:for event in Pygame.event.get (): if Event.type = = Quit:terminate () if fi nish: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 (Gameboa  Rd, 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 = = MOUSE Buttondown and Event.button = = 1:x, y = pygame.mouse.get_pos () col = int (x/cellwidth) row = Int (Y/cell Height) index = col + row*vhnums if (index = = BLACKCELL-1 or index = = blackcell+1 or index = = blackcell-vhnums O R 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 (backgroundcol OR) 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) F or 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)

Hopefully 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.