Python加pyGame实现的简单拼图游戏实例


Posted in Python onMay 15, 2015

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

Python 相关文章推荐
分析用Python脚本关闭文件操作的机制
Jun 28 Python
详解Django中Request对象的相关用法
Jul 17 Python
Python中的字符串操作和编码Unicode详解
Jan 18 Python
Python编程pygal绘图实例之XY线
Dec 09 Python
Python爬取附近餐馆信息代码示例
Dec 09 Python
Pycharm在创建py文件时,自动添加文件头注释的实例
May 07 Python
在Pycharm terminal中字体大小设置的方法
Jan 16 Python
Python判断字符串是否xx开始或结尾的示例
Aug 08 Python
python实现两个文件夹的同步
Aug 29 Python
Python基于class()实现面向对象原理详解
Mar 26 Python
Pytest实现setup和teardown的详细使用详解
Apr 17 Python
Python基础之进程详解
May 21 Python
Python实现从URL地址提取文件名的方法
May 15 #Python
Python基础入门之seed()方法的使用
May 15 #Python
Python中的random()方法的使用介绍
May 15 #Python
Python的randrange()方法使用教程
May 15 #Python
Python中的choice()方法使用详解
May 15 #Python
Python中利用sqrt()方法进行平方根计算的教程
May 15 #Python
简单介绍Python中的round()方法
May 15 #Python
You might like
967 个函式
2006/10/09 PHP
PHP nl2br函数 将换行字符转成 &amp;lt;br&amp;gt;
2009/08/21 PHP
解析在PHP中使用mysqli扩展库对mysql的操作
2013/07/03 PHP
PHP检测移动设备类mobile detection使用实例
2014/04/14 PHP
php生成图片缩略图功能示例
2017/02/22 PHP
JavaScript表达式:URL 协议介绍
2013/03/10 Javascript
解析JavaScript中的不可见数据类型
2013/12/02 Javascript
Jquery 切换不同图片示例代码
2013/12/05 Javascript
vuejs手把手教你写一个完整的购物车实例代码
2017/07/06 Javascript
Js利用console计算代码运行时间的方法示例
2017/09/24 Javascript
vue组件父子间通信之综合练习(聊天室)
2017/11/07 Javascript
Angular 开发学习之Angular CLI的安装使用
2017/12/31 Javascript
Node.js原生api搭建web服务器的方法步骤
2019/02/15 Javascript
AjaxFileUpload.js实现异步上传文件功能
2019/04/19 Javascript
解决vue项目刷新后,导航菜单高亮显示的位置不对问题
2019/11/01 Javascript
npx create-react-app xxx创建项目报错的解决办法
2020/02/17 Javascript
解决vue-loader加载不上的问题
2020/10/21 Javascript
Python中利用Scipy包的SIFT方法进行图片识别的实例教程
2016/06/03 Python
Python中音频处理库pydub的使用教程
2017/06/07 Python
Python 中Pickle库的使用详解
2018/02/24 Python
一篇文章读懂Python赋值与拷贝
2018/04/19 Python
python 查找文件名包含指定字符串的方法
2018/06/05 Python
Python 串口读写的实现方法
2019/06/12 Python
树莓派使用USB摄像头和motion实现监控
2019/06/22 Python
python列表推导和生成器表达式知识点总结
2020/01/10 Python
python中绕过反爬虫的方法总结
2020/11/25 Python
css3中background新增的4个新的相关属性用法介绍
2013/09/26 HTML / CSS
英国休闲奢华的缩影:Crew Clothing
2019/05/05 全球购物
医学护理毕业生自荐信
2013/11/07 职场文书
《鱼游到了纸上》教学反思
2014/02/20 职场文书
能源工程专业应届生求职信
2014/03/01 职场文书
工艺工程师岗位职责
2014/03/04 职场文书
2014年预备党员学习两会心得体会
2014/03/17 职场文书
个人查摆问题整改措施
2014/10/04 职场文书
2015年度物流工作总结
2015/04/30 职场文书
欠条范文
2015/07/03 职场文书