C语言代码实现飞机大战
本文实例为大家分享了C语言实现简单飞机大战的具体代码,供大家参考,具体内容如下
这个游戏的功能很单一,也就是“飞机大战”,哈哈哈哈。总共只有300多行代码左右,你也可以想想它会有多简陋,把它复制下来编译一下可以直接执行,需要的同学可以自取~
PS:我运行的环境是devc++,前提你要在C99的环境中执行
以下是源代码
#include#include #include //将用户从键盘获得的输入进行输出 #include //获得用户键盘的输入 //定义全局变量 inthigh,width;//定义边界 intposition_x,position_y;//飞机位置 intbullet_x,bullet_y;//子弹位置 intenemy_x,enemy_y;//敌军飞机 intscore;//获得分数 intflag;//飞机状态 voidgotoxy(intx,inty);//光标移动到(x,y)位置 voidwelcometogame();//初始化界面 intcolor(intc);//更改文字颜色 voidexplation();//游戏右侧显示 voidscoreandtips();//显示游戏提示 voidshow();//显示游戏界面 voidendgame();//游戏结束 /** *文字颜色函数 */ intcolor(intc) { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),c);//更改文字颜色 return0; } /** *设置光标位置 */ voidgotoxy(intx,inty) { COORDc; c.X=x; c.Y=y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),c); } voidwelcometogame()//开始界面 { intn; color(15); gotoxy(43,10); printf("飞机大战"); color(11); gotoxy(25,22); printf("1.开始游戏"); gotoxy(45,22); printf("2.游戏说明"); gotoxy(65,22); printf("3.退出游戏"); gotoxy(40,27); color(3); printf("请选择123:"); color(14); scanf("%d",&n);//输入选项 switch(n) { case1: system("cls"); show(); break; case2: explation();//游戏说明函数 break; case3: exit(0);//退出游戏 break; default: color(12); gotoxy(40,28); printf("请输入1-3之间的数!"); _getch();//输入任意键 system("cls");//清屏 welcometogame(); } } voidexplation()//游戏提示 { inti,j=1; system("cls"); color(10); gotoxy(44,1); printf("游戏说明"); color(2); for(i=3;i<=28;i++)//输出上下边框=== { for(j=6;j<=80;j++)//输出左右边框|| { gotoxy(j,i); if(i==3||i==28)printf("="); elseif(j==6||j==80)printf("||"); } } color(3); gotoxy(20,5); printf("1.W,A,S,D分别控制飞机的上下左右移动"); color(10); gotoxy(20,8); printf("2.按空格发射子弹,打中敌机即可得到一分"); color(14); gotoxy(20,11); printf("3.碰到敌机子弹死亡"); color(11); gotoxy(20,14); printf("4.ESC:退出游戏"); color(4); gotoxy(20,17); printf("5.玩的愉快!!!"); color(7); gotoxy(20,20); printf("/*****按任意键返回主页面*****/"); _getch();//按任意键返回主界面 system("cls"); welcometogame(); } voidscoreandtips()//游戏侧边提示 { gotoxy(50,8); color(14); printf("游戏得分:%d",score); gotoxy(50,10); printf("用WASD分别控制飞机的移动"); gotoxy(50,12); printf("按下空格键即为发射炮弹"); gotoxy(50,14); printf("@的样子就是敌人的飞机"); } voidHideCursor()//用于隐藏光标 { CONSOLE_CURSOR_INFOcursor_info={1,0};//第二个值为0表示隐藏光标 SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info); } voidstartup()//数据初始化 { high=20;//定义游戏界面的高度 width=40;//游戏界面的宽度 position_x=high-3;//定义飞机的初始位置 position_y=width/2; bullet_x=0; bullet_y=position_y; enemy_x=0; enemy_y=position_y; score=0; flag=0;//飞机完好 HideCursor(); } voidshow()//显示界面 { inti,j,k; for(i=0;i 0)//子弹上升效果 bullet_x--; if((bullet_x==enemy_x)&&(bullet_y==enemy_y))//子弹命中敌机 { score++; bullet_x=-1; enemy_x=1; enemy_y=2+rand()%width-2; } staticintspeed; if(speed<30)//减慢敌机速度,不影响飞机和子弹速度 speed++; if(speed==30) { if(enemy_x 1) position_x--; if((input=='s')&&position_x 1) position_y--; if((input=='d')&&position_y 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。