基于python做一個2048小游戲
邏輯設計
2048的邏輯無非是操作4 × 4 的方格,每個方格中有一個數,這些數可以移動,如果兩個相同的數字在移動時相撞了,就可以彼此合并。
而這個4 × 4的方格,無非是一個矩陣。只需設計好移動邏輯,再用PyGame將這個方格表現出來就算大功告成。
2048只有四個手勢動作,即上下左右,這四個動作所引發(fā)的結果都可以歸結為對單行或者單列的操作,進而歸結為對一個列表的操作。
首先,對列表進行操作時,先排除0的影響,在排除0的影響之后,若相鄰元素相等,即可合并。例如對于[ 0 , 2 , 2 , 0 ],如果向左合并則輸出為[ 4 , 0 , 0 , 0 ]。
def mergeLst(lst):
lst = [x for x in lst if x] # 刪除lst中的0
newLst = []
N, i = len(lst), 0
while i < N:
if i<N-1 and lst[i] == lst[i+1]:
newLst.append(lst[i]*2)
i += 2
else:
newLst.append(lst[i])
i += 1
return newLst + (4-len(newLst))*[0]
測試如下
mergeLst([0,2,2,0]) # [4, 0, 0, 0] mergeLst([2,2,2,2]) # [4, 4, 0, 0] mergeLst([2,2,8,4]) # [4, 8, 4, 0]
相應地,對一個矩陣進行合并,只需針對每一行進行操作即可
[mergeLst(x) for x in mat]
對于上下左右不同按鍵的操作,可以先將矩陣旋轉,然后再進行合并操作,合并之后再逆轉回來。
def rotate(mat):
newMat = [[[] for _ in mat] for b in mat[0]]
for i,row in enumerate(mat, 1):
for j,r in enumerate(row, 0):
newMat[j][-i] = r
return newMat
在實際操作中,左、下、右、上分別對應旋轉0,1,2,3次,即
KEY_DCT = {
pygame.K_LEFT:0, pygame.K_DOWN:1,
pygame.K_RIGHT:2, pygame.K_UP:3
}
從而游戲的總挪動邏輯如下
def updateMat(mat, key):
N = KEY_DCT[key]
for _ in range(N):
mat = rotate(mat)
mat = [mergeLst(x) for x in mat]
for _ in range(4-N):
mat = rotate(mat) # 旋轉回來
return mat
2048游戲在開始之前,需要初始化一個4x4的矩陣,然后每次操作之前,需要在矩陣中為0的位置隨機生成一個數。隨機生成的數的取值范圍決定了游戲的難度,所以生成方式也比較靈活,下面給出一種普通的生成方法
from itertools import product
from random import sample, randint
def addNew(mat):
ijs = []
for i,j in product(range(4), range(4)):
if mat[i][j] == 0:
ijs.appen((i,j))
if len(ijs) == 0:
return False
i, j = sample(ijs, 1)[0] # 挑選一個不為0的點
x = randint(1,100)
x = 7 - np.floor(np.log2(x))
mat[i,j] = int(2**x)
return True
繪圖邏輯
這個游戲的繪圖邏輯比較簡單,只需為矩陣中每個元素賦予一個顏色即可。
GRAY = (205, 205, 205)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
SIZE = 80
def setColor(N):
if N == 0:
return (233, 233, 233)
n = int(math.log2(N))
delta = int(255/15)
return (255-n*delta, n*delta, (128+n*delta)%255)
def draw(win, font, mat):
for i,j in product(range(4), range(4)):
ch = str(mat[i][j])
rect = (j*SIZE, i*SIZE, SIZE, SIZE)
c = setColor(mat[i][j])
pygame.draw.rect(win, c, rect)
pygame.draw.rect(win, GRAY, rect, 5)
txt = font.render(ch, True, GREEN)
offset = (0.5 - len(ch)/10)*SIZE
xy = (j*SIZE+offset, (i+0.3)*SIZE)
win.blit(txt, xy)
主循環(huán)
最后,實現游戲的主循環(huán)如下
def gui():
pygame.init()
win = pygame.display.set_mode(
(4 * SIZE, 4 * SIZE))
pygame.display.set_caption("迷宮游戲")
font.init()
f = font.Font('C:\\WINDOWS\\Fonts\\ARLRDBD.TTF', 32)
running = True
mat = [[0]*4 for _ in range(4)]
addNew(mat)
while running:
win.fill(WHITE)
for evt in pygame.event.get():
if evt.type == pygame.QUIT:
return
if evt.type == pygame.KEYDOWN and evt.key in KEY_DCT:
newMat = updateMat(mat, evt.key)
if newMat!=mat:
mat = newMat
running = addNew(mat)
draw(win, f, mat)
# 判斷玩家是否到達出口
if max([max(x) for x in mat])==2048:
print("恭喜通關")
running = False
if min([min(x) for x in mat])>0:
print("游戲失敗")
running = False
pygame.display.update()
游戲效果如下

以上就是基于python做一個2048小游戲的詳細內容,更多關于python 2048小游戲的資料請關注腳本之家其它相關文章!
相關文章
Python使用BeautifulSoup和Scrapy抓取網頁數據的具體教程
在當今信息爆炸的時代,數據無處不在,如何有效地抓取、處理和分析這些數據成為了許多開發(fā)者和數據科學家的必修課,本篇博客將深入探討如何使用Python中的兩個強大工具:BeautifulSoup和Scrapy來抓取網頁數據,需要的朋友可以參考下2025-01-01
關于python3?opencv?圖像二值化的問題(cv2.adaptiveThreshold函數)
這篇文章主要介紹了python3?opencv?圖像二值化cv2.adaptiveThreshold函數的相關知識,結合示例代碼介紹了adaptiveThreshold方法的用法,需要的朋友可以參考下2022-04-04

