国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python加pyGame實(shí)現(xiàn)的簡單拼圖游戲?qū)嵗?/h1>
2020-02-23 01:14:07
字體:
供稿:網(wǎng)友

本文實(shí)例講述了Python加pyGame實(shí)現(xiàn)的簡單拼圖游戲。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:

import pygame, sys, randomfrom pygame.locals import *# 一些常量WINDOWWIDTH = 500WINDOWHEIGHT = 500BACKGROUNDCOLOR = (255, 255, 255)BLUE = (0, 0, 255)BLACK = (0, 0, 0)FPS = 40VHNUMS = 3CELLNUMS = VHNUMS*VHNUMSMAXRANDTIME = 100# 退出def terminate():  pygame.quit()  sys.exit()# 隨機(jī)生成游戲盤面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()# 設(shè)置窗口windowSurface = pygame.display.set_mode((gameRect.width, gameRect.height))pygame.display.set_caption('拼圖')cellWidth = int(gameRect.width / VHNUMS)cellHeight = int(gameRect.height / VHNUMS)finish = FalsegameBoard, blackCell = newGameBoard()# 游戲主循環(huán)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)            
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

主站蜘蛛池模板: 河北区| 肥城市| 德阳市| 屏东县| 东安县| 额尔古纳市| 独山县| 安顺市| 合山市| 嘉义市| 景德镇市| 阿合奇县| 三亚市| 沙田区| 东乌| 秦安县| 搜索| 青铜峡市| 贵南县| 龙川县| 兴和县| 罗甸县| 玉树县| 巴里| 吴江市| 大化| 通化市| 申扎县| 鲁山县| 宜州市| 迁安市| 武乡县| 福建省| 万载县| 星子县| 故城县| 深水埗区| 灌云县| 南开区| 滨州市| 中宁县|