# Python实现拼图游戏

#_*_ coding:gbk _*_

importpygame, sys, random

frompygame.localsimport *

# 一些常量

WINDOWWIDTH=500

WINDOWHEIGHT=500

BACKGROUNDCOLOR=(255,255,255)

BLUE=(,,255)

BLACK=(,,)

FPS=40

VHNUMS=3

CELLNUMS=VHNUMS*VHNUMS

MAXRANDTIME=100

# 退出

defterminate():

pygame.quit()

sys.exit()

# 随机生成游戏盘面

defnewGameBoard():

board=[]

foriinrange(CELLNUMS):

board.append(i)

blackCell=CELLNUMS-1

board[blackCell]= -1

foriinrange(MAXRANDTIME):

direction=random.randint(,3)

if(direction==):

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)

returnboard, blackCell

# 若空白图像块不在最左边，则将空白块左边的块移动到空白块位置

defmoveRight(board,blackCell):

ifblackCell%VHNUMS==:

returnblackCell

board[blackCell-1],board[blackCell]=board[blackCell],board[blackCell-1]

returnblackCell-1

# 若空白图像块不在最右边，则将空白块右边的块移动到空白块位置

defmoveLeft(board,blackCell):

ifblackCell%VHNUMS==VHNUMS-1:

returnblackCell

board[blackCell+1],board[blackCell]=board[blackCell],board[blackCell+1]

returnblackCell+1

# 若空白图像块不在最上边，则将空白块上边的块移动到空白块位置

defmoveDown(board,blackCell):

ifblackCell

returnblackCell

board[blackCell-VHNUMS],board[blackCell]=board[blackCell],board[blackCell-VHNUMS]

returnblackCell-VHNUMS

# 若空白图像块不在最下边，则将空白块下边的块移动到空白块位置

defmoveUp(board,blackCell):

ifblackCell>=CELLNUMS-VHNUMS:

returnblackCell

board[blackCell+VHNUMS],board[blackCell]=board[blackCell],board[blackCell+VHNUMS]

returnblackCell+VHNUMS

# 是否完成

defisFinished(board,blackCell):

foriinrange(CELLNUMS-1):

ifboard[i]!=i:

returnFalse

returnTrue

# 初始化

pygame.init()

mainClock=pygame.time.Clock()

# 加载图片

gameRect=gameImage.get_rect()

# 设置窗口

windowSurface=pygame.display.set_mode((gameRect.width, gameRect.height))

pygame.display.set_caption((u'puzzle').encode('utf-8'))

#font = pygame.font.SysFont('楷体', 60)

cellWidth=int(gameRect.width/VHNUMS)

cellHeight=int(gameRect.height/VHNUMS)

finish=False

gameBoard, blackCell=newGameBoard()

# 游戏主循环

whileTrue:

foreventinpygame.event.get():

ifevent.type==QUIT:

terminate()

iffinish:

continue

ifevent.type==KEYDOWN:

ifevent.key==K_LEFTorevent.key==ord('a'):

blackCell=moveLeft(gameBoard, blackCell)

ifevent.key==K_RIGHTorevent.key==ord('d'):

blackCell=moveRight(gameBoard, blackCell)

ifevent.key==K_UPorevent.key==ord('w'):

blackCell=moveUp(gameBoard, blackCell)

ifevent.key==K_DOWNorevent.key==ord('s'):

blackCell=moveDown(gameBoard, blackCell)

ifevent.type==MOUSEBUTTONDOWNandevent.button==1:

x, y=pygame.mouse.get_pos()

col=int(x/cellWidth)

row=int(y/cellHeight)

index=col+row*VHNUMS

if(index==blackCell-1orindex==blackCell+1orindex==blackCell-VHNUMSorindex==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)

foriinrange(CELLNUMS):

rowDst=int(i/VHNUMS)

colDst=int(i%VHNUMS)

rectDst=pygame.Rect(colDst*cellWidth, rowDst*cellHeight, cellWidth, cellHeight)

ifgameBoard[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)

foriinrange(VHNUMS+1):

pygame.draw.line(windowSurface, BLACK, (i*cellWidth,), (i*cellWidth, gameRect.height))

foriinrange(VHNUMS+1):

pygame.draw.line(windowSurface, BLACK, (, i*cellHeight), (gameRect.width, i*cellHeight))

pygame.display.update()

mainClock.tick(FPS)

3897 篇文章242 人订阅

0 条评论

## 相关文章

20510

### 4个理由告诉你Java为何排行第一

Java已经有20年的历史了，甚至更久，而这取决于你所询问的人和你的计算方式。忽略它的年龄不看，Java依然排行第一。它的实用性、性能和向后兼容性都彰显其价值所...

7720

22670

### 帮你提升 Python 的 27 种编程语言

【编者按】本文作者是 Python 的设计者之一 Nick Coghlan，总结了提升 Python 的 27 种编程语言，包括过程式编程、面向对象的数据模型、...

36080

38380

373110

### 领域驱动模型（DDD）

2004年Eric Evans 发表《领域驱动设计——软件核心复杂性应对之道》（Domain-Driven Design –Tackling Complexit...

40810

36660

### 4个理由告诉你Java为何排行第一

Java已经有20年的历史了，甚至更久，而这取决于你所询问的人和你的计算方式。忽略它的年龄不看，Java依然排行第一。它的实用性、性能和向后兼容性都彰显其价值所...

9720

29400