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

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

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

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

游戲效果

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

可以不使用這個類,在程序中直接使用相應(yīng)的數(shù)據(jù)。但是使用這個類更便于程序閱讀和修改基礎(chǔ)數(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)建的圖像對象列表即可。
類的構(gòu)造函數(shù)要接收一個數(shù)字,按著這個數(shù)字讀生成相應(yīng)圖片的路徑和名稱 picture_name。在按照這個打開相應(yīng)的照片。
pygame相應(yīng)方法可以簡單學(xué)習(xí)一下。

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二維列表存儲,一個存儲圖片位置,一個存儲圖片對象。
游戲開始,圖片的順序的應(yīng)該是亂的。
先要對數(shù)據(jù)進行打亂,打亂時要按照原有的順序打亂,不然可能會出現(xiàn)圖片不可能復(fù)原的情況。

數(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生成相應(yīng)順序的picture列表
 # 創(chuàng)建窗口
 screen = screen_create(set)

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

響應(yīng)按鍵控制

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

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

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 游戲結(jié)束!\n 步數(shù):{}".format(bushu[0]))
 return False

此函數(shù)要在響應(yīng)按鍵函數(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 游戲結(jié)束!\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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Numpy之random函數(shù)使用學(xué)習(xí)

    Numpy之random函數(shù)使用學(xué)習(xí)

    這篇文章主要介紹了Numpy之random使用學(xué)習(xí),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-01-01
  • Python 線程池用法簡單示例

    Python 線程池用法簡單示例

    這篇文章主要介紹了Python 線程池用法,結(jié)合簡單實例形式分析了Python線程池相關(guān)使用技巧與操作注意事項,需要的朋友可以參考下
    2019-10-10
  • Caffe卷積神經(jīng)網(wǎng)絡(luò)solver及其配置詳解

    Caffe卷積神經(jīng)網(wǎng)絡(luò)solver及其配置詳解

    這篇文章主要為大家介紹了Caffe卷積神經(jīng)網(wǎng)絡(luò)solver及其配置詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-06-06
  • 基于Python實現(xiàn)拉格朗日插值法

    基于Python實現(xiàn)拉格朗日插值法

    拉格朗日插值法是以法國十八世紀(jì)數(shù)學(xué)家約瑟夫·拉格朗日命名的一種多項式插值方法。本文將利用Python語言實現(xiàn)這一插值法,需要的可以參考一下
    2022-12-12
  • 通過實例解析Python文件操作實現(xiàn)步驟

    通過實例解析Python文件操作實現(xiàn)步驟

    這篇文章主要介紹了通過實例解析Python文件操作實現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-09-09
  • Python實現(xiàn)文件信息進行合并實例代碼

    Python實現(xiàn)文件信息進行合并實例代碼

    這篇文章主要介紹了Python實現(xiàn)文件信息進行合并實例代碼,具有一定借鑒價值,需要的朋友可以參考下
    2018-01-01
  • PyQt5 QDockWidget控件應(yīng)用詳解

    PyQt5 QDockWidget控件應(yīng)用詳解

    這篇文章主要介紹了PyQt5 QDockWidget控件應(yīng)用詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • Python+Pyecharts實現(xiàn)散點圖的繪制

    Python+Pyecharts實現(xiàn)散點圖的繪制

    散點圖是指在回歸分析中,數(shù)據(jù)點在直角坐標(biāo)系平面上的分布圖,散點圖表示因變量隨自變量而變化的大致趨勢,據(jù)此可以選擇合適的函數(shù)對數(shù)據(jù)點進行擬合。本文將利用Python Pyecharts實現(xiàn)散點圖的繪制,需要的可以參考一下
    2022-06-06
  • python爬蟲入門教程--正則表達(dá)式完全指南(五)

    python爬蟲入門教程--正則表達(dá)式完全指南(五)

    要想做爬蟲,不可避免的要用到正則表達(dá)式,如果是簡單的字符串處理,類似于split,substring等等就足夠了,可是涉及到比較復(fù)雜的匹配,當(dāng)然是正則的天下,下面這篇文章主要給大家介紹了python爬蟲之正則表達(dá)式的相關(guān)資料,需要的朋友可以參考下。
    2017-05-05
  • 基于Python實現(xiàn)人臉自動戴口罩系統(tǒng)

    基于Python實現(xiàn)人臉自動戴口罩系統(tǒng)

    2019年新型冠狀病毒感染的肺炎疫情發(fā)生以來,牽動人心,舉國哀痛,口罩、酒精、消毒液奇貨可居。這篇文章主要介紹了基于Python的人臉自動戴口罩系統(tǒng),需要的朋友可以參考下
    2020-02-02

最新評論