本文实例讲述了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程序设计有所帮助。

广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。