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 list语法学习(带例子)
Nov 01 Python
跟老齐学Python之坑爹的字符编码
Sep 28 Python
零基础写python爬虫之爬虫的定义及URL构成
Nov 04 Python
Python SQLite3数据库日期与时间常见函数用法分析
Aug 14 Python
python递归函数绘制分形树的方法
Jun 22 Python
tensorflow 用矩阵运算替换for循环 用tf.tile而不写for的方法
Jul 27 Python
python实现图片转字符小工具
Apr 30 Python
numpy下的flatten()函数用法详解
May 27 Python
python文件转为exe文件的方法及用法详解
Jul 08 Python
使用django的objects.filter()方法匹配多个关键字的方法
Jul 18 Python
Python爬虫工具requests-html使用解析
Apr 29 Python
Pycharm plot独立窗口显示的操作
Dec 11 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下实现农历日历的代码
2007/03/07 PHP
php使用Jpgraph绘制复杂X-Y坐标图的方法
2015/06/10 PHP
laravel 中如何使用ajax和vue总结
2017/08/16 PHP
Laravel ORM 数据model操作教程
2019/10/21 PHP
jquery+json 通用三级联动下拉列表
2010/04/19 Javascript
JavaScript 设计模式 安全沙箱模式
2010/09/24 Javascript
javascript中方便增删改cookie的一个类
2012/10/11 Javascript
Javascript弹出窗口的各种方法总结
2013/11/11 Javascript
js控制表单不能输入空格的小例子
2013/11/20 Javascript
给html超链接设置事件不使用href来完成跳
2014/04/20 Javascript
推荐阅读的js快速判断IE浏览器(兼容IE10与IE11)
2015/12/13 Javascript
基于javascript html5实现多文件上传
2016/03/03 Javascript
jQuery根据ID、CLASS、等获取对象的实例
2016/12/04 Javascript
vue监听滚动事件实现滚动监听
2017/04/11 Javascript
简单实现js点击展开二级菜单功能
2017/05/16 Javascript
bootstrap表单示例代码分享
2017/05/18 Javascript
在 webpack 中使用 ECharts的实例详解
2018/02/05 Javascript
JS 数组随机洗牌的实例代码
2018/09/12 Javascript
Vue实现菜单切换功能
2020/11/08 Javascript
[03:04]DOTA2超级联赛专访ZSMJ “莫名其妙”的逆袭
2013/05/23 DOTA
跟老齐学Python之??碌某?? target=
2014/09/12 Python
Python中不同进制的语法及转换方法分析
2016/07/27 Python
django 使用全局搜索功能的实例详解
2019/07/18 Python
Django使用Channels实现WebSocket的方法
2019/07/28 Python
Python爬虫scrapy框架Cookie池(微博Cookie池)的使用
2021/01/13 Python
css3的transition属性详解
2014/12/15 HTML / CSS
The Kooples美国官方网站:为情侣提供的法国当代时尚品牌
2019/01/03 全球购物
哄娃神器4moms商店:美国婴童用品品牌
2019/03/07 全球购物
夜大毕业生自我评价分享
2013/11/10 职场文书
公务员培训心得体会
2013/12/28 职场文书
宿舍使用违章电器检讨书
2014/01/12 职场文书
红旗方阵解说词
2014/02/12 职场文书
房产协议书范本
2014/10/18 职场文书
文明旅游倡议书
2015/04/28 职场文书
2015年会计人员工作总结
2015/05/22 职场文书
导游词之唐山景点
2019/12/18 职场文书