話不多說,直接上菜
為了方便大家copy,我就不分段解釋了
import turtle, random
# 定義一個類,用來畫除了數(shù)字方塊之外的圖形
class BackGround):
def __init__(self):
super().__init__()
()
()
def draw_block(self):
('bg.gif') # 畫出背景方塊
for i in allpos:
(i)
()
('white', 'white') # 畫出其他背景
(-215, 120)
()
(215, 120)
(215, 110)
(-215, 110)
()
(';)
(-125, 210)
()
(';)
(125, 245)
()
('top_;)
(125, 170)
()
# 游戲失敗及達(dá)成2048的提示文字
def judge(self):
global flag_win, flag_win_lose_text
('blue')
judge = 0 # 判斷是否還有位置可以移動
for i in block_dic.values():
for j in block_dic.values():
if i.num == 0 or i.num == j.num and i.distance(j) == 100:
judge += 1
if judge == 0: # 無位置可移動,游戲失敗
(' GAME OVER\n重新開始請按空格鍵', align='center', font=('黑體', 30, 'bold'))
flag_win_lose_text = False
if flag_win is True: # 此條件讓2048達(dá)成的判斷只能進(jìn)行一次
for k in block_dic.values():
if k.num == 2048: # 游戲達(dá)成
flag_win = False
(' 達(dá)成2048\n繼續(xù)游戲請按回車鍵', align='center', font=('黑體', 30, 'bold'))
flag_win_lose_text = False
def win_lose_clear(self):
global flag_win_lose_text
()
flag_win_lose_text = True
def show_score(self): # 分值的顯示
global score, top_score
if score > top_score:
top_score = score
with open('.\\;, 'w') as f:
f.write(f'{top_score}')
('white')
(125, 210)
()
(f'{score}', align='center', font=('Arial', 20, 'bold'))
(125, 135)
(f'{top_score}', align='center', font=('Arial', 20, 'bold'))
# 數(shù)字方塊類
class Block):
def __init__(self):
super().__init__()
()
()
= 0
def draw(self):
()
dic_draw = {2: '#eee6db', 4: '#efe0cd', 8: '#f5af7b',
16: '#fb9660', 32: '#f57d5a', 64: '#f95c3d',
128: '#eccc75', 256: '#eece61', 512: '#efc853',
1024: '#ebc53c', 2048: '#eec430', 4096: '#aeb879',
8192: '#aab767', 16384: '#a6b74f'}
if > 0: # 數(shù)字大于0,畫出方塊
(f'{dic_draw[]}') # 選擇顏色
()
()+48, ()+48)
()-96, ())
(), ()-96)
()+96, ())
(), ()+96)
()
()-48, ()-68)
if > 4: # 按照數(shù)字選擇數(shù)字的顏色
('white')
else:
('#6d6058')
(f'{}', align='center', font=('Arial', 27, 'bold'))
(), ()+20)
class Game():
def init(self):
back = BackGround() # 實(shí)例畫出游戲的背景
back.draw_block()
for i in allpos: # 畫出16個海龜對應(yīng)16個數(shù)字塊
block = Block()
block.goto(i)
block_dic[i] = block
game.grow()
def restart(self): # 重開游戲的方法
global score, flag_win_lose_text
score = 0
for i in block_dic.values():
i.num = 0
i.clear()
win_lo()
game.grow()
flag_win_lose_text = True # 此flag為游戲達(dá)成或失敗出現(xiàn)提示語后的判斷,要提示語被clear后才能繼續(xù)move
def grow(self): # 隨機(jī)出現(xiàn)一個2或4的數(shù)字塊
block_list = []
for i in allpos:
if block_dic[i].num == 0:
block_li(block_dic[i]) # 挑出空白方塊的海龜
turtle_choice = random.choice(block_list) # 隨機(jī)選中其中一個海龜
= random.choice([2, 2, 2, 2, 4]) # 賦屬性num=2/4
()
win_lo()
()
ms.update()
def move_up(self):
allpos1 = allpos[::4] # 切片為四列
allpos2 = allpos[1::4]
allpos3 = allpos[2::4]
allpos4 = allpos[3::4]
(allpos1, allpos2, allpos3, allpos4)
def move_down(self):
allpos1 = allpos[-4::-4]
allpos2 = allpos[-3::-4]
allpos3 = allpos[-2::-4]
allpos4 = allpos[-1::-4]
(allpos1, allpos2, allpos3, allpos4)
def move_left(self):
allpos1 = allpos[:4]
allpos2 = allpos[4:8]
allpos3 = allpos[8:12]
allpos4 = allpos[12:16]
(allpos1, allpos2, allpos3, allpos4)
def move_right(self):
allpos1 = allpos[-1:-5:-1]
allpos2 = allpos[-5:-9:-1]
allpos3 = allpos[-9:-13:-1]
allpos4 = allpos[-13:-17:-1]
(allpos1, allpos2, allpos3, allpos4)
def move_move(self, allpos1, allpos2, allpos3, allpos4):
if flag_win_lose_text is True:
count1 = (allpos1) # 四列或四行依次移動
count2 = (allpos2)
count3 = (allpos3)
count4 = (allpos4)
if count1 or count2 or count3 or count4: # 判斷是否有方塊移動,有才能繼續(xù)出現(xiàn)新的數(shù)字塊
()
def move(self, pos_list):
num_list = [] # 為某一列或行的數(shù)字塊海龜?shù)淖鴺?biāo)
for i in pos_list:
num_li(block_dic[i].num) # 把這些海龜?shù)腘UM形成list
new_num_list, count = (num_list) # 只是list_oper的方法形成新的list
for j in range(len(new_num_list)): # 把新的list依次賦值給對應(yīng)的海龜.num屬性并調(diào)用draw()方法
block_dic[pos_list[j]].num = new_num_list[j]
block_dic[pos_list[j]].draw()
return count
def list_oper(self, num_list): # num_list的操作,假設(shè)其為【2,0,2,2】
global score
count = True
temp = []
new_temp = []
for j in num_list:
if j != 0:
(j) # temp=[2,2,2]
flag = True
for k in range(len(temp)):
if flag:
if k < len(temp)-1 and temp[k] == temp[k+1]:
new_(temp[k]*2)
flag = False
score += temp[k]
else:
new_(temp[k]) # new_temp=[4,2]
else:
flag = True
for m in range(len(num_list)-len(new_temp)):
new_(0) # new_temp=[4,2,0,0]
if new_temp == num_list:
count = False # 此變量判斷num_list沒有變化,數(shù)字塊無移動
return(new_temp, count)
if __name__ == '__main__':
ms = () # 主窗口的設(shè)置
ms.setup(430, 630, 400, 50)
ms.bgcolor('gray')
ms.title('2048')
ms.tracer(0)
ms.register_shape('bg.gif')
ms.register_shape(';)
ms.register_shape(';)
ms.register_shape('top_;)
block_dic = {} # 放數(shù)字方塊海龜?shù)淖值?,位置坐?biāo)為key,對應(yīng)海龜為value
allpos = [(-150, 50), (-50, 50), (50, 50), (150, 50),
(-150, -50), (-50, -50), (50, -50), (150, -50),
(-150, -150), (-50, -150), (50, -150), (150, -150),
(-150, -250), (-50, -250), (50, -250), (150, -250)]
flag_win = True # 達(dá)成2048的判斷,讓達(dá)成的文字僅出現(xiàn)一次
flag_win_lose_text = True # 用來判斷失敗或成功的提示文字是否有被清除,不清除不能繼續(xù)移動方塊
score = 0
with open('.\\;, 'r') as f:
top_score = in()) # 讀取score中的數(shù)據(jù)
show_score_text = BackGround()
win_lose_text = BackGround()
game = Game()
game.init()
ms.listen()
ms.onkey, 'Up')
ms.onkey, 'Down')
ms.onkey, 'Left')
ms.onkey, 'Right')
ms.onkey, 'Return')
ms.onkey, 'space')
ms.mainloop()
這是游戲界面:
歡迎挑戰(zhàn)最高分。
要運(yùn)行出來,必須本地要有這些文件:bg.gif,,,top_,
有需要源文件的歡迎私信我 “學(xué)習(xí)” 即可免費(fèi)領(lǐng)取。還有一些學(xué)習(xí)的資料,支持作者的,可以關(guān)注和點(diǎn)贊。感謝你們!
1.《2048游戲技巧,干貨看這篇!python小游戲2048,上班摸魚必備(附源碼)》援引自互聯(lián)網(wǎng),旨在傳遞更多網(wǎng)絡(luò)信息知識,僅代表作者本人觀點(diǎn),與本網(wǎng)站無關(guān),侵刪請聯(lián)系頁腳下方聯(lián)系方式。
2.《2048游戲技巧,干貨看這篇!python小游戲2048,上班摸魚必備(附源碼)》僅供讀者參考,本網(wǎng)站未對該內(nèi)容進(jìn)行證實(shí),對其原創(chuàng)性、真實(shí)性、完整性、及時性不作任何保證。
3.文章轉(zhuǎn)載時請保留本站內(nèi)容來源地址,http://f99ss.com/yule/2189538.html