日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

俄罗斯方块(C++)

發布時間:2023/12/13 c/c++ 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 俄罗斯方块(C++) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#include<iostream> #include<stdlib.h> #include<windows.h> #include<time.h> #include<conio.h> using namespace std;#define A1 0//A代表長條型,B為方塊,C為L型,D為閃電型(實在無法描述那個形狀) #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 14void SetPos(short i,short j)//設定光標位置 {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} };//這個2維數組是用來保存各個形狀位置的int high[15]={4,1,2,2,3,2,3,2,3,2,3,2,3,2,3};//這個數組是用來保存各個形狀高度的class Box//俄羅斯方塊類 {private:int map[23][12];//畫面坐標int hotpoint[2];//熱點(即當前活動的點,所有圖形都是相當此點繪制的)int top;//當前最高位置int point;//分數int level;//等級int ID;//當前活動圖形的ID號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);//判斷當前位置能否繪制圖形void Welcome();//歡迎界面void DrawBox(int x,int y,int num);//繪制圖形void Redraw(int x,int y,int num);//擦除圖形void Run();//運行void Turn();//轉動方塊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<<"分數:"<<point;SetPos(16*2,18);cout<<"等級:"<<level; }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數組相對于點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()//轉動圖形,單純的該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<<"■■■■■■■■■■■■■■■■■■■"<<endl;cout<<"■ 俄羅斯方塊控制臺版(不閃屏) ■"<<endl;cout<<"■■■■■■■■■■■■■■■■■■■"<<endl;cout<<"■ A,D左右移動 S向下加速 ■"<<endl;cout<<"■ 空格鍵轉動方塊 ■"<<endl;cout<<"■■■■■■■■■■■■■■■■■■■"<<endl;cout<<"■ ■"<<endl;cout<<"■ 測試版 ■"<<endl;cout<<"■ ■"<<endl;cout<<"■ 按1-9選擇等級!! ■"<<endl;cout<<"■ ■"<<endl;cout<<"■ ■"<<endl;cout<<"■■■■■■■■■■■■■■■■■■■"<<endl;SetPos(8,10);x=getch();if(x<='9'&&x>='1')//設置等級{level=x-'0';break;}} }void Box::UpdataMap()//更新畫面(關鍵) {int clear;int i,j,k;int nx,ny;int flag;for(i=0;i<4;i++)//更新map數組的信息{nx=hotpoint[0]+sharp[ID][i*2];ny=hotpoint[1]+sharp[ID][i*2+1];map[nx][ny]=1;}if(hotpoint[0]<top)//如果熱點高于頂點則更新頂點top=hotpoint[0];clear=0;//消除的格數for(i=hotpoint[0];i<hotpoint[0]+high[ID];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--)//從當前位置向上所有的點下移一行{if(k==0)//最高點特殊處理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<<" ";elsecout<<"■";}}}top++;//消除成功,最高點下移clear++;point+=clear*100;}}SetPos(16*2,17);cout<<"分數:"<<point; }void Box::Run()//運行游戲 {int i=0;char x;int Count;//計數器int tempID;int temp;srand((int)time(0));ID=rand()%15;//隨機生成ID和下一個IDtempID=rand()%15;DrawBox(hotpoint[0],hotpoint[1],ID);//繪制圖形DrawBox(3,17,tempID);Count=1000-level*100;//等級決定計數while(1){if(i>=Count)//時間到{i=0;//計數器清零if(Judge(hotpoint[0]+1,hotpoint[1]))//如果下個位置無效(即到底){UpdataMap();//更新畫面ID=tempID;//生成新ID,用原等待ID替換為當前IDhotpoint[0]=0;//熱點更新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]))//無法繪制開始圖形,游戲結束{system("cls");SetPos(25,15);cout<<"游戲結束!!!最終得分為:"<<point<<endl;system("pause");exit(0);}}else{Redraw(hotpoint[0],hotpoint[1],ID);//沒有到底,方塊下移一位hotpoint[0]++;//熱點下移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==' ')//轉動方塊{temp=ID;Turn();if(Judge(hotpoint[0],hotpoint[1])==0){Redraw(hotpoint[0],hotpoint[1],temp);DrawBox(hotpoint[0],hotpoint[1],ID);}elseID=temp;}while(kbhit())//讀掉剩下的鍵盤信息getch();}Sleep(1);//等待1毫秒i++;//計數器加1} }int Box::Judge(int x,int y)//判斷當前是否可以繪制方塊 {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)//不能,返回1return 1;}return 0; }int main()//主函數 {Box game;game.Welcome();system("cls");game.DrawMap();game.Run();system("pause"); }

?

總結

以上是生活随笔為你收集整理的俄罗斯方块(C++)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。