C++實現(xiàn)彩色飛機(jī)大戰(zhàn)
更新時間:2020年10月29日 12:41:02 作者:研究猿小劉
這篇文章主要為大家詳細(xì)介紹了C++實現(xiàn)彩色飛機(jī)大戰(zhàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了C++實現(xiàn)彩色飛機(jī)大戰(zhàn)的具體代碼,供大家參考,具體內(nèi)容如下
1.基本的能夠?qū)崿F(xiàn)鍵盤按上下左右進(jìn)行控制飛機(jī),擊殺敵人飛機(jī),記錄得分,(缺點:死亡后不能從新玩,需要重新啟動程序,),缺點將在2中解決
/*隱藏光標(biāo)的代碼 #include <stdio.h> #include <windows.h> int main() { HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_CURSOR_INFO CursorInfo; GetConsoleCursorInfo(handle, &CursorInfo);//獲取控制臺光標(biāo)信息 CursorInfo.bVisible = false; //隱藏控制臺光標(biāo) SetConsoleCursorInfo(handle, &CursorInfo);//設(shè)置控制臺光標(biāo)狀態(tài) getchar(); }*/ /* 明白兩個事實, 敵人飛機(jī)和自己的飛機(jī)的橫坐標(biāo)和縱坐標(biāo)相同時 表示死亡 敵人飛機(jī)和自己的子彈碰撞即 子彈坐標(biāo)和自己子彈的坐標(biāo)相同時,飛機(jī)死亡并且加分, */ #include "stdio.h" #include <windows.h> #include <conio.h> #include <time.h> #define Esc 27 //退出 #define Up 72 //上,下,左,右 #define Down 80 #define Left 75 #define Right 77 #define Kong 32 //發(fā)射子彈 int x = 10; //飛機(jī)坐標(biāo) int y = 18; int d2 = 10;//敵機(jī)坐標(biāo) int d1 = 10; int d = 10;//d 和r 用來進(jìn)行碰撞檢測 int r = 1; int r1 = 1; int r2 = 1; int t = 1; // 游戲結(jié)束 int f = 0; // 計分?jǐn)?shù) int m = 5; // 敵機(jī)數(shù) int j = 0; // 殲敵數(shù) char p; // 接受按鍵 void kongzhi(int bx, int by);//聲明函數(shù) void huatu(); void gotoxy(int x, int y) //移動坐標(biāo) { COORD coord; coord.X = x; coord.Y = y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),3); } void gotoxy_red(int x, int y) //移動坐標(biāo) { COORD coord; coord.X = x; coord.Y = y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),4); } void gotoxy_blue(int x, int y) //移動坐標(biāo) { COORD coord; coord.X = x; coord.Y = y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),1); } void gotoxy_green(int x, int y) //移動坐標(biāo) { COORD coord; coord.X = x; coord.Y = y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),2); } void hidden()//隱藏光標(biāo),不讓光標(biāo)顯示 { HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_CURSOR_INFO cci; GetConsoleCursorInfo(hOut, &cci); cci.bVisible = 0;//賦1為顯示,賦0為隱藏 SetConsoleCursorInfo(hOut, &cci); } //************************************************************************************** //說明 void shuoming() { printf("\t\t\t\n\n\n\n"); gotoxy_blue(0, 0); printf("\t\t\t\t\t\t\tPlane Control\n\n" "\t\t\t\t\t\t\t\tUP\n\n" "\t\t\t\t\t\t\tDown\n\n" "\t\t\t\t\t\t\tLeft \n\n" "\t\t\t\t\t\t\tRight \n\n" "\t\t\t\t\t\t\t bullet space\n\n\n" "\t\t\t\t\t\t\tQuit Esc\n"); gotoxy_red(0, 0); } //**************************************************************************************** //判斷我機(jī)死沒死/游戲結(jié)束 void byebye() { if (((x == d && y == r) || (x == d1 && y == r1) || (x == d2 && y == r2))||( (d>=19||r>=19)||(d1>=19||r1>=19)||(d1>=19||r1>=19) )) { gotoxy(1, 3); printf(" !!! 游戲結(jié)束 !!!\n" "*******************\n" " 您的總得分: %d\n\n" " 敵機(jī)數(shù): %d\n" " 殲敵數(shù): %d\n" " 命中率: %.0f %%\n" "*******************\n", f, m, j, ((float)j / (float)m) * 100); while (!_kbhit()) { Sleep(500); gotoxy(1, 12); printf(" 繼續(xù)請按任意鍵...\n\n\n"); Sleep(900); gotoxy(1, 12); printf(" "); } gotoxy_red(0, 0); huatu(); f = 0; m = 0; j = 0; if (x >= 18) x--; else x++; gotoxy(x, y); printf("A"); } } // 計分/更新敵機(jī) void jifan() { //x,y是子彈的坐標(biāo) if (x == d && y == r) // d=10, r=1, d,d1,d2 是敵機(jī)的x軸, 為10 ,r為敵機(jī)的縱坐標(biāo) { gotoxy(d, r); printf("3"); Sleep(200); gotoxy(d, r); printf(" "); f += 2; r = 0; j++;//讓r=0,即是讓敵人的飛機(jī)消失 } if (x == d1 && y == r1) { gotoxy(d1, r1); printf("1"); Sleep(200); gotoxy(d1, r1); printf(" "); f += 3; r1 = 0; j++; } if (x == d2 && y == r2) { gotoxy(d2, r2); printf("0"); Sleep(200); gotoxy(d2, r2); printf(" "); f += 1; r2 = 0; j++; } gotoxy(57, 2); printf("%d\n", f); } //畫圖 void huatu() { int i, n; for (i = 0; i <= 20; i++) { for (n = 0; n <= 20; n++) { printf("*"); } printf("\n"); } for (i = 1; i <= 19; i++) { for (n = 1; n <= 19; n++) { gotoxy_red(i, n); printf(" "); } } } //隨機(jī)產(chǎn)生敵機(jī) void dfeiji() { while (t) { if (!r) { d = rand() % 17 + 1; m++; } //r,r1,r2 初始值都為1,當(dāng)變?yōu)?的時候開始產(chǎn)生隨機(jī)數(shù) if (!r1) { d1 = rand() % 17 + 1; m++; } if (!r2) { d2 = rand() % 17 + 1; m++; } while (t) { r=r+2; r1=r1+2; r2=r2+2; gotoxy(d, r); printf("b");//d,d1, d2 為敵機(jī)產(chǎn)生的位置,都為10 gotoxy(d1, r1); printf("c"); gotoxy(d2, r2); printf("d"); Sleep(900); gotoxy(d, r); printf(" "); gotoxy(d1, r1); printf(" "); gotoxy(d2, r2); printf(" "); kongzhi(0, 0);//控制飛機(jī)后,要立即進(jìn)行判斷 byebye();//判斷飛機(jī)有沒有死亡 if (r == 19) r = 0; if (r1 == 19) r1 = 0; if (r2 == 19) r2 = 0; if (r == 0 || r1 == 0 || r2 == 0) break; } } } //操控飛機(jī) void kongzhi(int bx, int by)//調(diào)用的時候傳入了 0, 0 { int a; while (_kbhit()) { if ((p = _getch()) == -32) p = _getch(); a = p; gotoxy(22, 5); switch (a) {//控制方向 case Up:if (y != 1) { gotoxy(x, y); printf(" "); y--; gotoxy(x, y); printf("A"); }break; case Down:if (y != 18) { gotoxy(x, y); printf(" "); y++; gotoxy(x, y); printf("A"); }break; case Left:if (x != 1) { gotoxy(x, y); printf(" "); x--; gotoxy(x, y); printf("A"); }break; case Right:if (x != 18) { gotoxy(x, y); printf(" "); x++; gotoxy(x, y); printf("A"); }break; case Kong: { bx = y;//先把y的值存起來,存到bx for (by = y; by > 1;) //發(fā)射子彈, y軸坐標(biāo)一直減減,打印 | { by--;//y的坐標(biāo) gotoxy(x, by); printf("|"); Sleep(10); gotoxy(x, by); printf(" "); y = by;//記錄子彈打到哪了,好進(jìn)行碰撞檢測 jifan();//計分?jǐn)?shù) if (r == 0 || r1 == 0 || r2 == 0) break; } y = bx;//恢復(fù)y的值 }break; case Esc:t = 0; break; //退出 default:break; } } } int main() { srand(time(NULL)); shuoming();//打印游戲說明,之后讓光標(biāo)進(jìn)入0,0 hidden();//隱藏光標(biāo),不讓光標(biāo)顯示 huatu();//畫出墻壁 gotoxy(x, y);//x=10,y=8, x 和y 是自己飛機(jī)的坐標(biāo),是全局變量 printf("A"); gotoxy(50, 2); printf("Score:"); while (t) //t是一個全局變量 初始值為1 { kongzhi(0, 0);//調(diào)用控制飛機(jī)函數(shù), (操作飛機(jī)后并記分?jǐn)?shù)) if (t) //如果游戲沒有結(jié)束,則 產(chǎn)生敵機(jī) dfeiji();//產(chǎn)生敵機(jī) ,并判斷飛機(jī)有沒有死亡 } }
2.(封裝了一個函數(shù))結(jié)束游戲后能夠重新開始進(jìn)行下一局
/*隱藏光標(biāo)的代碼 #include <stdio.h> #include <windows.h> int main() { HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_CURSOR_INFO CursorInfo; GetConsoleCursorInfo(handle, &CursorInfo);//獲取控制臺光標(biāo)信息 CursorInfo.bVisible = false; //隱藏控制臺光標(biāo) SetConsoleCursorInfo(handle, &CursorInfo);//設(shè)置控制臺光標(biāo)狀態(tài) getchar(); }*/ /* 明白兩個事實, 敵人飛機(jī)和自己的飛機(jī)的橫坐標(biāo)和縱坐標(biāo)相同時 表示死亡 敵人飛機(jī)和自己的子彈碰撞即 子彈坐標(biāo)和自己子彈的坐標(biāo)相同時,飛機(jī)死亡并且加分, */ #include "stdio.h" #include <windows.h> #include <conio.h> #include <time.h> #define Esc 27 //退出 #define Up 72 //上,下,左,右 #define Down 80 #define Left 75 #define Right 77 #define Kong 32 //發(fā)射子彈 int x = 10; //飛機(jī)坐標(biāo) int y = 18; int d2 = 10;//敵機(jī)坐標(biāo) int d1 = 10; int d = 10;//d 和r 用來進(jìn)行碰撞檢測 int r = 1; int r1 = 1; int r2 = 1; int t = 1; // 游戲結(jié)束 int f = 0; // 計分?jǐn)?shù) int m = 5; // 敵機(jī)數(shù) int j = 0; // 殲敵數(shù) char p; // 接受按鍵 void kongzhi(int bx, int by);//聲明函數(shù) void huatu(); void gotoxy(int x, int y) //移動坐標(biāo) { COORD coord; coord.X = x; coord.Y = y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),3); } void gotoxy_red(int x, int y) //移動坐標(biāo) { COORD coord; coord.X = x; coord.Y = y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),4); } void gotoxy_blue(int x, int y) //移動坐標(biāo) { COORD coord; coord.X = x; coord.Y = y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),1); } void gotoxy_green(int x, int y) //移動坐標(biāo) { COORD coord; coord.X = x; coord.Y = y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),2); } void hidden()//隱藏光標(biāo),不讓光標(biāo)顯示 { HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_CURSOR_INFO cci; GetConsoleCursorInfo(hOut, &cci); cci.bVisible = 0;//賦1為顯示,賦0為隱藏 SetConsoleCursorInfo(hOut, &cci); } //************************************************************************************** //說明 void shuoming() { printf("\t\t\t\n\n\n\n"); gotoxy_blue(0, 0); printf("\t\t\t\t\t\t\tPlane Control\n\n" "\t\t\t\t\t\t\t\tUP\n\n" "\t\t\t\t\t\t\tDown\n\n" "\t\t\t\t\t\t\tLeft \n\n" "\t\t\t\t\t\t\tRight \n\n" "\t\t\t\t\t\t\t bullet space\n\n\n" "\t\t\t\t\t\t\tQuit Esc\n"); gotoxy_red(0, 0); } //**************************************************************************************** //判斷我機(jī)死沒死/游戲結(jié)束 void byebye() { if (((x == d && y == r) || (x == d1 && y == r1) || (x == d2 && y == r2))||( (d>=19||r>=19)||(d1>=19||r1>=19)||(d1>=19||r1>=19) )) { gotoxy(1, 3); printf(" !!! game over !!!\n" "*******************\n" " score: %d\n\n" " di ji shu: %d\n" " jian di shu: %d\n" " ming zhong lv: %.0f %%\n" "*******************\n", f, m, j, ((float)j / (float)m) * 100); t=0; } } // 計分/更新敵機(jī) void jifan() { //x,y是子彈的坐標(biāo) if (x == d && y == r) // d=10, r=1, d,d1,d2 是敵機(jī)的x軸, 為10 ,r為敵機(jī)的縱坐標(biāo) { gotoxy(d, r); printf("3"); Sleep(200); gotoxy(d, r); printf(" "); f += 2; r = 0; j++;//讓r=0,即是讓敵人的飛機(jī)消失 } if (x == d1 && y == r1) { gotoxy(d1, r1); printf("1"); Sleep(200); gotoxy(d1, r1); printf(" "); f += 3; r1 = 0; j++; } if (x == d2 && y == r2) { gotoxy(d2, r2); printf("0"); Sleep(200); gotoxy(d2, r2); printf(" "); f += 1; r2 = 0; j++; } gotoxy(57, 2); printf("%d\n", f); } //畫圖 void huatu() { int i, n; for (i = 0; i <= 20; i++) { for (n = 0; n <= 20; n++) { printf("*"); } printf("\n"); } for (i = 1; i <= 19; i++) { for (n = 1; n <= 19; n++) { gotoxy_red(i, n); printf(" "); } } } //隨機(jī)產(chǎn)生敵機(jī) void dfeiji() { while (t) { if (!r) { d = rand() % 17 + 1; m++; } //r,r1,r2 初始值都為1,當(dāng)變?yōu)?的時候開始產(chǎn)生隨機(jī)數(shù) if (!r1) { d1 = rand() % 17 + 1; m++; } if (!r2) { d2 = rand() % 17 + 1; m++; } while (t) { r=r+2; r1=r1+2; r2=r2+2; gotoxy(d, r); printf("b");//d,d1, d2 為敵機(jī)產(chǎn)生的位置,都為10 gotoxy(d1, r1); printf("c"); gotoxy(d2, r2); printf("d"); Sleep(900); gotoxy(d, r); printf(" "); gotoxy(d1, r1); printf(" "); gotoxy(d2, r2); printf(" "); kongzhi(0, 0);//控制飛機(jī)后,要立即進(jìn)行判斷 byebye();//判斷飛機(jī)有沒有死亡 if (r == 19) r = 0; if (r1 == 19) r1 = 0; if (r2 == 19) r2 = 0; if (r == 0 || r1 == 0 || r2 == 0) break; } } } //操控飛機(jī) void kongzhi(int bx, int by)//調(diào)用的時候傳入了 0, 0 { int a; while (_kbhit()) { if ((p = _getch()) == -32) p = _getch(); a = p; gotoxy(22, 5); switch (a) {//控制方向 case Up:if (y != 1) { gotoxy(x, y); printf(" "); y--; gotoxy(x, y); printf("A"); }break; case Down:if (y != 18) { gotoxy(x, y); printf(" "); y++; gotoxy(x, y); printf("A"); }break; case Left:if (x != 1) { gotoxy(x, y); printf(" "); x--; gotoxy(x, y); printf("A"); }break; case Right:if (x != 18) { gotoxy(x, y); printf(" "); x++; gotoxy(x, y); printf("A"); }break; case Kong: { bx = y;//先把y的值存起來,存到bx for (by = y; by > 1;) //發(fā)射子彈, y軸坐標(biāo)一直減減,打印 | { by--;//y的坐標(biāo) gotoxy(x, by); printf("|"); Sleep(10); gotoxy(x, by); printf(" "); y = by;//記錄子彈打到哪了,好進(jìn)行碰撞檢測 jifan();//計分?jǐn)?shù) if (r == 0 || r1 == 0 || r2 == 0) break; } y = bx;//恢復(fù)y的值 }break; case Esc:t = 0; break; //退出 default:break; } } } void zuzhong(){ x = 10; //飛機(jī)坐標(biāo) y = 18; d2 = 10;//敵機(jī)坐標(biāo) d1 = 10; d = 10;//d 和r 用來進(jìn)行碰撞檢測 r = 1; r1 = 1; r2 = 1; t = 1; // 游戲結(jié)束 f = 0; // 計分?jǐn)?shù) m = 5; // 敵機(jī)數(shù) j = 0; // 殲敵數(shù) char p; // 接受按鍵 srand(time(NULL)); shuoming();//打印游戲說明,之后讓光標(biāo)進(jìn)入0,0 hidden();//隱藏光標(biāo),不讓光標(biāo)顯示 huatu();//畫出墻壁 gotoxy(x, y);//x=10,y=8, x 和y 是自己飛機(jī)的坐標(biāo),是全局變量 printf("A"); gotoxy(50, 2); printf("Score:"); while (t) //t是一個全局變量 初始值為1 { kongzhi(0, 0);//調(diào)用控制飛機(jī)函數(shù), (操作飛機(jī)后并記分?jǐn)?shù)) if (t) //如果游戲沒有結(jié)束,則 產(chǎn)生敵機(jī) dfeiji();//產(chǎn)生敵機(jī) ,并判斷飛機(jī)有沒有死亡 } } int main() { while(1){ system("cls"); zuzhong(); printf("please enter Enter key contine"); getchar(); } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C/C++ 中sizeof(''a'')對比詳細(xì)介紹
這篇文章主要介紹了C/C++ 中sizeof('a')的值對比詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下2017-02-02解決Devc++運(yùn)行窗口中文亂碼的實現(xiàn)步驟
本文主要介紹了如何解決Devc++運(yùn)行窗口中文亂碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06