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提示[Errno 32]Broken pipe导致线程crash错误解决方法
Nov 19 Python
python实现简单的计时器功能函数
Mar 14 Python
Python创建二维数组实例(关于list的一个小坑)
Nov 07 Python
Python3多线程爬虫实例讲解代码
Jan 05 Python
python方法生成txt标签文件的实例代码
May 10 Python
Python字典中的键映射多个值的方法(列表或者集合)
Oct 17 Python
Python实现一个数组除以一个数的例子
Jul 20 Python
postman传递当前时间戳实例详解
Sep 14 Python
Django 项目布局方法(值得推荐)
Mar 22 Python
Windows+Anaconda3+PyTorch+PyCharm的安装教程图文详解
Apr 03 Python
解决python对齐错误的方法
Jul 16 Python
如何利用opencv判断两张图片是否相同详解
Jul 07 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
解析如何在PHP下载文件名中解决乱码的问题
2013/06/20 PHP
解析php dirname()与__FILE__常量的应用
2013/06/24 PHP
PHP数据库表操作的封装类及用法实例详解
2016/07/12 PHP
laravel项目利用twemproxy部署redis集群的完整步骤
2018/05/11 PHP
CodeIgniter框架数据库基本操作示例
2018/05/24 PHP
js 编写规范
2010/03/03 Javascript
jQuery 核心函数以及jQuery对象
2010/03/23 Javascript
基于jQuery实现模拟页面加载进度条
2013/04/01 Javascript
时间戳转换为时间 年月日时间的JS函数
2013/08/19 Javascript
js实现汉字排序的方法
2015/07/23 Javascript
解决给dom元素绑定click等事件无效问题的方法
2017/02/17 Javascript
jQuery输入框密码的显示隐藏【代码分享】
2017/04/29 jQuery
requirejs按需加载angularjs文件实例
2017/06/08 Javascript
jQuery Layer弹出层传值到父页面的实现代码
2017/08/17 jQuery
vue 实现全选全不选的示例代码
2018/03/29 Javascript
Vue + Node.js + MongoDB图片上传组件实现图片预览和删除功能详解
2020/04/29 Javascript
vue中keep-alive、activated的探讨和使用详解
2020/07/26 Javascript
[03:10]超级美酒第四天 fy拉比克秀 大合集
2018/06/05 DOTA
[30:55]完美世界DOTA2联赛PWL S2 Magma vs LBZS 第二场 11.18
2020/11/18 DOTA
Python的Flask框架中的Jinja2模板引擎学习教程
2016/06/30 Python
Python自定义装饰器原理与用法实例分析
2018/07/16 Python
OpenCV+python手势识别框架和实例讲解
2018/08/03 Python
Python连接Oracle之环境配置、实例代码及报错解决方法详解
2020/02/11 Python
python读写文件write和flush的实现方式
2020/02/21 Python
SpringBoot实现登录注册常见问题解决方案
2020/03/04 Python
python 两个一样的字符串用==结果为false问题的解决
2020/03/12 Python
python语言是免费还是收费的?
2020/06/15 Python
python中如何写类
2020/06/29 Python
Python3中对json格式数据的分析处理
2021/01/28 Python
建筑工程专业学生的自我评价
2013/12/25 职场文书
大学毕业感言
2014/01/10 职场文书
卫校中专生的自我评价
2014/01/15 职场文书
社区学习十八大感想
2014/01/22 职场文书
关于圣诞节的广播稿
2014/01/26 职场文书
客服部班长工作责任制
2014/02/25 职场文书
2019XX公司员工考核管理制度!
2019/08/07 职场文书