欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python加pyGame實(shí)現(xiàn)的簡單拼圖游戲?qū)嵗?/h1>
 更新時間:2015年05月15日 15:02:15   作者:songguo  
這篇文章主要介紹了Python加pyGame實(shí)現(xiàn)的簡單拼圖游戲,以一個完整實(shí)例形式分析了pyGame模塊操作圖片的相關(guān)技巧,需要的朋友可以參考下

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

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()
# 隨機(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 = False
gameBoard, 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)

希望本文所述對大家的Python程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • 基于Python實(shí)現(xiàn)模擬三體運(yùn)動的示例代碼

    基于Python實(shí)現(xiàn)模擬三體運(yùn)動的示例代碼

    此前所做的一切三體和太陽系的動畫,都是基于牛頓力學(xué)的,而且直接對微分進(jìn)行差分化,從而精度非常感人,用不了幾年就得撞一起去。所以本文來用Python重新模擬一下三體運(yùn)動,感興趣的可以了解一下
    2023-03-03
  • python的簡單四則運(yùn)算語法樹可視化

    python的簡單四則運(yùn)算語法樹可視化

    這篇文章主要介紹了python的簡單四則運(yùn)算語法樹可視化,這篇文章的內(nèi)容也很簡單,就是給定一個四則運(yùn)算的表達(dá)式,畫出它的語法樹,需要的朋友可以參考下
    2023-04-04
  • Python掃描IP段查看指定端口是否開放的方法

    Python掃描IP段查看指定端口是否開放的方法

    這篇文章主要介紹了Python掃描IP段查看指定端口是否開放的方法,涉及Python使用socket模塊實(shí)現(xiàn)端口掃描功能的相關(guān)技巧,需要的朋友可以參考下
    2015-06-06
  • PyQt5 多窗口連接實(shí)例

    PyQt5 多窗口連接實(shí)例

    今天小編就為大家分享一篇PyQt5 多窗口連接實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-06-06
  • Python數(shù)據(jù)分析之?Matplotlib?折線圖繪制

    Python數(shù)據(jù)分析之?Matplotlib?折線圖繪制

    這篇文章主要介紹了Python數(shù)據(jù)分析之?Matplotlib?折線圖繪制,在數(shù)據(jù)分析中,數(shù)據(jù)可視化也非常重要,下文通過數(shù)據(jù)分析展開對折線圖的繪制,需要的小伙伴可以參考一下
    2022-05-05
  • Python讀取mat文件,并轉(zhuǎn)為csv文件的實(shí)例

    Python讀取mat文件,并轉(zhuǎn)為csv文件的實(shí)例

    今天小編就為大家分享一篇Python讀取mat文件,并轉(zhuǎn)為csv文件的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • 利用PyTorch實(shí)現(xiàn)爬山算法

    利用PyTorch實(shí)現(xiàn)爬山算法

    這篇文章主要介紹了利用PyTorch實(shí)現(xiàn)爬山算法,爬山算法是一種局部擇優(yōu)的方法,采用啟發(fā)式方法,是對深度優(yōu)先搜索的一種改進(jìn),它利用反饋信息幫助生成解的決策,屬于人工智能算法的一種
    2022-07-07
  • python自動化神器pyautogui使用步驟

    python自動化神器pyautogui使用步驟

    這篇文章主要給大家介紹了關(guān)于python自動化神器pyautogui使用步驟的相關(guān)資料,在Python當(dāng)中不僅代碼簡單,而且有著非常豐富的模塊,pyautogui就可以稱之為自動化操作的"神器",需要的朋友可以參考下
    2023-07-07
  • 基于python讀取圖像的幾種方式匯總

    基于python讀取圖像的幾種方式匯總

    Python進(jìn)行圖片處理,第一步就是讀取圖片,下面這篇文章主要給大家介紹了關(guān)于基于python讀取圖像的幾種方式的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-06-06
  • python實(shí)現(xiàn)數(shù)據(jù)清洗(缺失值與異常值處理)

    python實(shí)現(xiàn)數(shù)據(jù)清洗(缺失值與異常值處理)

    今天小編就為大家分享一篇python實(shí)現(xiàn)數(shù)據(jù)清洗(缺失值與異常值處理),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12

最新評論