最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python實現(xiàn)拼圖小游戲

 更新時間:2020年02月22日 10:16:15   作者:彡楠風丶吹  
這篇文章主要為大家詳細介紹了python實現(xiàn)拼圖小游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

Python小白一只,正在成長,程序自己設計,很多不足,算法很多地方能優(yōu)化。歡迎大佬來指教。

游戲效果

創(chuàng)建設置類,儲存游戲基礎數(shù)據(jù)

可以不使用這個類,在程序中直接使用相應的數(shù)據(jù)。但是使用這個類更便于程序閱讀和修改基礎數(shù)據(jù)。

class Settings:
 def __init__(self):
 self.picture_num = 4 # 每行圖片數(shù)
 self.screen_width = 408 # 窗口寬度
 self.screen_length = 809 # 窗口長度
 self.picture_length = 100 # 每個正方形圖片的長
 self.screen_bgcol = (96, 127, 255) # 背景顏色
 self.picture_bian = 1 # 每個圖片的邊緣寬度 ,便于分清每個照片
 self.picture_distance = 102 # 兩個圖片之間的距離

創(chuàng)建圖片類,儲存游戲需要的圖片

這樣可以在游戲的開始把游戲用到的圖片一起讀到內(nèi)存,顯示照片時直接使用創(chuàng)建的圖像對象列表即可。
類的構造函數(shù)要接收一個數(shù)字,按著這個數(shù)字讀生成相應圖片的路徑和名稱 picture_name。在按照這個打開相應的照片。
pygame相應方法可以簡單學習一下。

class Picture:
 def __init__(self, num):
 self.picture_name = 'images/p{}.gif'.format(num)
 self.picture = pygame.image.load(self.picture_name) # 打開照片
 self.picture_rect = self.picture.get_rect() # 獲得照片屬性類
 def display_picture(self, screen, x, y): # 在屏幕上顯示圖片方法
 self.picture_rect.x = x
 self.picture_rect.y = y
 screen.blit(self.picture, self.picture_rect)

生成初始數(shù)據(jù),創(chuàng)建窗口

游戲數(shù)據(jù)用兩個4*4二維列表存儲,一個存儲圖片位置,一個存儲圖片對象。
游戲開始,圖片的順序的應該是亂的。
先要對數(shù)據(jù)進行打亂,打亂時要按照原有的順序打亂,不然可能會出現(xiàn)圖片不可能復原的情況。

數(shù)據(jù)打亂函數(shù)

def data_begin(caozuoshu, p0, data):
 for i in caozuoshu:
 move(i, p0, data)

def move(i, p0, data):
 if i == 3 and p0[1] > 0:
 t = data[p0[0]][p0[1]]
 data[p0[0]][p0[1]] = data[p0[0]][p0[1]-1]
 data[p0[0]][p0[1]-1] = t
 p0[1] -= 1
 elif i == 4 and p0[1] < 3:
 t = data[p0[0]][p0[1]]
 data[p0[0]][p0[1]] = data[p0[0]][p0[1]+1]
 data[p0[0]][p0[1]+1] = t
 p0[1] += 1
 elif i == 1 and p0[0] > 0:
 t = data[p0[0]][p0[1]]
 data[p0[0]][p0[1]] = data[p0[0]-1][p0[1]]
 data[p0[0]-1][p0[1]] = t
 p0[0] -= 1
 elif i == 2 and p0[0] < 3:
 t = data[p0[0]][p0[1]]
 data[p0[0]][p0[1]] = data[p0[0]+1][p0[1]]
 data[p0[0]+1][p0[1]] = t
 p0[0] += 1

def create_caozuoshu():
 n = 30
 caozuo = [1, 2, 3, 4]
 caozuoshu = []
 for i in range(n):
 caozuoshu.append(random.choice(caozuo))
 return caozuoshu

這樣之后,把data列表打亂

在按照data生成picture列表

