python加pygame实现的简单拼图游戏实例

本文实例讲述了python加pygame实现的简单拼图游戏。分享给大家供大家参考。具体实现方法如下:

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程序设计有所帮助。

Posted in 未分类

发表评论