python實現(xiàn)拼圖小游戲
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)文章
tensorflow中tf.reduce_mean函數(shù)的使用
這篇文章主要介紹了tensorflow中tf.reduce_mean函數(shù)的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
Python對口紅進行數(shù)據(jù)分析來選定情人節(jié)禮物
情人節(jié)送小仙女什么禮物?讓我們來用Python對口紅進行數(shù)據(jù)分析,那個女孩子會拒絕這樣精心挑選的禮物,感興趣的小伙伴快來看看吧2022-02-02
Python機器學(xué)習(xí)NLP自然語言處理基本操作詞袋模型
本文是Python機器學(xué)習(xí)NLP自然語言處理系列文章,帶大家開啟一段學(xué)習(xí)自然語言處理 (NLP) 的旅程。本篇文章主要學(xué)習(xí)NLP自然語言處理基本操作之詞袋模型2021-09-09
windows上安裝Anaconda和python的教程詳解
本文主要給大家介紹windows上安裝Anaconda和python的教程詳解,非常不錯,具有參考借鑒價值,需要的朋友參考下2017-03-03
Python中如何使用sqlite3操作SQLite數(shù)據(jù)庫詳解
這篇文章主要介紹了Python中SQLite數(shù)據(jù)庫的使用,包括連接數(shù)據(jù)庫、創(chuàng)建表、數(shù)據(jù)增刪改查、事務(wù)管理和參數(shù)化查詢等,并提供了操作示例,需要的朋友可以參考下2025-03-03
使用python-cv2實現(xiàn)視頻的分解與合成的示例代碼
這篇文章主要介紹了使用python-cv2實現(xiàn)視頻的分解與合成的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10

