C语言怎么用封装方法实现飞机大战游戏
发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,本文小编为大家详细介绍"C语言怎么用封装方法实现飞机大战游戏",内容详细,步骤清晰,细节处理妥当,希望这篇"C语言怎么用封装方法实现飞机大战游戏"文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一
千家信息网最后更新 2026年11月01日C语言怎么用封装方法实现飞机大战游戏
本文小编为大家详细介绍"C语言怎么用封装方法实现飞机大战游戏",内容详细,步骤清晰,细节处理妥当,希望这篇"C语言怎么用封装方法实现飞机大战游戏"文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。
一、项目描述和最终的成果展示
项目描述: 在上一次的基础上用函数进行了封装,对于一些功能也进行了一些优化。
最终效果图如下:

二、用函数进行封装
代码如下:
#include#include #include #include //全局变量int position_x,position_y;//飞机位置int high,width;//游戏画面尺寸void startup()//数据的初始化{ high = 20; width = 30; position_x = high/2;//飞机的上下位置 position_y = width/2;//飞机的左右·位置}void show()//显示画面{ system("cls"); int i,j; for(i=0;i 三、新型的发射子弹功能
代码如下:
#include#include #include #include //全局变量int position_x,position_y;//飞机位置int high,width;//游戏画面尺寸int bullet_x,bullet_y;//子弹位置//定义隐藏光标函数void HideCursor(){ CONSOLE_CURSOR_INFO cursor; cursor.bVisible = FALSE; cursor.dwSize = sizeof(cursor); HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleCursorInfo(handle, &cursor);}void startup()//数据的初始化{ high = 120; width = 100; position_x = high/2;//飞机的上下位置 position_y = width/2;//飞机的左右·位置 bullet_x = 0; bullet_y = position_y;}void show()//显示画面{ system("cls"); int i,j; for(i=0;i -1) bullet_x--;}void updateWithInput()//与用户输入有关的更新{ char input; if(kbhit()) { input=getch(); if( input == 'a' || input == 'A') position_y--;//左移 if( input == 'd' || input == 'D') position_y++;//右移 if( input == 'w' || input == 'W') position_x--;//上移 if( input == 's' || input == 'S') position_x++;//下移 if( input == ' ') { bullet_x=position_x-1; bullet_y=position_y; } }}int main(void){ startup();//数据的初始化 while(1) { show();//显示画面 HideCursor();//隐藏光标,防止光标乱闪。 updateWithoutInput();//与用户输入无关的更新 updateWithInput();//与用户输入有关的更新 } return 0;} 效果图如下:
发射子弹的功能和上次有了明显的改进,有了一个动态发射的一个效果。
四、实现移动的敌机功能和更正屏幕闪烁,清除光标功能
代码如下;
#include#include #include #include //全局变量int position_x,position_y;//飞机位置int high,width;//游戏画面尺寸int bullet_x,bullet_y;//子弹位置int enemy_x,enemy_y;//敌机的位置int score;//得分//定义隐藏光标函数void HideCursor(){ CONSOLE_CURSOR_INFO cursor; cursor.bVisible = FALSE; cursor.dwSize = sizeof(cursor); HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleCursorInfo(handle, &cursor);}void gotoxy(int x,int y)//将光标移动到(x,y)位置{ HANDLE handle =GetStdHandle(STD_OUTPUT_HANDLE); COORD pos; pos.X = x; pos.Y = y; SetConsoleCursorPosition(handle,pos);}void startup()//数据的初始化{ system("color 09"); high = 30; width =50; position_x = high/2;//飞机的上下位置 position_y = width/2;//飞机的左右位置 bullet_x = 0; bullet_y = position_y; enemy_x=0; enemy_y=position_y; score=0;}void show()//显示画面{ //system("cls"); gotoxy(0,0); int i,j; for(i=0;i -1) bullet_x--; if( (bullet_x == enemy_x) && (bullet_y ==enemy_y) )//子弹击中飞机 { score++;//分数无效 enemy_x=-1;//产生新的敌机 enemy_y=rand()%width; bullet_x=-2;//子弹无效 } // 用来控制敌机向下移动的速度,每隔几次循环才移动一次敌机 // 这样修改,虽然用户按键的交互速度还是很快,但是NPC的移动显示可以降速 if(speed<10) speed++; if(speed == 10 ) { enemy_x++; speed = 0; }}void updateWithInput()//与用户输入有关的更新{ char input; if(kbhit()) { input=getch(); if( input == 'a' || input == 'A') position_y--;//左移 if( input == 'd' || input == 'D') position_y++;//右移 if( input == 'w' || input == 'W') position_x--;//上移 if( input == 's' || input == 'S') position_x++;//下移 if( input == ' ') { bullet_x=position_x-1; bullet_y=position_y; } }}int main(void){ startup();//数据的初始化 while(1) { show();//显示画面 HideCursor();//隐藏光标,防止光标乱闪。 updateWithoutInput();//与用户输入无关的更新 updateWithInput();//与用户输入有关的更新 } return 0;} 效果图如下:
五、订正一些BUG和完成一些美化
我们的项目基本是已经完成了。但是还有很多的漏洞。
比如: 飞机控制越界问题,以及敌机越界问题。
而且界面不够好看我们要再美化一下。
以及增加游戏暂停功能。
游戏结束功能。代码如下:
#include#include #include #include //全局变量int position_x,position_y;//飞机位置int high,width;//游戏画面尺寸int bullet_x,bullet_y;//子弹位置int enemy_x,enemy_y;//敌机的位置int score;//得分//定义隐藏光标函数void HideCursor(){ CONSOLE_CURSOR_INFO cursor; cursor.bVisible = FALSE; cursor.dwSize = sizeof(cursor); HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleCursorInfo(handle, &cursor);}void gotoxy(int x,int y)//将光标移动到(x,y)位置{ HANDLE handle =GetStdHandle(STD_OUTPUT_HANDLE); COORD pos; pos.X = x; pos.Y = y; SetConsoleCursorPosition(handle,pos);}void startup()//数据的初始化{ system("color 09"); high = 30; width =50; position_x = high/2;//飞机的上下位置 position_y = width/2;//飞机的左右位置 bullet_x = 0; bullet_y = position_y; enemy_x=0; enemy_y=position_y; score=0;}void show()//显示画面{ //system("cls"); gotoxy(0,0); int i,j; for(i=0;i -1) bullet_x--; if( (bullet_x == enemy_x) && (bullet_y ==enemy_y) )//子弹击中飞机 { score++;//分数无效 enemy_x=-1;//产生新的敌机 enemy_y=rand()%width+1; bullet_x=-2;//子弹无效 } // 用来控制敌机向下移动的速度,每隔几次循环才移动一次敌机 // 这样修改,虽然用户按键的交互速度还是很快,但是NPC的移动显示可以降速 if(speed<6) speed++; if(speed == 6 ) { enemy_x++; if(enemy_x==high-1)//如果飞机越界再次生成 { enemy_x=-1;//产生新的敌机 enemy_y=rand()%width+1; } if( enemy_x==position_x-1)//撞机了 游戏结束 { system("cls"); printf("飞机坠毁了,游戏结束\n"); printf("分数为:%d\n",score); printf("请重启再开始新的一局\n"); while(1) { } } speed = 0; }}void updateWithInput()//与用户输入有关的更新{ char input; if(kbhit()) { input=getch(); if( input == 'a' || input == 'A') { position_y--;//左移 if(position_y==0)//判断是否越界 { position_y++; } } if( input == 'd' || input == 'D') { position_y++;//右移 if(position_y==width-2)//判断是否越界 { position_y--; } } if( input == 'w' || input == 'W') { position_x--;//上移 if(position_x==1)//判断是否越界 { position_x++; } } if( input == 's' || input == 'S') { position_x++;//下移 if(position_x==high-1)//判断是否越界 { position_x--; } } if( input == ' ')//发射子弹 { bullet_x=position_x-1; bullet_y=position_y; } if( input == '1')//按1键游戏暂停 { while(1) { input=getch(); if(input == '1')//再按1键游戏继续 break; } } }}int main(void){ startup();//数据的初始化 while(1) { show();//显示画面 HideCursor();//隐藏光标,防止光标乱闪。 updateWithoutInput();//与用户输入无关的更新 updateWithInput();//与用户输入有关的更新 } return 0;} 效果图如下:
读到这里,这篇"C语言怎么用封装方法实现飞机大战游戏"文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注行业资讯频道。
飞机
位置
用户
输入
更新
光标
画面
敌机
子弹
移动
数据
有关
功能
封装
函数
效果
上下
代码
全局
变量
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
互联网科技图片
广东猪兼强互联网科技有限公司
中关村互联网科技集团
中国互联网科技股
与互联网科技公司
互联网未来科技
互联网科技论文
奇士达互联网科技
友谊互联网络科技
丰台科技园 互联网公司
顺网科技2021年互联网排名
老年人脱离互联网科技的作文
开启互联网科技时代小说
移动互联网年度科技创新
洛阳昕诺飞互联网科技有限公司
健康食品互联网创新科技园项目
国大互联网教育科技有限公司工作
卓翼科技产业互联网合作
科技赋能视角下互联网保险发展
唐山双旺合互联网科技产业园
广州百单互联网科技有限公司
互联网科技垄断
善班互联网科技
万易互联网科技有限公司
顺网科技2021年互联网排名
阿里互联网科技公司
阿大互联网科技有限公司
互联网科技机房
安徽零酒互联网科技有限公司
东风日产互联网科技公司叫什么