def create_pictures(picture, data, set):
 for i in range(set.picture_num):
 for j in range(set.picture_num):
  p = Picture(data[i][j])
  picture[i][j] = p

創(chuàng)建窗口函數(shù)

def screen_create(set):
 pygame.init()
 screen = pygame.display.set_mode((set.screen_length, set.screen_width))
 pygame.display.set_caption("拼圖")
 return screen

主函數(shù)

if __name__ == '__main__':
 set = Settings()
 # 初始數(shù)據(jù)
 data = [[9, 1, 3, 4],
  [2, 16, 14, 8],
  [6, 10, 5, 12],
  [13, 7, 11, 15]]
 p0 = [1, 1]
 caozuoshu = create_caozuoshu()
 data_begin(caozuoshu, p0, data)
 bushu = [0]
 # 創(chuàng)建圖片
 picture = [[None, None, None, None],
  [None, None, None, None],
  [None, None, None, None],
  [None, None, None, None]]
 yuantu = Picture(17)
 create_pictures(picture, data, set) # 按照data生成相應順序的picture列表
 # 創(chuàng)建窗口
 screen = screen_create(set)

 # 游戲主循環(huán)
 while True:
 check_events(picture, p0, data, bushu)
 screen_updata(picture, screen, set, yuantu)

響應按鍵控制

響應按鍵是,picture和data列表都要同步改變,data用來判斷是否拼圖完成。

響應按鍵,產(chǎn)生相應的控制

def check_events(picture, p0, data, bushu):
 for event in pygame.event.get():
 if event.type == pygame.QUIT:
  sys.exit()
 elif event.type == pygame.KEYDOWN and game_over(data, set, bushu):
  if event.key == pygame.K_DOWN and p0[0] > 0:
  xinhao = 1
  bushu[0] += 1
  updata(xinhao, picture, p0, data)
  elif event.key == pygame.K_UP and p0[0] < 3:
  xinhao = 2
  bushu[0] += 1
  updata(xinhao, picture, p0, data)
  elif event.key == pygame.K_RIGHT and p0[1] > 0:
  xinhao = 3
  bushu[0] += 1
  updata(xinhao, picture, p0, data)
  elif event.key == pygame.K_LEFT and p0[1] < 3:
  xinhao = 4
  bushu[0] += 1
  updata(xinhao, picture, p0, data)

按照控制數(shù),更新picture和data

def updata(xinhao, picture, p0, data):
 if xinhao == 3:
 tmp = picture[p0[0]][p0[1]]
 picture[p0[0]][p0[1]] = picture[p0[0]][p0[1]-1]
 picture[p0[0]][p0[1]-1] = tmp

 t = data[p0[0]][p0[1]]
 data[p0[0]][p0[1]] = data[p0[0]][p0[1]-1]
 data[p0[0]][p0[1]-1] = t
 p0[1] -= 1

 elif xinhao == 4:
 tmp = picture[p0[0]][p0[1]]
 picture[p0[0]][p0[1]] = picture[p0[0]][p0[1] + 1]
 picture[p0[0]][p0[1] + 1] = tmp

 t = data[p0[0]][p0[1]]
 data[p0[0]][p0[1]] = data[p0[0]][p0[1]+1]
 data[p0[0]][p0[1]+1] = t
 p0[1] += 1
 elif xinhao == 1:
 tmp = picture[p0[0]][p0[1]]
 picture[p0[0]][p0[1]] = picture[p0[0] - 1][p0[1]]
 picture[p0[0] - 1][p0[1]] = tmp

 t = data[p0[0]][p0[1]]
 data[p0[0]][p0[1]] = data[p0[0]-1][p0[1]]
 data[p0[0]-1][p0[1]] = t
 p0[0] -= 1
 elif xinhao == 2:
 tmp = picture[p0[0]][p0[1]]
 picture[p0[0]][p0[1]] = picture[p0[0] + 1][p0[1]]
 picture[p0[0] + 1][p0[1]] = tmp

 t = data[p0[0]][p0[1]]
 data[p0[0]][p0[1]] = data[p0[0] + 1][p0[1]]
 data[p0[0] +1][p0[1]] = t
 p0[0] += 1

