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

歡迎訪問 生活随笔!

生活随笔

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

c/c++

C++坦克大战源代码

發布時間:2023/12/10 c/c++ 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++坦克大战源代码 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

效果截圖:

?源碼

#include <iostream> #include <time.h> #include <windows.h>#define W 1 //上 #define S 2 //下 #define A 3 //左 #define D 4 //右 #define L 5 // 坦克有4條命void HideCursor() { //隱藏光標 CONSOLE_CURSOR_INFO cursor_info = { 1,0 };SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info); } void GoToxy(int x, int y) { //光標移動,X、Y表示橫、縱坐標COORD coord = { x, y };SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); }//全局變量 int map[50][40];//地圖二維數組 int B_num; //子彈編號 int Pos; //敵方坦克生成位置,-1為左邊,0為中間,1為右邊,2為我的坦克位置 int Speed = 7; //游戲速度 int Enemy; //還未出現的敵人 const char* Tank_Model[3][4] ={{"◢┃ ◣", "◢╦ ◣", "◢╦◣", "◢╦◣"},{"╠ █╣", "╠ █╣", "━█╣", "╠█━"},{"◥╩ ◤", "◥┃ ◤", "◥╩◤", "◥╩◤"}}; //坦克 class Tank{ public:int x, y; //中心坐標int Direction; //方向int Model; //模型int Revival; //復活次數int Num; //敵方坦克編號 bool Type; //我方坦克此參數為1bool Exist; //存活為1,不存活為0 }AI_tank[6], my_tank; //子彈 class Bullet{ public:int x, y; //坐標int Direction; //方向bool Exist; //1為存在,0不存在bool Type; //0為敵方子彈,1為我方子彈 }bullet[50] ;//基本函數 void GoToxy(int x, int y); //光標移動 void HideCursor(); //隱藏光標void Key(); //鍵盤輸入 void Init(); //初始化 void Pause(); //暫停 void Show(); //打印框架 void Print_Map(); //打印地圖 void Cheak_Game(); //檢測游戲勝負 void GameOver(); //游戲結束//坦克 void Creat_AI_T(Tank* AI_tank); //建立坦克 void Creat_My_T(Tank* my_tank); void Move_AI_T(Tank* AI_tank);//坦克移動 void Move_My_T(int turn); void Clear_T(int x, int y); //清除坦克 void Print_T(Tank tank); //打印坦克 bool Cheak_T(Tank tank, int direction); //檢測障礙,1阻礙//子彈 void Creat_AI_B(Tank* tank); //敵方坦克發射子彈 void Creat_My_B(Tank tank);//我方坦克發射子彈 void Move_B(Bullet bullet[50]); //子彈移動 void Break_B(Bullet* bullet); //子彈碰撞 void Print_B(int x, int y);//打印子彈 void Clear_B(int x, int y); //清除子彈 int Cheak_B(int x, int y); //子彈前方情況void Show() { //打印框架 std::cout << " ▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁";std::cout << "▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁\n";for (int i = 0; i < 48; i++) {std::cout << "▕ ▏\n";}std::cout << " ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔";std::cout << "▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔\n"; } void Print_Map() { // 打印地圖 int Map[50][40] = { //map里的值: 0為可通過陸地,1為磚,6為墻,100~105為敵方坦克,200為我的坦克,{ 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4 },{ 4,0,0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,4,4,4,4,4 }};for (int i = 0; i < 50; i++)for (int j = 0; j < 40; j++) map[i][j] = Map[i][j];for (int i = 0; i < 50; i++)for (int j = 0; j < 40; j++) if (map[i][j] == 1) {GoToxy(2 * j, i);std::cout << "▓";} else if (map[i][j] == 6) {GoToxy(2 * j, i);std::cout << "■";} GoToxy(38, 46); std::cout << " ◣◢";GoToxy(38, 47); std::cout << "◣█ ◢";GoToxy(38, 48); std::cout << "◢█ ◣"; } void Cheak_Game() {//敵人坦克全部不存活if (Enemy <= 0 && !AI_tank[0].Exist && !AI_tank[1].Exist && !AI_tank[2].Exist&& !AI_tank[3].Exist && !AI_tank[4].Exist && !AI_tank[5].Exist)GameOver();if (my_tank.Revival >= L)//我復活次數用完GameOver();//游戲結束 } void GameOver() {bool home = 1;while (home) { GoToxy(37, 21);std::cout << "游戲結束!"; if (GetAsyncKeyState(0xD) & 0x8000) { //回車鍵system("cls"); //清屏Show();Init(); //初始化break;}else if (GetAsyncKeyState(0x1B) & 0x8000) //Esc鍵退出 exit(0);} } void Creat_My_T(Tank* my_tank) {//建立我的坦克my_tank->x = 15;my_tank->y = 47;my_tank->Direction = 1;// my_tank->Model = 0;my_tank->Exist = 1;my_tank->Type = 1;Print_T(*my_tank); //打印我的坦克 } void Move_My_T(int turn) {//turn為Key()函數傳入的方向值Clear_T(my_tank.x, my_tank.y);my_tank.Direction = turn; if (Cheak_T(my_tank, my_tank.Direction)) //我方坦克當前方向上無障礙switch (turn) {case W: my_tank.y--; break; //上case S: my_tank.y++; break; //下case A: my_tank.x--; break; //左case D: my_tank.x++; break; //右} Print_T(my_tank); } void Print_T(Tank tank) {//打印for (int i = 0; i < 3; i++) {GoToxy((tank.x - 1) * 2, tank.y - 1 + i);//在坦克中心坐標的左邊,上中下三行打印std::cout << Tank_Model[i][tank.Direction - 1]; //打印的是地址,地址既字符串for (int j = 0; j < 3; j++)if (tank.Type)//若為我的坦克map[tank.y + j - 1][tank.x + i - 1] = 200;//在map上敵方值為100~105,我方為200elsemap[tank.y + j - 1][tank.x + i - 1] = 100 +tank.Num;//這樣可以通過map值讀取坦克編號} } void Creat_AI_T(Tank* AI_tank) {AI_tank->x = 19 + 17 * (Pos); //pos為坦克生成位置,-1為左位置,0為中間,1為右,2為我的坦克位置AI_tank->y = 2;AI_tank->Direction = 2; //方向朝下AI_tank->Revival++; //復活次數+1AI_tank->Exist = 1;//存在Pos++;Enemy--;if (Pos == 2) //循環重置(pos只能為-1,0,1)Pos = -1;Print_T(*AI_tank);return; } void Move_AI_T(Tank* AI_tank) { if (AI_tank->Exist) { //存在 Clear_T(AI_tank->x, AI_tank->y);if (Cheak_T(*AI_tank, AI_tank->Direction))//前方無障礙switch (AI_tank->Direction) {case W: AI_tank->y--; break; //上case S: AI_tank->y++; break; //下case A: AI_tank->x--; break; //左case D: AI_tank->x++; break; //右}else {//前方有障礙 for (int i = rand() % 4 + 1; i <= 4; i++)if (Cheak_T(*AI_tank, i)){ //循環判斷,返1可通過AI_tank->Direction = i;break;}}Print_T(*AI_tank); //打印敵方坦克} } bool Cheak_T(Tank tank, int direction) { //檢測坦克前方障礙,返1為可通過switch (direction) { case W: if (map[tank.y - 2][tank.x] == 0 && map[tank.y - 2][tank.x - 1] == 0 && map[tank.y - 2][tank.x + 1] == 0)return 1;else return 0;case S:if (map[tank.y + 2][tank.x] == 0 && map[tank.y + 2][tank.x - 1] == 0 && map[tank.y + 2][tank.x + 1] == 0)return 1;else return 0;case A:if (map[tank.y][tank.x - 2] == 0 && map[tank.y - 1][tank.x - 2] == 0 && map[tank.y + 1][tank.x - 2] == 0)return 1;else return 0;case D:if (map[tank.y][tank.x + 2] == 0 && map[tank.y - 1][tank.x + 2] == 0 && map[tank.y + 1][tank.x + 2] == 0)return 1;else return 0;default: return 0;} } void Clear_T(int x, int y) { //清除坦克for (int i = 0; i <= 2; i++)for (int j = 0; j <= 2; j++) {//將坦克占用的地圖清零map[y + j - 1][x + i - 1] = 0;GoToxy(2 * x + 2 * j - 2, y + i - 1);std::cout << " ";} }//鍵盤輸入 void Key() { //上下左右鍵if (GetAsyncKeyState('W') & 0x8000)Move_My_T(W);else if (GetAsyncKeyState('S') & 0x8000)Move_My_T(S);else if (GetAsyncKeyState('A') & 0x8000)Move_My_T(A);else if (GetAsyncKeyState('D') & 0x8000)Move_My_T(D); //子彈發射else if (GetAsyncKeyState('P') & 0x8000) {Creat_My_B(my_tank);}else if (GetAsyncKeyState(0x1B) & 0x8000)// Esc鍵退出exit(0); else if (GetAsyncKeyState(0x20) & 0x8000)//空格暫停Pause(); } void Pause() { //暫停while (1) {if (GetAsyncKeyState(0xD) & 0x8000) { //回車鍵繼續 break;}else if (GetAsyncKeyState(0x1B) & 0x8000) //Esc鍵退出 exit(0);} } void Creat_AI_B(Tank* tank){ //敵方發射子彈if (!(rand() % 10)) { //在隨后的每個游戲周期中有10分之一的可能發射子彈 Creat_My_B(*tank);} } void Creat_My_B(Tank tank) {switch (tank.Direction) { case W:bullet[B_num].x = tank.x;bullet[B_num].y = tank.y - 2;bullet[B_num].Direction = 1;//1表示向上break;case S:bullet[B_num].x = tank.x;bullet[B_num].y = tank.y + 2;bullet[B_num].Direction = 2;//2表示向下break;case A:bullet[B_num].x = tank.x - 2;bullet[B_num].y = tank.y;bullet[B_num].Direction = 3;//3表示向左break;case D:bullet[B_num].x = tank.x + 2;bullet[B_num].y = tank.y;bullet[B_num].Direction = 4;//4表示向右break;}bullet[B_num].Exist = 1; //子彈存在bullet[B_num].Type = tank.Type; //我方坦克發射的子彈bullet.Type=1B_num++;if (B_num == 50) //如果子彈編號增長到50號,那么重頭開始編號B_num = 0; //考慮到地圖上不可能同時存在50顆子彈,所以數組元素設置50個 } void Move_B(Bullet bullet[50]) { //子彈移動 for (int i = 0; i < 50; i++) {if (bullet[i].Exist) {//如果子彈存在 if (map[bullet[i].y][bullet[i].x] == 0) { Clear_B(bullet[i].x, bullet[i].y);//子彈當前位置無障礙,抹除子彈圖形switch (bullet[i].Direction) {//子彈變到下一個坐標case W:(bullet[i].y)--; break;case S:(bullet[i].y)++; break;case A:(bullet[i].x)--; break;case D:(bullet[i].x)++; break;}}//判斷子彈當前位置情況if (map[bullet[i].y][bullet[i].x] == 0) //子彈坐標無障礙Print_B(bullet[i].x, bullet[i].y);//打印else Break_B(&bullet[i]); //子彈碰撞 for (int j = 0; j < 50; j++) //子彈間的碰撞判斷,若是我方子彈和敵方子彈碰撞則都刪除,若為兩敵方子彈則無視if (bullet[j].Exist && j != i && (bullet[i].Type || bullet[j].Type) && bullet[i].x == bullet[j].x && bullet[i].y == bullet[j].y){ //同樣的兩顆我方子彈不可能產生碰撞bullet[j].Exist = 0;bullet[i].Exist = 0;Clear_B(bullet[j].x, bullet[j].y);break;}}} } void Break_B(Bullet* bullet) { int x = bullet->x; int y = bullet->y; //子彈坐標int i;if (map[y][x] == 1) { //子彈碰到磚塊 if (bullet->Direction == A || bullet->Direction == D) //若子彈是橫向的for (i = -1; i <= 1; i++)if (map[y + i][x] == 1) {map[y + i][x] = 0;GoToxy(2 * x, y + i);std::cout << " ";}if (bullet->Direction == W || bullet->Direction == S) //子彈是向上或是向下移動的for (i = -1; i <= 1; i++)if (map[y][x + i] == 1) { //如果子彈打中磚塊兩旁為磚塊,則刪除磚,若不是則忽略 map[y][x + i] = 0; //磚塊碎GoToxy(2 * (x + i), y);std::cout << " ";}bullet->Exist = 0; //子彈不存在}else if (map[y][x] == 4 || map[y][x] == 6) //子彈碰到邊框或者不可摧毀方塊bullet->Exist = 0;else if (bullet->Type ==1 && map[y][x] >= 100 && map[y][x] <= 105) { //我方子彈碰到了敵方坦克 AI_tank[(int)map[y][x] % 100].Exist = 0;bullet->Exist = 0; Clear_T(AI_tank[(int)map[y][x] % 100].x, AI_tank[(int)map[y][x] % 100].y); //清除坦克}else if (bullet->Type == 0 && map[y][x] == 200) { //若敵方子彈擊中我的坦克 my_tank.Exist = 0;bullet->Exist = 0;Clear_T(my_tank.x, my_tank.y);my_tank.Revival++; //我方坦克復活次數加1}else if (map[y][x] == 9) { //子彈碰到巢 bullet->Exist = 0;GoToxy(38, 46); std::cout << " "; GoToxy(38, 47); std::cout << " ";GoToxy(38, 48); std::cout << "◢◣ ";GameOver();} } int Cheak_B(int x, int y) {//子彈當前位置情況if (map[y][x] == 0)return 1;elsereturn 0; } void Print_B(int x, int y){GoToxy(2 * x, y);std::cout << "o"; } void Clear_B(int x, int y){GoToxy(2 * x, y);if (Cheak_B(x, y) == 1) {//子彈當前坐標在空地上 std::cout << " ";} }void Init() { //初始化Enemy = 24;my_tank.Revival = 0; //我的坦克復活次數為0Pos = 0;B_num = 0;Print_Map();Creat_My_T(&my_tank);for (int i = 0; i < 50; i++) {//子彈bullet[i].Exist = 0;}for (int i = 0; i <= 5; i++) {//敵方坦克AI_tank[i].Revival = 0;AI_tank[i].Exist = 0; //初始化坦克全是不存活的,用Creat_AI_T()建立不存活的坦克AI_tank[i].Num = i;AI_tank[i].Type = 0;} }int main() { int i;int gap[16] = { 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 }; //間隔數組,用于控制速度HideCursor(); //隱藏光標Show(); //打印框架Init(); //初始化while(1) {if (gap[0]++ % Speed == 0) {//速度調整, Cheak_Game(); //游戲勝負檢測for (i = 0; i <= 5; i++) {//敵方坦克移動循環if (gap[i + 7]++ % 3 == 0)Move_AI_T(&AI_tank[i]);}for (i = 0; i <= 5; i++)//建立敵方坦克if (AI_tank[i].Exist == 0 && AI_tank[i].Revival < 4 && gap[i+1]++ % 50 == 0) { //一個敵方坦克每局只有4條命//坦克死掉后間隔一段時間建立Creat_AI_T(&AI_tank[i]);break; } for (i = 0; i <= 5; i++)if (AI_tank[i].Exist)Creat_AI_B(&AI_tank[i]);if (my_tank.Exist && gap[14]++ % 2 == 0)Key();if (my_tank.Exist == 0 && my_tank.Revival < L && gap[15]++ % 15 == 0)//我方坦克復活Creat_My_T(&my_tank); Move_B(bullet); }Sleep(5);}return 0; }

制作不易,有問題請評論改正。

總結

以上是生活随笔為你收集整理的C++坦克大战源代码的全部內容,希望文章能夠幫你解決所遇到的問題。

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