教你使用Python的pygame模塊實(shí)現(xiàn)拼圖游戲
pygame介紹
Python Pygame 是一款專門為開發(fā)和設(shè)計(jì) 2D 電子游戲而生的軟件包,它支 Windows、Linux、Mac OS 等操作系統(tǒng),具有良好的跨平臺性。Pygame 由 Pete Shinners 于 2000 年開發(fā)而成,是一款免費(fèi)、開源的的軟件包,因此您可以放心地使用它來開發(fā)游戲,不用擔(dān)心有任何費(fèi)用產(chǎn)生。
Pygame 在 SDL(Simple DirectMedia Layer,使用 C語言編寫的多媒體開發(fā)庫) 的基礎(chǔ)上開發(fā)而成,它提供了諸多操作模塊,比如圖像模塊(image)、聲音模塊(mixer)、輸入/輸出(鼠標(biāo)、鍵盤、顯示屏)模塊等。相比于開發(fā) 3D 游戲而言,Pygame 更擅長開發(fā) 2D 游戲,比如于飛機(jī)大戰(zhàn)、貪吃蛇、掃雷等游戲。
安裝pygame
pip install pygame
pygame常用模塊
- pygame.cdrom 訪問光驅(qū)
- pygame.cursors 加載光標(biāo)
- pygame.display 訪問顯示設(shè)備
- pygame.draw 繪制形狀、線和點(diǎn)
- pygame.event 管理事件
- pygame.font 使用字體
- pygame.image 加載和存儲圖片
- pygame.joystick 使用游戲手柄或者類似的東西
- pygame.key 讀取鍵盤按鍵
- pygame.mixer 聲音
- pygame.mouse 鼠標(biāo)
- pygame.movie 播放視頻
- pygame.music 播放音頻
- pygame.overlay 訪問高級視頻疊加
- pygame.rect 管理矩形區(qū)域
- pygame.scrap 本地剪貼板訪問
- pygame.sndarray 操作聲音數(shù)據(jù)
- pygame.sprite 操作移動圖像
- pygame.surface 管理圖像和屏幕
- pygame.surfarray 管理點(diǎn)陣圖像數(shù)據(jù)
- pygame.time 管理時間和幀信息
- pygame.transform 縮放和移動圖像
pygame入門案例
import pygame import sys pygame.init() # 初始化pygame size = width, height = 320, 240 # 設(shè)置窗口大小 screen = pygame.display.set_mode(size) # 顯示窗口 while True: # 死循環(huán)確保窗口一直顯示 for event in pygame.event.get(): # 遍歷所有事件 if event.type == pygame.QUIT: # 如果單擊關(guān)閉窗口,則退出 sys.exit() pygame.quit() # 退出pygame
pygame實(shí)現(xiàn)拼圖游戲
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() # 隨機(jī)生成游戲盤面 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('1.jpg') gameRect = gameImage.get_rect() # 設(shè)置窗口,窗口的寬度和高度取決于圖片的寬高 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() # 游戲主循環(huán) 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)
總結(jié)
到此這篇關(guān)于使用Python的pygame模塊實(shí)現(xiàn)拼圖游戲的文章就介紹到這了,更多相關(guān)Python pygame實(shí)現(xiàn)拼圖游戲內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python?常用內(nèi)置模塊超詳細(xì)梳理總結(jié)
模塊是一個包含索引你定義的函數(shù)和變量的文件,其擴(kuò)展名為.py。模塊可以被其他程序引入,以使用該模塊中的函數(shù)等功能。這也是使用python標(biāo)準(zhǔn)庫的方法2022-03-03Python下singleton模式的實(shí)現(xiàn)方法
這篇文章主要介紹了Python下singleton模式的實(shí)現(xiàn)方法,有一定的借鑒價值,需要的朋友可以參考下2014-07-07使用pytorch進(jìn)行張量計(jì)算、自動求導(dǎo)和神經(jīng)網(wǎng)絡(luò)構(gòu)建功能
pytorch它是一個基于Python的開源深度學(xué)習(xí)框架,它提供了兩個核心功能:張量計(jì)算和自動求導(dǎo),這篇文章主要介紹了使用pytorch進(jìn)行張量計(jì)算、自動求導(dǎo)和神經(jīng)網(wǎng)絡(luò)構(gòu)建,需要的朋友可以參考下2023-04-04django rest framework serializer返回時間自動格式化方法
這篇文章主要介紹了django rest framework serializer返回時間自動格式化方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03Python+tkinter實(shí)現(xiàn)樹形圖繪制
Treeview是ttk中的樹形表組件,功能十分強(qiáng)大,非常適用于系統(tǒng)路徑的表達(dá),下面我們就來看看如何利用這一組件實(shí)現(xiàn)樹形圖的繪制吧,有需要的可以參考下2023-09-09linux環(huán)境下python中MySQLdb模塊的安裝方法
這篇文章主要給大家介紹了在linux環(huán)境下python中MySQLdb模塊的安裝方法,文中給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。2017-06-06