C++实现彩色飞机大战
时间:2022-05-09 09:40:45|栏目:C代码|点击: 次
本文实例为大家分享了C++实现彩色飞机大战的具体代码,供大家参考,具体内容如下
1.基本的能够实现键盘按上下左右进行控制飞机,击杀敌人飞机,记录得分,(缺点:死亡后不能从新玩,需要重新启动程序,),缺点将在2中解决
/*隐藏光标的代码 #include <stdio.h> #include <windows.h> int main() { HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_CURSOR_INFO CursorInfo; GetConsoleCursorInfo(handle, &CursorInfo);//获取控制台光标信息 CursorInfo.bVisible = false; //隐藏控制台光标 SetConsoleCursorInfo(handle, &CursorInfo);//设置控制台光标状态 getchar(); }*/ /* 明白两个事实, 敌人飞机和自己的飞机的横坐标和纵坐标相同时 表示死亡 敌人飞机和自己的子弹碰撞即 子弹坐标和自己子弹的坐标相同时,飞机死亡并且加分, */ #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 //发射子弹 int x = 10; //飞机坐标 int y = 18; int d2 = 10;//敌机坐标 int d1 = 10; int d = 10;//d 和r 用来进行碰撞检测 int r = 1; int r1 = 1; int r2 = 1; int t = 1; // 游戏结束 int f = 0; // 计分数 int m = 5; // 敌机数 int j = 0; // 歼敌数 char p; // 接受按键 void kongzhi(int bx, int by);//声明函数 void huatu(); void gotoxy(int x, int y) //移动坐标 { 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) //移动坐标 { 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) //移动坐标 { 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) //移动坐标 { COORD coord; coord.X = x; coord.Y = y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),2); } void hidden()//隐藏光标,不让光标显示 { 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); } //**************************************************************************************** //判断我机死没死/游戏结束 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(" !!! 游戏结束 !!!\n" "*******************\n" " 您的总得分: %d\n\n" " 敌机数: %d\n" " 歼敌数: %d\n" " 命中率: %.0f %%\n" "*******************\n", f, m, j, ((float)j / (float)m) * 100); while (!_kbhit()) { Sleep(500); gotoxy(1, 12); printf(" 继续请按任意键...\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"); } } // 计分/更新敌机 void jifan() { //x,y是子弹的坐标 if (x == d && y == r) // d=10, r=1, d,d1,d2 是敌机的x轴, 为10 ,r为敌机的纵坐标 { gotoxy(d, r); printf("3"); Sleep(200); gotoxy(d, r); printf(" "); f += 2; r = 0; j++;//让r=0,即是让敌人的飞机消失 } 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(" "); } } } //随机产生敌机 void dfeiji() { while (t) { if (!r) { d = rand() % 17 + 1; m++; } //r,r1,r2 初始值都为1,当变为0的时候开始产生随机数 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 为敌机产生的位置,都为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);//控制飞机后,要立即进行判断 byebye();//判断飞机有没有死亡 if (r == 19) r = 0; if (r1 == 19) r1 = 0; if (r2 == 19) r2 = 0; if (r == 0 || r1 == 0 || r2 == 0) break; } } } //操控飞机 void kongzhi(int bx, int by)//调用的时候传入了 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;) //发射子弹, y轴坐标一直减减,打印 | { by--;//y的坐标 gotoxy(x, by); printf("|"); Sleep(10); gotoxy(x, by); printf(" "); y = by;//记录子弹打到哪了,好进行碰撞检测 jifan();//计分数 if (r == 0 || r1 == 0 || r2 == 0) break; } y = bx;//恢复y的值 }break; case Esc:t = 0; break; //退出 default:break; } } } int main() { srand(time(NULL)); shuoming();//打印游戏说明,之后让光标进入0,0 hidden();//隐藏光标,不让光标显示 huatu();//画出墙壁 gotoxy(x, y);//x=10,y=8, x 和y 是自己飞机的坐标,是全局变量 printf("A"); gotoxy(50, 2); printf("Score:"); while (t) //t是一个全局变量 初始值为1 { kongzhi(0, 0);//调用控制飞机函数, (操作飞机后并记分数) if (t) //如果游戏没有结束,则 产生敌机 dfeiji();//产生敌机 ,并判断飞机有没有死亡 } }
2.(封装了一个函数)结束游戏后能够重新开始进行下一局
/*隐藏光标的代码 #include <stdio.h> #include <windows.h> int main() { HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_CURSOR_INFO CursorInfo; GetConsoleCursorInfo(handle, &CursorInfo);//获取控制台光标信息 CursorInfo.bVisible = false; //隐藏控制台光标 SetConsoleCursorInfo(handle, &CursorInfo);//设置控制台光标状态 getchar(); }*/ /* 明白两个事实, 敌人飞机和自己的飞机的横坐标和纵坐标相同时 表示死亡 敌人飞机和自己的子弹碰撞即 子弹坐标和自己子弹的坐标相同时,飞机死亡并且加分, */ #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 //发射子弹 int x = 10; //飞机坐标 int y = 18; int d2 = 10;//敌机坐标 int d1 = 10; int d = 10;//d 和r 用来进行碰撞检测 int r = 1; int r1 = 1; int r2 = 1; int t = 1; // 游戏结束 int f = 0; // 计分数 int m = 5; // 敌机数 int j = 0; // 歼敌数 char p; // 接受按键 void kongzhi(int bx, int by);//声明函数 void huatu(); void gotoxy(int x, int y) //移动坐标 { 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) //移动坐标 { 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) //移动坐标 { 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) //移动坐标 { COORD coord; coord.X = x; coord.Y = y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),2); } void hidden()//隐藏光标,不让光标显示 { 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); } //**************************************************************************************** //判断我机死没死/游戏结束 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; } } // 计分/更新敌机 void jifan() { //x,y是子弹的坐标 if (x == d && y == r) // d=10, r=1, d,d1,d2 是敌机的x轴, 为10 ,r为敌机的纵坐标 { gotoxy(d, r); printf("3"); Sleep(200); gotoxy(d, r); printf(" "); f += 2; r = 0; j++;//让r=0,即是让敌人的飞机消失 } 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(" "); } } } //随机产生敌机 void dfeiji() { while (t) { if (!r) { d = rand() % 17 + 1; m++; } //r,r1,r2 初始值都为1,当变为0的时候开始产生随机数 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 为敌机产生的位置,都为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);//控制飞机后,要立即进行判断 byebye();//判断飞机有没有死亡 if (r == 19) r = 0; if (r1 == 19) r1 = 0; if (r2 == 19) r2 = 0; if (r == 0 || r1 == 0 || r2 == 0) break; } } } //操控飞机 void kongzhi(int bx, int by)//调用的时候传入了 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;) //发射子弹, y轴坐标一直减减,打印 | { by--;//y的坐标 gotoxy(x, by); printf("|"); Sleep(10); gotoxy(x, by); printf(" "); y = by;//记录子弹打到哪了,好进行碰撞检测 jifan();//计分数 if (r == 0 || r1 == 0 || r2 == 0) break; } y = bx;//恢复y的值 }break; case Esc:t = 0; break; //退出 default:break; } } } void zuzhong(){ x = 10; //飞机坐标 y = 18; d2 = 10;//敌机坐标 d1 = 10; d = 10;//d 和r 用来进行碰撞检测 r = 1; r1 = 1; r2 = 1; t = 1; // 游戏结束 f = 0; // 计分数 m = 5; // 敌机数 j = 0; // 歼敌数 char p; // 接受按键 srand(time(NULL)); shuoming();//打印游戏说明,之后让光标进入0,0 hidden();//隐藏光标,不让光标显示 huatu();//画出墙壁 gotoxy(x, y);//x=10,y=8, x 和y 是自己飞机的坐标,是全局变量 printf("A"); gotoxy(50, 2); printf("Score:"); while (t) //t是一个全局变量 初始值为1 { kongzhi(0, 0);//调用控制飞机函数, (操作飞机后并记分数) if (t) //如果游戏没有结束,则 产生敌机 dfeiji();//产生敌机 ,并判断飞机有没有死亡 } } int main() { while(1){ system("cls"); zuzhong(); printf("please enter Enter key contine"); getchar(); } }
上一篇:Cocos2d-x UI开发之CCControlColourPicker控件类使用实例
栏 目:C代码
本文标题:C++实现彩色飞机大战
本文地址:http://www.codeinn.net/misctech/201413.html