俄羅斯方塊(C++)

      網(wǎng)友投稿 809 2025-03-31

      #include


      #include

      #include

      #include

      #include

      using namespace std;

      #define A1 0//A代表長條型,B為方塊,C為L型,D為閃電型(實(shí)在無法描述那個(gè)形狀)

      #define A2 1

      #define B 2

      #define C11 3

      #define C12 4

      #define C13 5

      #define C14 6

      #define C21 7

      #define C22 8

      #define C23 9

      #define C24 10

      #define D11 11

      #define D12 12

      #define D21 13

      #define D22 14

      void SetPos(short i,short j)//設(shè)定光標(biāo)位置

      {

      COORD pos={i,j};

      HANDLE Out=GetStdHandle(STD_OUTPUT_HANDLE);

      SetConsoleCursorPosition(Out, pos);

      }

      int sharp[15][8]=

      {

      {0,0,1,0,2,0,3,0},{0,0,0,1,0,2,0,3},

      {0,0,1,0,0,1,1,1},

      {0,0,1,0,1,1,1,2},{0,1,1,1,2,0,2,1},{0,0,0,1,0,2,1,2},{0,0,0,1,1,0,2,0},

      {1,0,1,1,1,2,0,2},{0,0,0,1,1,1,2,1},{0,0,0,1,0,2,1,0},{0,0,1,0,2,0,2,1},

      {0,0,0,1,1,1,1,2},{0,1,1,0,1,1,2,0},

      {0,1,0,2,1,0,1,1},{0,0,1,0,1,1,2,1}

      };//這個(gè)2維數(shù)組是用來保存各個(gè)形狀位置的

      int high[15]={4,1,2,2,3,2,3,2,3,2,3,2,3,2,3};//這個(gè)數(shù)組是用來保存各個(gè)形狀高度的

      class Box//俄羅斯方塊類

      {

      private:

      int map[23][12];//畫面坐標(biāo)

      int hotpoint[2];//熱點(diǎn)(即當(dāng)前活動(dòng)的點(diǎn),所有圖形都是相當(dāng)此點(diǎn)繪制的)

      int top;//當(dāng)前最高位置

      int point;//分?jǐn)?shù)

      int level;//等級(jí)

      int ID;//當(dāng)前活動(dòng)圖形的ID號(hào)

      public:

      Box()//初始化

      {

      int i,j;

      for(i=0;i<23;i++)

      for(j=0;j<12;j++)

      map[i][j]=0;

      hotpoint[0]=0;

      hotpoint[1]=5;

      point=0;

      level=1;

      top=99;

      ID=0;

      }

      void DrawMap();//畫界面

      int Judge(int x,int y);//判斷當(dāng)前位置能否繪制圖形

      void Welcome();//歡迎界面

      void DrawBox(int x,int y,int num);//繪制圖形

      void Redraw(int x,int y,int num);//擦除圖形

      void Run();//運(yùn)行

      void Turn();//轉(zhuǎn)動(dòng)方塊

      void UpdataMap();//更新畫面

      };

      void Box::DrawMap()//畫界面

      {

      int i;

      for(i=0;i<14;i++)

      {

      SetPos(i*2,0);

      cout<<"■";

      }

      for(i=1;i<=24;i++)

      {

      SetPos(0,i);

      cout<<"■";

      SetPos(13*2,i);

      cout<<"■";

      }

      for(i=0;i<14;i++)

      {

      SetPos(i*2,24);

      cout<<"■";

      }

      i=15;

      for(i=15;i<=25;i++)

      {

      SetPos(i*2,0);

      cout<<"■";

      }

      for(i=1;i<=8;i++)

      {

      SetPos(15*2,i);

      cout<<"■";

      SetPos(25*2,i);

      cout<<"■";

      }

      for(i=15;i<=25;i++)

      {

      SetPos(i*2,9);

      cout<<"■";

      }

      SetPos(16*2,16);

      cout<<"俄羅斯方塊";

      SetPos(16*2,17);

      cout<<"分?jǐn)?shù):"<

      SetPos(16*2,18);

      cout<<"等級(jí):"<

      }

      void Box::DrawBox(int x,int y,int num)//繪制圖形

      {

      int i;

      int nx,ny;

      for(i=0;i<4;i++)

      {

      nx=x+sharp[num][i*2];

      ny=y+sharp[num][i*2+1];

      SetPos((ny+1)*2,nx+1);//利用sharp數(shù)組相對(duì)于點(diǎn)x,y繪制形狀

      cout<<"■";

      }

      }

      void Box::Redraw(int x,int y,int num)//擦除圖形,原理同上

      {

      int i;

      int nx,ny;

      for(i=0;i<4;i++)

      {

      nx=x+sharp[num][i*2];

      ny=y+sharp[num][i*2+1];

      SetPos((ny+1)*2,nx+1);

      cout<<" ";

      }

      }

      void Box::Turn()//轉(zhuǎn)動(dòng)圖形,單純的該ID而已

      {

      switch(ID)

      {

      case A1: ID=A2; break;

      case A2: ID=A1; break;

      case B: ID=B; break;

      case C11: ID=C12; break;

      case C12: ID=C13; break;

      case C13: ID=C14; break;

      case C14: ID=C11; break;

      case C21: ID=C22; break;

      case C22: ID=C23; break;

      case C23: ID=C24; break;

      case C24: ID=C21; break;

      case D11: ID=D12; break;

      case D12: ID=D11; break;

      case D21: ID=D22; break;

      case D22: ID=D21; break;

      }

      }

      void Box::Welcome()//歡迎界面

      {

      char x;

      while(1)

      {

      system("cls");

      cout<<"■■■■■■■■■■■■■■■■■■■"<

      cout<<"■ 俄羅斯方塊控制臺(tái)版(不閃屏) ■"<

      cout<<"■■■■■■■■■■■■■■■■■■■"<

      cout<<"■ A,D左右移動(dòng) S向下加速 ■"<

      cout<<"■ 空格鍵轉(zhuǎn)動(dòng)方塊 ■"<

      cout<<"■■■■■■■■■■■■■■■■■■■"<

      cout<<"■ ■"<

      cout<<"■ 測試版 ■"<

      cout<<"■ ■"<

      cout<<"■ 按1-9選擇等級(jí)!! ■"<

      cout<<"■ ■"<

      cout<<"■ ■"<

      cout<<"■■■■■■■■■■■■■■■■■■■"<

      SetPos(8,10);

      x=getch();

      if(x<='9'&&x>='1')//設(shè)置等級(jí)

      {

      level=x-'0';

      break;

      }

      }

      }

      void Box::UpdataMap()//更新畫面(關(guān)鍵)

      {

      int clear;

      int i,j,k;

      int nx,ny;

      int flag;

      for(i=0;i<4;i++)//更新map數(shù)組的信息

      {

      nx=hotpoint[0]+sharp[ID][i*2];

      ny=hotpoint[1]+sharp[ID][i*2+1];

      map[nx][ny]=1;

      }

      if(hotpoint[0]

      top=hotpoint[0];

      clear=0;//消除的格數(shù)

      for(i=hotpoint[0];i

      {

      flag=0;

      for(j=0;j<12;j++)//檢測是否可以消除此行

      {

      if(map[i][j]==0)

      {

      flag=1;

      break;

      }

      }

      if(flag==0)//可以消除

      {

      for(k=i;k>=top;k--)//從當(dāng)前位置向上所有的點(diǎn)下移一行

      {

      if(k==0)//最高點(diǎn)特殊處理

      for(j=0;j<12;j++)

      {

      map[k][j]=0;

      SetPos((j+1)*2,k+1);

      cout<<" ";

      }

      else

      {

      for(j=0;j<12;j++)

      {

      map[k][j]=map[k-1][j];

      SetPos((j+1)*2,k+1);

      if(map[k][j]==0)

      cout<<" ";

      俄羅斯方塊(C++)

      else

      cout<<"■";

      }

      }

      }

      top++;//消除成功,最高點(diǎn)下移

      clear++;

      point+=clear*100;

      }

      }

      SetPos(16*2,17);

      cout<<"分?jǐn)?shù):"<

      }

      void Box::Run()//運(yùn)行游戲

      {

      int i=0;

      char x;

      int Count;//計(jì)數(shù)器

      int tempID;

      int temp;

      srand((int)time(0));

      ID=rand()%15;//隨機(jī)生成ID和下一個(gè)ID

      tempID=rand()%15;

      DrawBox(hotpoint[0],hotpoint[1],ID);//繪制圖形

      DrawBox(3,17,tempID);

      Count=1000-level*100;//等級(jí)決定計(jì)數(shù)

      while(1)

      {

      if(i>=Count)//時(shí)間到

      {

      i=0;//計(jì)數(shù)器清零

      if(Judge(hotpoint[0]+1,hotpoint[1]))//如果下個(gè)位置無效(即到底)

      {

      UpdataMap();//更新畫面

      ID=tempID;//生成新ID,用原等待ID替換為當(dāng)前ID

      hotpoint[0]=0;//熱點(diǎn)更新

      hotpoint[1]=5;

      Redraw(3,17,tempID);

      tempID=rand()%15;

      DrawBox(hotpoint[0],hotpoint[1],ID);

      DrawBox(3,17,tempID);

      if(Judge(hotpoint[0],hotpoint[1]))//無法繪制開始圖形,游戲結(jié)束

      {

      system("cls");

      SetPos(25,15);

      cout<<"游戲結(jié)束!!!最終得分為:"<

      system("pause");

      exit(0);

      }

      }

      else

      {

      Redraw(hotpoint[0],hotpoint[1],ID);//沒有到底,方塊下移一位

      hotpoint[0]++;//熱點(diǎn)下移

      DrawBox(hotpoint[0],hotpoint[1],ID);

      }

      }

      if(kbhit())//讀取鍵盤信息

      {

      x=getch();

      if(x=='a'||x=='A')//左移

      {

      if(Judge(hotpoint[0],hotpoint[1]-1)==0)

      {

      Redraw(hotpoint[0],hotpoint[1],ID);

      hotpoint[1]-=1;

      DrawBox(hotpoint[0],hotpoint[1],ID);

      }

      }

      if(x=='d'||x=='D')//右移

      {

      if(Judge(hotpoint[0],hotpoint[1]+1)==0)

      {

      Redraw(hotpoint[0],hotpoint[1],ID);

      hotpoint[1]+=1;

      DrawBox(hotpoint[0],hotpoint[1],ID);

      }

      }

      if(x=='s'||x=='S')//向下加速

      {

      if(Judge(hotpoint[0]+1,hotpoint[1])==0)

      {

      Redraw(hotpoint[0],hotpoint[1],ID);

      hotpoint[0]+=1;

      DrawBox(hotpoint[0],hotpoint[1],ID);

      }

      }

      if(x==' ')//轉(zhuǎn)動(dòng)方塊

      {

      temp=ID;

      Turn();

      if(Judge(hotpoint[0],hotpoint[1])==0)

      {

      Redraw(hotpoint[0],hotpoint[1],temp);

      DrawBox(hotpoint[0],hotpoint[1],ID);

      }

      else

      ID=temp;

      }

      while(kbhit())//讀掉剩下的鍵盤信息

      getch();

      }

      Sleep(1);//等待1毫秒

      i++;//計(jì)數(shù)器加1

      }

      }

      int Box::Judge(int x,int y)//判斷當(dāng)前是否可以繪制方塊

      {

      int i;

      int nx,ny;

      for(i=0;i<4;i++)

      {

      nx=x+sharp[ID][i*2];

      ny=y+sharp[ID][i*2+1];

      if(nx<0||nx>=23||ny<0||ny>=12||map[nx][ny]==1)//不能,返回1

      return 1;

      }

      return 0;

      }

      int main()//主函數(shù)

      {

      Box game;

      game.Welcome();

      system("cls");

      game.DrawMap();

      game.Run();

      system("pause");

      }

      C++ 數(shù)據(jù)結(jié)構(gòu)

      版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請(qǐng)聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。

      版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請(qǐng)聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。

      上一篇:excel如何使一列負(fù)數(shù)變成正數(shù)(在excel中如何把負(fù)數(shù)變?yōu)檎龜?shù))
      下一篇:在線微表單登錄(微信在線表單
      相關(guān)文章
      亚洲成A人片在线播放器| 久久亚洲国产视频| 亚洲麻豆精品果冻传媒| 亚洲毛片αv无线播放一区| 亚洲国产天堂久久久久久| 久久久久久亚洲av无码蜜芽| 亚洲中文字幕无码久久| 亚洲色成人四虎在线观看| 亚洲色大成网站www| 亚洲欧美成人av在线观看| 亚洲熟妇成人精品一区| 亚洲日本VA中文字幕久久道具| 亚洲成人激情小说| 亚洲成av人在线观看网站| 亚洲精品无码久久久久APP| 亚洲AV成人精品一区二区三区| 亚洲高清一区二区三区电影| 亚洲AV无码成人精品区狼人影院| 国产AV无码专区亚洲AV麻豆丫| 国产精品亚洲综合天堂夜夜| 亚洲成人一区二区| 亚洲午夜国产片在线观看| 自拍偷自拍亚洲精品第1页| 亚洲色欲色欲www在线丝 | 亚洲综合日韩久久成人AV| a级亚洲片精品久久久久久久| 亚洲线精品一区二区三区影音先锋| 亚洲女同成av人片在线观看| 亚洲国产精品嫩草影院在线观看 | 亚洲综合一区无码精品| 亚洲成熟丰满熟妇高潮XXXXX| 日韩色视频一区二区三区亚洲 | 亚洲最大视频网站| 麻豆狠色伊人亚洲综合网站| 亚洲欧美综合精品成人导航| 一区国严二区亚洲三区| 久久亚洲高清综合| 亚洲成人激情在线| 亚洲人配人种jizz| 综合偷自拍亚洲乱中文字幕| 中文字幕亚洲综合久久男男|