判斷是否拼圖完成

def game_over(data, set,bushu):
 datao = [[1, 2, 3, 4],
  [5, 6, 7, 8],
  [9, 10, 11, 12],
  [13, 14, 15, 16]]
 for i in range(set.picture_num):
 for j in range(set.picture_num):
  if datao[i][j] != data[i][j]:
  return True
 print("牛逼!\n 游戲結束!\n 步數(shù):{}".format(bushu[0]))
 return False

此函數(shù)要在響應按鍵函數(shù)中實時使用,監(jiān)測是否完成拼圖。

完整程序

import pygame
import random
import sys

class Settings:
 def __init__(self):
 self.picture_num = 4
 self.screen_width = 408
 self.screen_length = 809
 self.picture_length = 100
 self.screen_bgcol = (96, 127, 255)
 self.picture_speed = 5
 self.picture_bian = 1
 self.picture_distance = 102

class Picture:
 def __init__(self, num):
 self.picture_name = 'images/p{}.gif'.format(num)
 self.picture = pygame.image.load(self.picture_name)
 self.picture_rect = self.picture.get_rect()
 def display_picture(self, screen, x, y):
 self.picture_rect.x = x
 self.picture_rect.y = y
 screen.blit(self.picture, self.picture_rect)
'''def data_begin(data,p0):
 n = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
 ns = 16
 for i in range(4):
 for j in range(4):
  num = random.randint(0, ns-1)
  ns -= 1
  data[i][j] = n.pop(num)
  if data[i][j] == 16:
  p0[0] = i
  p0[1] = j'''
def data_begin(caozuoshu, p0, data):
 for i in caozuoshu:
 move(i, p0, data)

def move(i, p0, data):
 if i == 3 and p0[1] > 0:
 t = data[p0[0]][p0[1]]
 data[p0[0]][p0[1]] = data[p0[0]][p0[1]-1]
 data[p0[0]][p0[1]-1] = t
 p0[1] -= 1
 elif i == 4 and p0[1] < 3:
 t = data[p0[0]][p0[1]]
 data[p0[0]][p0[1]] = data[p0[0]][p0[1]+1]
 data[p0[0]][p0[1]+1] = t
 p0[1] += 1
 elif i == 1 and p0[0] > 0:
 t = data[p0[0]][p0[1]]
 data[p0[0]][p0[1]] = data[p0[0]-1][p0[1]]
 data[p0[0]-1][p0[1]] = t
 p0[0] -= 1
 elif i == 2 and p0[0] < 3:
 t = data[p0[0]][p0[1]]
 data[p0[0]][p0[1]] = data[p0[0]+1][p0[1]]
 data[p0[0]+1][p0[1]] = t
 p0[0] += 1

def create_caozuoshu():
 n = 30
 caozuo = [1, 2, 3, 4]
 caozuoshu = []
 for i in range(n):
 caozuoshu.append(random.choice(caozuo))
 return caozuoshu

def create_pictures(picture, data, set):
 for i in range(set.picture_num):
 for j in range(set.picture_num):
  p = Picture(data[i][j])
  picture[i][j] = p

def screen_updata(picture, screen, set, yuantu):
 screen.fill(set.screen_bgcol)
 x, y = 402, set.picture_bian
 for i in range(set.picture_num):
 for j in range(set.picture_num):
  picture[i][j].display_picture(screen, x, y)
  x += set.picture_distance
 x = 402
 y += set.picture_distance
 yuantu.display_picture(screen, 1, 4)
 pygame.display.flip()

def screen_create(set):
 pygame.init()
 screen = pygame.display.set_mode((set.screen_length, set.screen_width))
 pygame.display.set_caption("拼圖")
 return screen

