您好,登錄后才能下訂單哦!
python代碼實現2048游戲?相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。
一、分析與函數設計
1.1 游戲玩法
2048這款游戲的玩法很簡單,每次可以選擇上下左右滑動,每滑動一次,所有的數字方塊都會往滑動的方向靠攏,系統也會在空白的地方亂數出現一個數字方塊,相同數字的方塊在靠攏、相撞時會相加。(介紹來自百度百科)
1.2 函數設計
_init _() 初始化4*4游戲地圖,分數等游戲基本數據
is_gameover() 判斷是否結束游戲
rannumber() 玩家每次移動時在地圖上隨機生成2、4
show() 在控制臺打印出4*4游戲地圖
print_score() 在控制臺打印出當前分數
up(), upmove() 上移
down(), downmove() 下移
left(), leftmove() 左移
right(), rightmove() 右移
nextstep() 讀取玩家按鍵
二、代碼實現
注: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("游戲結束") 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.地圖的儲存與表示:目前沒有界面設計,因此就用二維數組直接儲存與表示
2.數組越界:調試代碼中遇到過五六次,除了牢記要邊緣檢測外, and 語句左右條件順序也要有講究。
例:while(temp<=2 and self.number[temp+1][j]==0 ) 注:self.number為4*4的二維數組
當temp=3時,語句在temp<=2 被阻塞,不會執行self.number[temp+1][j]==0,此時沒有問題;
若語句改為while(self.number[temp+1][j]==0 and temp<=2), 先執行self.number[temp+1][j]==0,此時編譯器報錯數組越界
看完上述內容,你們掌握python代碼實現2048游戲的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。