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

作者:songguo 时间:2021-12-20 04:31:45 

本文实例讲述了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,pyGame,游戏
0
投稿

猜你喜欢

  • Python的命令行参数实例详解

    2023-06-11 09:05:45
  • 详细聊聊为什么Python中0.2+0.1不等于0.3

    2021-08-19 12:12:35
  • python模块中判断全局变量的赋值的实例讲解

    2021-01-05 13:29:37
  • python重试装饰器的简单实现方法

    2022-07-15 12:54:10
  • 自定义404错误页面实现自动跳转

    2007-12-10 18:25:00
  • Python字符串字母大小写转换的各种情况详析

    2023-11-23 07:35:41
  • python+requests接口自动化框架的实现

    2022-11-25 04:03:40
  • 详解Django关于StreamingHttpResponse与FileResponse文件下载的最优方法

    2021-04-13 08:17:01
  • oracle中添加删除主键的方法

    2023-06-30 00:28:52
  • 9种使用Chrome Firefox 自带调试工具调试javascript技巧

    2023-07-19 01:03:48
  • Oracle10g 安装方法

    2009-06-19 17:50:00
  • 2008年10佳改版网站

    2008-09-22 20:15:00
  • 使用PyCharm创建Django项目及基本配置详解

    2021-03-31 10:51:36
  • ASP 使用三层架构 asp中使用类

    2011-03-16 10:52:00
  • PHP判断密码强度的方法详解

    2023-06-14 03:00:08
  • keras实现调用自己训练的模型,并去掉全连接层

    2023-08-10 16:34:21
  • 网页颜色变黑白CSS代码 适合所有类型网站

    2010-04-21 10:37:00
  • DreamweaverMX2004的一句话技巧

    2009-05-22 18:23:00
  • Python操作word文档插入图片和表格的实例演示

    2023-09-20 08:21:09
  • 利用SQL Server复制技术实现数据同步更新

    2009-10-23 14:11:00
  • asp之家 网络编程 m.aspxhome.com