def game_over(data, set,bushu):
 datao = [[1, 2, 3, 4],
  [5, 6, 7, 8],
  [9, 10, 11, 12],
  [13, 14, 15, 16]]
 for i in range(set.picture_num):
 for j in range(set.picture_num):
  if datao[i][j] != data[i][j]:
  return True
 print("牛逼!\n 游戲結束!\n 步數(shù):{}".format(bushu[0]))
 return False

def updata(xinhao, picture, p0, data):
 if xinhao == 3:
 tmp = picture[p0[0]][p0[1]]
 picture[p0[0]][p0[1]] = picture[p0[0]][p0[1]-1]
 picture[p0[0]][p0[1]-1] = tmp

 t = data[p0[0]][p0[1]]
 data[p0[0]][p0[1]] = data[p0[0]][p0[1]-1]
 data[p0[0]][p0[1]-1] = t
 p0[1] -= 1

 elif xinhao == 4:
 tmp = picture[p0[0]][p0[1]]
 picture[p0[0]][p0[1]] = picture[p0[0]][p0[1] + 1]
 picture[p0[0]][p0[1] + 1] = tmp

 t = data[p0[0]][p0[1]]
 data[p0[0]][p0[1]] = data[p0[0]][p0[1]+1]
 data[p0[0]][p0[1]+1] = t
 p0[1] += 1
 elif xinhao == 1:
 tmp = picture[p0[0]][p0[1]]
 picture[p0[0]][p0[1]] = picture[p0[0] - 1][p0[1]]
 picture[p0[0] - 1][p0[1]] = tmp

 t = data[p0[0]][p0[1]]
 data[p0[0]][p0[1]] = data[p0[0]-1][p0[1]]
 data[p0[0]-1][p0[1]] = t
 p0[0] -= 1
 elif xinhao == 2:
 tmp = picture[p0[0]][p0[1]]
 picture[p0[0]][p0[1]] = picture[p0[0] + 1][p0[1]]
 picture[p0[0] + 1][p0[1]] = tmp

 t = data[p0[0]][p0[1]]
 data[p0[0]][p0[1]] = data[p0[0] + 1][p0[1]]
 data[p0[0] +1][p0[1]] = t
 p0[0] += 1
 #print(data)

def check_events(picture, p0, data, bushu):
 for event in pygame.event.get():
 if event.type == pygame.QUIT:
  sys.exit()
 elif event.type == pygame.KEYDOWN and game_over(data, set, bushu):
  if event.key == pygame.K_DOWN and p0[0] > 0:
  xinhao = 1
  bushu[0] += 1
  updata(xinhao, picture, p0, data)
  elif event.key == pygame.K_UP and p0[0] < 3:
  xinhao = 2
  bushu[0] += 1
  updata(xinhao, picture, p0, data)
  elif event.key == pygame.K_RIGHT and p0[1] > 0:
  xinhao = 3
  bushu[0] += 1
  updata(xinhao, picture, p0, data)
  elif event.key == pygame.K_LEFT and p0[1] < 3:
  xinhao = 4
  bushu[0] += 1
  updata(xinhao, picture, p0, data)

if __name__ == '__main__':
 set = Settings()
 # 初始數(shù)據(jù)
 data = [[9, 1, 3, 4],
  [2, 16, 14, 8],
  [6, 10, 5, 12],
  [13, 7, 11, 15]]
 p0 = [1, 1]
 caozuoshu = create_caozuoshu()
 data_begin(caozuoshu, p0, data)
 bushu = [0]
 # 創(chuàng)建圖片
 picture = [[None, None, None, None],
  [None, None, None, None],
  [None, None, None, None],
  [None, None, None, None]]
 yuantu = Picture(17)
 create_pictures(picture, data, set)
 # 創(chuàng)建窗口
 screen = screen_create(set)

 # 游戲主循環(huán)
 while True:
 check_events(picture, p0, data, bushu)
 screen_updata(picture, screen, set, yuantu)

