圆月山庄资源网 Design By www.vgjia.com
本文实例讲述了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程序设计有所帮助。
圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com
暂无评论...
更新日志
2024年11月08日
2024年11月08日
- 雨林唱片《赏》新曲+精选集SACD版[ISO][2.3G]
- 罗大佑与OK男女合唱团.1995-再会吧!素兰【音乐工厂】【WAV+CUE】
- 草蜢.1993-宝贝对不起(国)【宝丽金】【WAV+CUE】
- 杨培安.2009-抒·情(EP)【擎天娱乐】【WAV+CUE】
- 周慧敏《EndlessDream》[WAV+CUE]
- 彭芳《纯色角3》2007[WAV+CUE]
- 江志丰2008-今生为你[豪记][WAV+CUE]
- 罗大佑1994《恋曲2000》音乐工厂[WAV+CUE][1G]
- 群星《一首歌一个故事》赵英俊某些作品重唱企划[FLAC分轨][1G]
- 群星《网易云英文歌曲播放量TOP100》[MP3][1G]
- 方大同.2024-梦想家TheDreamer【赋音乐】【FLAC分轨】
- 李慧珍.2007-爱死了【华谊兄弟】【WAV+CUE】
- 王大文.2019-国际太空站【环球】【FLAC分轨】
- 群星《2022超好听的十倍音质网络歌曲(163)》U盘音乐[WAV分轨][1.1G]
- 童丽《啼笑姻缘》头版限量编号24K金碟[低速原抓WAV+CUE][1.1G]