C++俄羅斯方塊
阿新 • • 發佈:2018-12-19
俄羅斯方塊
//這是main函式CPP: #include <iostream> #include <windows.h> #include "fire.h" #include<time.h> void main() { srand((unsigned)time(NULL)); init(); while (1) { MoveCursor(13, 1);std::cout<<"Next ..."; if(lose) { MoveCursor(1, 5); std::cout<<" GAME OVER\n"; MoveCursor(3, 6); std::cout<<"<1> restart"; MoveCursor(3, 7); std::cout<<"<2> exit"; if(GetAsyncKeyState('1')&1)init(); if(GetAsyncKeyState('2')&1)return; MoveCursor(0, 22); system("pause"); } //system("cls"); draw(); PD_chongdie(); MoveCursor(14, 10); std::cout<<"操作指南"; MoveCursor(20, 12); std::cout<<"★ A左.D右.S快速下 ★"; MoveCursor(20, 14); std::cout<<"★ .< W >. 變形 ★"; MoveCursor(14, 18); std::cout<<"遊戲得分"; MoveCursor(20, 18); std::cout<<"★ "<<jifen<<" ★"; switch (level) { case 1:MoveCursor(13, 8);std::cout<<"LEVEL ☆ ONE";break; case 2:MoveCursor(13, 8);std::cout<<"LEVEL ☆ TWO";break; case 3:MoveCursor(13, 8);std::cout<<"LEVEL ☆ THERE";break; case 4:MoveCursor(13, 8);std::cout<<"LEVEL ☆ GOLD";break; } if(jifen<20)level = 1; else if(jifen >= 20 &&jifen <30)level = 2; else if(jifen >= 30 &&jifen <50)level = 3; else level = 4; if(mx==false) { if(jifen<20)Sleep(300); else if(jifen >= 20 &&jifen <30)Sleep(200); else if(jifen >= 30 &&jifen <50)Sleep(100); else Sleep(50); } } }
//這是一個頭檔案fire.h
extern int jifen;
extern bool lose;
extern bool mx;
extern int level;
void init();
void draw();
void run();
void MoveCursor(int x, int y);
void PD_chongdie();
//這是另一個頭檔案quanjubianliang.h int smap[] = { 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0 }; int Amap[] = { 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,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 1,0,0,0, 1,0,0,0, 1,0,0,0} }; int temp1[4][16]={ { 0,1,0,0, 1,1,0,0, 0,1,0,0, 0,0,0,0}, { 0,0,0,0, 1,1,1,0, 0,1,0,0, 0,0,0,0}, { 1,0,0,0, 1,1,0,0, 1,0,0,0, 0,0,0,0}, { 0,1,0,0, 1,1,1,0, 0,0,0,0, 0,0,0,0} }; int temp2[4][16]={ { 0,0,0,0, 1,1,1,0, 1,0,0,0, 0,0,0,0}, { 1,0,0,0, 1,0,0,0, 1,1,0,0, 0,0,0,0}, { 0,0,1,0, 1,1,1,0, 0,0,0,0, 0,0,0,0}, { 1,1,0,0, 0,1,0,0, 0,1,0,0, 0,0,0,0} }; int temp3[4][16]={ { 1,0,0,0, 1,1,1,0, 0,0,0,0, 0,0,0,0}, { 0,1,0,0, 0,1,0,0, 1,1,0,0, 0,0,0,0}, { 0,0,0,0, 1,1,1,0, 0,0,1,0, 0,0,0,0}, { 1,1,0,0, 1,0,0,0, 1,0,0,0, 0,0,0,0} }; int temp4[2][16]={ { 1,0,0,0, 1,1,0,0, 0,1,0,0, 0,0,0,0}, { 0,1,1,0, 1,1,0,0, 0,0,0,0, 0,0,0,0} }; int temp5[2][16]={ { 0,1,0,0, 1,1,0,0, 1,0,0,0, 0,0,0,0}, { 1,1,0,0, 0,1,1,0, 0,0,0,0, 0,0,0,0} }; int temp6[2][16]={ { 0,0,0,0, 1,1,1,1, 0,0,0,0, 0,0,0,0}, { 1,0,0,0, 1,0,0,0, 1,0,0,0, 1,0,0,0} };
//這是zal.cpp #include <iostream> #include <windows.h> #include"quanjubianliang.h" #define W 10 #define H 20 #define S (W*H) void SetColor(int color)//---------------------------------設定顏色 { HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleTextAttribute(hOut, color); } void MoveCursor(int x, int y)//-----------------------------設定游標位置 { HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);///獲取輸出流控制代碼 COORD coord;///定義COORD結構體(包含X和Y兩個成員) coord.X = x * 2; ///設定結構體X軸 coord.Y = y; ///設定結構體Y軸 SetConsoleCursorPosition(hOut, coord);///設定游標位置 CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };///修改游標寬頻和顯示 SetConsoleCursorInfo(hOut, &cursor_info);///設定游標不可見 } void jiance(); int px = W/2; int py = 0; int n=0; int m=0; int temp[16]={}; int bx=0; int jifen=0; int level=1; bool lose =false; bool jian=false; bool mx=false; void init() { px = W/2-2; py = 0; level=1; lose =false; bx=0; jifen=0; jian=false; mx=false; m=rand()%7; n=rand()%7; for(int i=0;i<16;++i) { smap[i]=ST_X[n][i]; temp[i]=ST_X[m][i]; } for(int i=0;i<S;++i) { bmap[i]=Amap[i]; } } void draw() { for(int y=0;y<4;++y) { MoveCursor(16, 3+y); for(int x=0;x<4;++x) { if(temp[x+4*y]==1) std::cout<<"■"; else std::cout<<" "; } std::cout<<std::endl; } MoveCursor(0, 0); for (int y = 0;y < H; ++y) { std::cout<<"■"; for (int x = 0; x < W; ++x) { if (x >= px && x < px + 4 && y >= py && y < py + 4 && smap[(x-px)+(y-py)*4] == 1 ) std::cout<<"□"; else switch(bmap[x+y*W]) { case 0:SetColor(14);std::cout<<" ";break; case 1:SetColor(11);std::cout<<"■";break; case 3:SetColor(14);std::cout<<"■";break; } }std::cout<<"■\n"; } } void PD_chongdie() { if(jian)jiance(); bool chongdie = false; bool xzc=false; bool xyc=false; for (int x = 0; x < 4; ++x) { for (int y = 0; y < 4; ++y) { if (smap[x+y*4] == 1) { if ( ( py+y+2) >= H || bmap[px+x+(py+y+1)*W] == 1 ) { chongdie = true; break; } } } } if (chongdie) //--------------------------重疊合並 { for (int y = 0; y < 4; ++y) { for (int x = 0; x < 4; ++x) { if (smap[x+y*4] == 1) bmap[px+x+(py+y)*W] = 1; } } n=m; jian=true; mx=false; py = 0; px = W/2-2;//--------------新方塊---- bx=0; m=rand()%7; for (int i = 0; i < 4*4; ++i) { smap[i]=temp[i];//ST_X[n][i] } for (int i = 0; i < 4*4; ++i) { temp[i]=ST_X[m][i]; } } else py++; bool xz=true; bool xy=true; if ((GetAsyncKeyState('A')&1)&& px> 0 && xzc == false ) { for (int x = 0; x < 4; ++x) { for (int y = 0; y < 4; ++y) { if (smap[x+y*4] == 1) { if (bmap[px+x-1+(py+y)*W] == 1 ) { xzc = true; break; } } } } if(xzc == false) px--; } if (GetAsyncKeyState('S')&1)mx=true; if (GetAsyncKeyState('D')&1) { int wx=0; for(int y=0;y<4;++y) { for(int x=0;x<4;++x) { if( smap[x+y*4]== 1 ) { if(x>wx)wx=x; } } } for (int x = 0; x < 4; ++x) { for (int y = 0; y < 4; ++y) { if (smap[x+y*4] == 1) { if (bmap[px+x+1+(py+y)*W] == 1 ) { xyc = true; break; } } } } if((px+wx+1<W)&& xyc==false) px += 1; } if (GetAsyncKeyState('W')&1) { bx++; switch (n) { case 1:if(bx==5)bx=1; for(int i=0;i<16;++i) {smap[i] = temp1[bx-1][i];}break; case 2:if(bx==5)bx=1; for(int i=0;i<16;++i) {smap[i] = temp2[bx-1][i] ;} break; case 3:if(bx==5)bx=1; for(int i=0;i<16;++i) {smap[i] = temp3[bx-1][i] ;} break; case 4:if(bx==3)bx=1; for(int i=0;i<16;++i) {smap[i] = temp4[bx-1][i];} break; case 5:if(bx==3)bx=1; for(int i=0;i<16;++i) {smap[i] = temp5[bx-1][i];} break; case 6:if(bx==3)bx=1; for(int i=0;i<16;++i) {smap[i] = temp6[bx-1][i];} break; } } } void jiance() { int xiao = 0; int wz_y[4]={}; for(int y=0;y<H;++y) { int yi=0; for(int x=0;x<W;++x) { if(bmap[x+y*W]== 0)break; else if(bmap[x+y*W]== 1)yi++; } if(yi==W)wz_y[xiao++]=y; } if(xiao) { bool xialuo=false; for(int j=0;j<4;++j)//---消除 { for(int i=0;i<S;++i) { if(bmap[i%W+wz_y[j]*W]==1) { bmap[i%W+wz_y[j]*W]=0; xialuo=true; } } if(xialuo) { for(int i=S-1;i>=0;--i) { if(i/W < wz_y[j])bmap[i+W]=bmap[i]; } } } switch (xiao)//----記分 { case 1:jifen++ ;break; case 2:jifen+= 3 ;break; case 3:jifen += 5 ;break; case 4:jifen += 8 ;break; } } jian=false; for(int i=0;i<W;++i) { if(bmap[i] != 0 )lose = true; } }