python代碼實(shí)現(xiàn)2048游戲?相信很多沒有經(jīng)驗(yàn)的人對(duì)此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。
創(chuàng)新互聯(lián)建站是專業(yè)的鎮(zhèn)安網(wǎng)站建設(shè)公司,鎮(zhèn)安接單;提供成都網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè),網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行鎮(zhèn)安網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!一、分析與函數(shù)設(shè)計(jì)
1.1 游戲玩法
2048這款游戲的玩法很簡單,每次可以選擇上下左右滑動(dòng),每滑動(dòng)一次,所有的數(shù)字方塊都會(huì)往滑動(dòng)的方向靠攏,系統(tǒng)也會(huì)在空白的地方亂數(shù)出現(xiàn)一個(gè)數(shù)字方塊,相同數(shù)字的方塊在靠攏、相撞時(shí)會(huì)相加。(介紹來自百度百科)
1.2 函數(shù)設(shè)計(jì)
_init _() 初始化4*4游戲地圖,分?jǐn)?shù)等游戲基本數(shù)據(jù)
is_gameover() 判斷是否結(jié)束游戲
rannumber() 玩家每次移動(dòng)時(shí)在地圖上隨機(jī)生成2、4
show() 在控制臺(tái)打印出4*4游戲地圖
print_score() 在控制臺(tái)打印出當(dāng)前分?jǐn)?shù)
up(), upmove() 上移
down(), downmove() 下移
left(), leftmove() 左移
right(), rightmove() 右移
nextstep() 讀取玩家按鍵
二、代碼實(shí)現(xiàn)
注:IDE為Spyder,Python版本為3.6
# -*- coding: utf-8 -*- """ Created on Sat Sep 29 16:29:04 2018 @author: PastoralDog """ import random class game2048(object): def __init__(self): self.score=0 self.number=[[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]] self.move=0 seed=random.randint(0,15) line=int(seed/4) row=seed%4 self.number[line][row]=2 def is_gameover(self): numbersum=0 for i in range(4): for j in range(4): if (self.number[i][j]!=0): numbersum+=1 if(numbersum!=16): return False for i in range(4): for j in range(3): if(self.number[i][j+1]==self.number[i][j]): return False for i in range(3): for j in range(4): if(self.number[i+1][j]==self.number[i][j]): return False print("游戲結(jié)束") print("您的得分為:"+str(self.score)) self.__init__() return True def rannumber(self): rannumber=random.randint(1,10) if(rannumber<=8): rannumber=2 else: rannumber=4 done=0 count=0 for i in range(4): for j in range(4): if(self.number[i][j]==0): count+=1 while(done==0 and count!=0): ranplace=random.randint(0,15) line=int(ranplace/4) row=ranplace%4 if(self.number[line][row]==0): done=1 self.number[line][row]=rannumber def show(self): print(self.number[0]) print(self.number[1]) print(self.number[2]) print(self.number[3]) def print_score(self): print("得分:"+str(self.score)) def upmove(self): for i in range(1,4): for j in range(4): temp=i while(temp>=1 and self.number[temp-1][j]==0): box=self.number[temp-1][j] self.number[temp-1][j]=self.number[temp][j] self.number[temp][j]=box if(self.number[temp][j]!=0):self.move=1 temp-=1 def up(self): self.upmove() for i in range(1,4): for j in range(4): if(self.number[i-1][j]==self.number[i][j]): if(self.number[i-1][j]!=2048): self.score+=self.number[i][j] self.number[i][j]=0 self.number[i-1][j]=2*self.number[i-1][j] self.upmove() if(self.move!=0):self.rannumber() self.move=0 self.show() self.is_gameover() self.print_score() def downmove(self): for i in range(2,-1,-1): for j in range(4): temp=i while(temp<=2 and self.number[temp+1][j]==0 ): box=self.number[temp+1][j] self.number[temp+1][j]=self.number[temp][j] self.number[temp][j]=box if(self.number[temp+1][j]!=0):self.move=1 temp+=1 def down(self): self.downmove() for i in range(2,-1,-1): for j in range(4): if(self.number[i+1][j]==self.number[i][j]): if(self.number[i+1][j]!=2048): self.score+=self.number[i][j] self.number[i][j]=0 self.number[i+1][j]=2*self.number[i+1][j] self.downmove() if(self.move!=0):self.rannumber() self.move=0 self.show() self.is_gameover() self.print_score() def leftmove(self): for i in range(4): for j in range(1,4): temp=j while(temp>=1 and self.number[i][temp-1]==0 ): box=self.number[i][temp-1] self.number[i][temp-1]=self.number[i][temp] self.number[i][temp]=box if(self.number[i][temp-1]!=0):self.move=1 temp-=1 def left(self): self.leftmove() for i in range(4): for j in range(0,3): if(self.number[i][j+1]==self.number[i][j]): if(self.number[i][j+1]!=2048): self.score+=self.number[i][j] self.number[i][j+1]=0 self.number[i][j]=2*self.number[i][j] self.leftmove() if(self.move!=0):self.rannumber() self.move=0 self.show() self.is_gameover() self.print_score() def rightmove(self): for i in range(4): for j in range(2,-1,-1): temp=j while(temp<=2 and self.number[i][temp+1]==0 ): box=self.number[i][temp+1] self.number[i][temp+1]=self.number[i][temp] self.number[i][temp]=box self.move=1 temp+=1 def right(self): self.rightmove() for i in range(4): for j in range(2,-1,-1): if(self.number[i][j+1]==self.number[i][j]): if(self.number[i][j+1]!=2048): self.score+=self.number[i][j] self.number[i][j]=0 self.number[i][j+1]=2*self.number[i][j+1] self.rightmove() if(self.move!=0):self.rannumber() self.move=0 self.show() self.is_gameover() self.print_score() def nextstep(self,step): if(step=='w'): self.up() elif(step=='s'): self.down() elif(step=='a'): self.left() elif(step=='d'): self.right() else: pass if __name__ == '__main__': game=game2048() game.show() while(True): step=input() if(step=='b'):break game.nextstep(step)
三、問題與解決方案
1.地圖的儲(chǔ)存與表示:目前沒有界面設(shè)計(jì),因此就用二維數(shù)組直接儲(chǔ)存與表示
2.數(shù)組越界:調(diào)試代碼中遇到過五六次,除了牢記要邊緣檢測(cè)外, and 語句左右條件順序也要有講究。
例:while(temp<=2 and self.number[temp+1][j]==0 ) 注:self.number為4*4的二維數(shù)組
當(dāng)temp=3時(shí),語句在temp<=2 被阻塞,不會(huì)執(zhí)行self.number[temp+1][j]==0,此時(shí)沒有問題;
若語句改為while(self.number[temp+1][j]==0 and temp<=2), 先執(zhí)行self.number[temp+1][j]==0,此時(shí)編譯器報(bào)錯(cuò)數(shù)組越界
看完上述內(nèi)容,你們掌握python代碼實(shí)現(xiàn)2048游戲的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!