程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 更多關於編程 >> Python加pyGame實現的簡單拼圖游戲實例

Python加pyGame實現的簡單拼圖游戲實例

編輯:更多關於編程

     本文實例講述了Python加pyGame實現的簡單拼圖游戲。分享給大家供大家參考。具體實現方法如下:

    ? 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 43 44 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 84 85 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 119 120 121 import pygame, sys, random from pygame.locals import * # 一些常量 WINDOWWIDTH = 500 WINDOWHEIGHT = 500 BACKGROUNDCOLOR = (255, 255, 255) BLUE = (0, 0, 255) BLACK = (0, 0, 0) FPS = 40 VHNUMS = 3 CELLNUMS = VHNUMS*VHNUMS MAXRANDTIME = 100 # 退出 def terminate(): pygame.quit() sys.exit() # 隨機生成游戲盤面 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 # 若空白圖像塊不在最左邊,則將空白塊左邊的塊移動到空白塊位置 def moveRight(board, blackCell): if blackCell % VHNUMS == 0: return blackCell board[blackCell-1], board[blackCell] = board[blackCell], board[blackCell-1] return blackCell-1 # 若空白圖像塊不在最右邊,則將空白塊右邊的塊移動到空白塊位置 def moveLeft(board, blackCell): if blackCell % VHNUMS == VHNUMS-1: return blackCell board[blackCell+1], board[blackCell] = board[blackCell], board[blackCell+1] return blackCell+1 # 若空白圖像塊不在最上邊,則將空白塊上邊的塊移動到空白塊位置 def moveDown(board, blackCell): if blackCell < VHNUMS: return blackCell board[blackCell-VHNUMS], board[blackCell] = board[blackCell], board[blackCell-VHNUMS] return blackCell-VHNUMS # 若空白圖像塊不在最下邊,則將空白塊下邊的塊移動到空白塊位置 def moveUp(board, blackCell): if blackCell >= CELLNUMS-VHNUMS: return blackCell board[blackCell+VHNUMS], board[blackCell] = board[blackCell], board[blackCell+VHNUMS] return blackCell+VHNUMS # 是否完成 def isFinished(board, blackCell): for i in range(CELLNUMS-1): if board[i] != i: return False return True # 初始化 pygame.init() mainClock = pygame.time.Clock() # 加載圖片 gameImage = pygame.image.load('pic.bmp') gameRect = gameImage.get_rect() # 設置窗口 windowSurface = pygame.display.set_mode((gameRect.width, gameRect.height)) pygame.display.set_caption('拼圖') cellWidth = int(gameRect.width / VHNUMS) cellHeight = int(gameRect.height / VHNUMS) finish = False gameBoard, blackCell = newGameBoard() # 游戲主循環 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.blit(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)

    希望本文所述對大家的Python程序設計有所幫助。

    1. 上一頁:
    2. 下一頁:
    Copyright © 程式師世界 All Rights Reserved