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 调用VC++的动态链接库(DLL)
Sep 06 Python
使用Python解析JSON数据的基本方法
Oct 15 Python
Python自定义主从分布式架构实例分析
Sep 19 Python
python 读文件,然后转化为矩阵的实例
Apr 23 Python
python3实现SMTP发送邮件详细教程
Jun 19 Python
对Python2与Python3中__bool__方法的差异详解
Nov 01 Python
python3使用GUI统计代码量
Sep 18 Python
Python3实现zip分卷压缩过程解析
Oct 09 Python
Python 实现递归法解决迷宫问题的示例代码
Jan 12 Python
Visual Studio code 配置Python开发环境
Sep 11 Python
Python爬虫之Selenium警告框(弹窗)处理
Dec 04 Python
Python爬虫+tkinter界面实现历史天气查询的思路详解
Feb 22 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
什么是短波收听SWL
2021/03/01 无线电
获取用户Ip地址通用方法与常见安全隐患(HTTP_X_FORWARDED_FOR)
2013/06/01 PHP
如何使用Strace调试工具
2013/06/03 PHP
基于PHP后台的Android新闻浏览客户端
2016/05/23 PHP
ThinkPHP中类的构造函数_construct()与_initialize()的区别详解
2017/03/13 PHP
PHP后端银联支付及退款实例代码
2017/06/23 PHP
用javascript实现的激活输入框后隐藏初始内容
2007/06/29 Javascript
javascript中字符串拼接需注意的问题
2010/07/13 Javascript
瀑布流布局并自动加载实现代码
2013/03/12 Javascript
jquery图片放大镜功能的实例代码
2013/03/26 Javascript
jquery动态增加text元素以及删除文本内容实例代码
2013/07/01 Javascript
JS截取url中问号后面参数的值信息
2014/04/29 Javascript
node.js中的url.resolve方法使用说明
2014/12/10 Javascript
AngularJs 60分钟入门基础教程
2016/04/03 Javascript
Node.js学习之查询字符串解析querystring详解
2017/09/28 Javascript
微信小程序之数据缓存的实例详解
2017/09/29 Javascript
js中el表达式的使用和非空判断方法
2018/03/28 Javascript
js异步上传多张图片插件的使用方法
2018/10/22 Javascript
pycharm 使用心得(六)进行简单的数据库管理
2014/06/06 Python
Python中的localtime()方法使用详解
2015/05/22 Python
Python爬虫框架Scrapy实战之批量抓取招聘信息
2015/08/07 Python
python使用opencv进行人脸识别
2017/04/07 Python
matplotlib实现区域颜色填充
2019/03/18 Python
浅谈Python大神都是这样处理XML文件的
2019/05/31 Python
Python-for循环的内部机制
2020/06/12 Python
安踏广告词改编版
2014/03/21 职场文书
合作协议书范本
2014/04/17 职场文书
宾馆仓管员岗位职责
2014/07/27 职场文书
2014年向国旗敬礼活动总结
2014/09/27 职场文书
安全生产标语大全
2014/10/06 职场文书
学校开学标语
2014/10/06 职场文书
新员工考核评语
2014/12/31 职场文书
网上祭英烈活动总结
2015/02/04 职场文书
个人工作表现自我评价
2015/03/06 职场文书
详解MongoDB的条件查询和排序
2021/06/23 MongoDB
Python中基础数据类型 set集合知识点总结
2021/08/02 Python