游戲用到的圖片,圖片位置和文件名要和程序中的一致

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • tensorflow中tf.reduce_mean函數(shù)的使用

    tensorflow中tf.reduce_mean函數(shù)的使用

    這篇文章主要介紹了tensorflow中tf.reduce_mean函數(shù)的使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-04-04
  • Python對口紅進行數(shù)據(jù)分析來選定情人節(jié)禮物

    Python對口紅進行數(shù)據(jù)分析來選定情人節(jié)禮物

    情人節(jié)送小仙女什么禮物?讓我們來用Python對口紅進行數(shù)據(jù)分析,那個女孩子會拒絕這樣精心挑選的禮物,感興趣的小伙伴快來看看吧
    2022-02-02
  • Windows下Sqlmap環(huán)境安裝教程詳解

    Windows下Sqlmap環(huán)境安裝教程詳解

    這篇文章主要介紹了Windows下Sqlmap環(huán)境安裝,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-08-08
  • 通過pycharm使用git的步驟(圖文詳解)

    通過pycharm使用git的步驟(圖文詳解)

    這篇文章主要介紹了通過pycharm使用git的步驟(圖文詳解),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-06-06
  • Python機器學習NLP自然語言處理基本操作詞袋模型

    Python機器學習NLP自然語言處理基本操作詞袋模型

    本文是Python機器學習NLP自然語言處理系列文章,帶大家開啟一段學習自然語言處理 (NLP) 的旅程。本篇文章主要學習NLP自然語言處理基本操作之詞袋模型
    2021-09-09
  • windows上安裝Anaconda和python的教程詳解

    windows上安裝Anaconda和python的教程詳解

    本文主要給大家介紹windows上安裝Anaconda和python的教程詳解,非常不錯,具有參考借鑒價值,需要的朋友參考下
    2017-03-03
  • Python中如何使用sqlite3操作SQLite數(shù)據(jù)庫詳解

    Python中如何使用sqlite3操作SQLite數(shù)據(jù)庫詳解

    這篇文章主要介紹了Python中SQLite數(shù)據(jù)庫的使用,包括連接數(shù)據(jù)庫、創(chuàng)建表、數(shù)據(jù)增刪改查、事務管理和參數(shù)化查詢等,并提供了操作示例,需要的朋友可以參考下
    2025-03-03
  • 使用python-cv2實現(xiàn)視頻的分解與合成的示例代碼

    使用python-cv2實現(xiàn)視頻的分解與合成的示例代碼

    這篇文章主要介紹了使用python-cv2實現(xiàn)視頻的分解與合成的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-10-10
  • 利用python爬取m3u8格式視頻的具體實現(xiàn)

    利用python爬取m3u8格式視頻的具體實現(xiàn)

    之前爬取的視頻都是mp4格式的,直接用requests請求就可以直接爬取,最近公司安排了一個小任務,需要爬取m3u8這種格式的視頻,下面這篇文章主要給大家介紹了關于利用python爬取m3u8格式視頻的相關資料,需要的朋友可以參考下
    2022-08-08
  • Python Series從0開始索引的方法

    Python Series從0開始索引的方法

    今天小編就為大家分享一篇Python Series從0開始索引的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-11-11

最新評論

福建省| 汽车| 赤水市| 罗山县| 固阳县| 建德市| 罗甸县| 濮阳县| 冀州市| 永州市| 秭归县| 广宗县| 凭祥市| 绥中县| 渝北区| 白山市| 丹东市| 潞城市| 自治县| 德格县| 神农架林区| 东城区| 离岛区| 兴义市| 商丘市| 修文县| 双城市| 德钦县| 濮阳市| 隆德县| 波密县| 华蓥市| 哈尔滨市| 兰西县| 阿拉善左旗| 东城区| 沾化县| 荥经县| 调兵山市| 海淀区| 巫溪县|