最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

200行python代碼實現(xiàn)2048游戲

 更新時間:2019年07月17日 14:31:44   作者:PastoralDog  
這篇文章主要為大家詳細(xì)介紹了200行Python代碼實現(xiàn)2048游戲,具有一定的參考價值,感興趣的小伙伴們可以參考一下

Python實戰(zhàn)系列用于記錄實戰(zhàn)項目中的思路,代碼實現(xiàn),出現(xiàn)的問題與解決方案以及可行的改進(jìn)方向

本文為第2篇–200行Python代碼實現(xiàn)2048

一、分析與函數(shù)設(shè)計

1.1 游戲玩法

2048這款游戲的玩法很簡單,每次可以選擇上下左右滑動,每滑動一次,所有的數(shù)字方塊都會往滑動的方向靠攏,系統(tǒng)也會在空白的地方亂數(shù)出現(xiàn)一個數(shù)字方塊,相同數(shù)字的方塊在靠攏、相撞時會相加。(介紹來自百度百科)

1.2 函數(shù)設(shè)計

  • _init _() 初始化4*4游戲地圖,分?jǐn)?shù)等游戲基本數(shù)據(jù)
  • is_gameover() 判斷是否結(jié)束游戲
  • rannumber() 玩家每次移動時在地圖上隨機(jī)生成2、4
  • show() 在控制臺打印出4*4游戲地圖
  • print_score() 在控制臺打印出當(dāng)前分?jǐn)?shù)
  • up(), upmove() 上移
  • down(), downmove() 下移
  • left(), leftmove() 左移
  • right(), rightmove() 右移
  • nextstep() 讀取玩家按鍵

二、代碼實現(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.地圖的儲存與表示:目前沒有界面設(shè)計,因此就用二維數(shù)組直接儲存與表示

2.數(shù)組越界:調(diào)試代碼中遇到過五六次,除了牢記要邊緣檢測外, and 語句左右條件順序也要有講究。

例:while(temp<=2 and self.number[temp+1][j]==0 ) 注:self.number為4*4的二維數(shù)組
當(dāng)temp=3時,語句在temp<=2 被阻塞,不會執(zhí)行self.number[temp+1][j]==0,此時沒有問題;
若語句改為while(self.number[temp+1][j]==0 and temp<=2), 先執(zhí)行self.number[temp+1][j]==0,此時編譯器報錯數(shù)組越界

四、可行的改進(jìn)方向

1.添加可視化界面,可考慮Tkinter,QT等

2.拓展游戲地圖大小為N x N

本人水平有限,歡迎大家提出問題與建議。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python3+Pygame實現(xiàn)射擊游戲完整代碼

    Python3+Pygame實現(xiàn)射擊游戲完整代碼

    這篇文章主要介紹了Python3+Pygame實現(xiàn)射擊游戲完整代碼,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-03-03
  • python入門:這篇文章帶你直接學(xué)會python

    python入門:這篇文章帶你直接學(xué)會python

    本教程并未涵蓋Python語言的全部內(nèi)容,只是一個入門的教程,Python有非常多的庫以及很多的功能特點(diǎn)需要學(xué)習(xí),小編只是拋磚引玉,希望大家可以從中受益
    2018-09-09
  • Python數(shù)據(jù)分析入門之?dāng)?shù)據(jù)讀取與存儲

    Python數(shù)據(jù)分析入門之?dāng)?shù)據(jù)讀取與存儲

    今天繼續(xù)帶大家學(xué)習(xí)python數(shù)據(jù)分析,下文中有非常詳細(xì)的代碼示例,清楚地解釋了python數(shù)據(jù)讀取與存儲的相關(guān)知識,需要的朋友可以參考下
    2021-05-05
  • pytorch關(guān)于Tensor的數(shù)據(jù)類型說明

    pytorch關(guān)于Tensor的數(shù)據(jù)類型說明

    這篇文章主要介紹了pytorch關(guān)于Tensor的數(shù)據(jù)類型說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • Python實現(xiàn)Excel自動分組合并單元格

    Python實現(xiàn)Excel自動分組合并單元格

    這篇文章主要為大家詳細(xì)介紹了Python實現(xiàn)Excel自動分組合并單元格,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-02-02
  • Python實現(xiàn)常見坐標(biāo)系的相互轉(zhuǎn)換

    Python實現(xiàn)常見坐標(biāo)系的相互轉(zhuǎn)換

    WGS84坐標(biāo)系、GCJ02坐標(biāo)系、BD09坐標(biāo)系和Web?墨卡托投影坐標(biāo)系是我們常見的四個坐標(biāo)系。這篇文章為大家整理了這四個坐標(biāo)系之間相互轉(zhuǎn)換的方法,需要的可以參考一下
    2023-02-02
  • pygame實現(xiàn)打字游戲

    pygame實現(xiàn)打字游戲

    這篇文章主要為大家詳細(xì)介紹了pygame實現(xiàn)打字游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-10-10
  • Python 中 AttributeError: ‘NoneType‘ object has no attribute ‘X‘ 錯誤問題解決方案

    Python 中 AttributeError: ‘NoneType‘ obje

    Python “AttributeError: ‘NoneType’ object has no attribute” 發(fā)生在我們嘗試訪問 None 值的屬性時,例如 來自不返回任何內(nèi)容的函數(shù)的賦值, 要解決該錯誤,請在訪問屬性之前更正分配,本文通過示例給大家說明錯誤是如何發(fā)生的,感興趣的朋友一起看看吧
    2023-08-08
  • Python使用psutil庫實現(xiàn)系統(tǒng)監(jiān)控與管理詳解

    Python使用psutil庫實現(xiàn)系統(tǒng)監(jiān)控與管理詳解

    在我們的測試工作中,監(jiān)控和管理系統(tǒng)資源是一項重要的任務(wù),本文將介紹如何使用psutil庫來實現(xiàn)系統(tǒng)監(jiān)控和管理,以及一些實用的技巧和示例,希望對大家有所幫助
    2022-10-10
  • 基于python實現(xiàn)自動化辦公學(xué)習(xí)筆記(CSV、word、Excel、PPT)

    基于python實現(xiàn)自動化辦公學(xué)習(xí)筆記(CSV、word、Excel、PPT)

    這篇文章主要介紹了基于python實現(xiàn)自動化辦公學(xué)習(xí)筆記,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08

最新評論

霸州市| 农安县| 顺平县| 兖州市| 南和县| 凤阳县| 鄂尔多斯市| 大姚县| 会泽县| 安新县| 漳州市| 揭阳市| 日土县| 淅川县| 大冶市| 顺昌县| 奉新县| 靖安县| 岳西县| 长乐市| 漳平市| 宝清县| 房产| 贵阳市| 崇州市| 汉寿县| 铜鼓县| 江源县| 麦盖提县| 绿春县| 仙桃市| 和林格尔县| 玛纳斯县| 衡水市| 曲阜市| 泽普县| 青田县| 郓城县| 白河县| 东源县| 迭部县|