700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > C语言—飞机大战小游戏

C语言—飞机大战小游戏

时间:2024-02-16 21:15:08

相关推荐

C语言—飞机大战小游戏

哈工大经典C语言大作业—飞机大战小游戏,源码如下,已经通过编译获得评分19+ (满分20)当时还是太菜了呜呜呜。

可以给大家参考一下,好像本来是加了音乐的,但是你们可能没有对应的音乐MP3文件,所以如果出现报错就把它那一段注释就可以了,该代码绝对可以通过编译运行!我觉得,这些东西还是给一大段代码然后读者对着代码编译琢磨更好,记忆更深刻,不能编译的代码纯属没用,以下就是我写的小游戏,注释较为详细就不给大家段落解释了(提示,使用VS编译还有就是使用了链表混分仅仅是第四个模式有用哈哈哈哈)。

注:不会的同学可以看看mooc 河海大学基于游戏开发的c语言程序设计实践。看代码建议从主函数开始

总共有四种模式,其中只有第四种模式加入链表操作(其实并有什么用处)

#include<stdio.h>#include<Windows.h>#include<stdlib.h>#include<conio.h>#include<time.h>#include<MMSystem.h>#pragma comment(lib,"Winmm.lib")#define HIGH 20 //游戏界面高度#define WIDTH 30 // 游戏界面宽度//#define NUM 10 //敌机下落速度int flag=0;int color(int c); //更改文字颜色int position_x, position_y; //飞机位置int bullet_x, bullet_y; //子弹位置int enemy_x, enemy_y; //敌机位置int score; //得分int i,j,p;static int speed = 0;int NUM=10 ,vt;int k;typedef struct Node{int x;int y;struct Node* pnext;}PLANE;PLANE *plane=NULL;PLANE * pnew = NULL;//定义一个新节点,并指向空并防止野指针;void CreateList()//创建链表{plane = (PLANE*)malloc(sizeof(PLANE));plane->pnext = NULL;}void AddNode() //增加节点{//为新的节点开辟空间pnew = (Node*)malloc(sizeof(Node));pnew->x = rand()%HIGH;pnew->y = rand()%WIDTH;//把节点连接起来pnew->pnext =plane->pnext;//头插法plane->pnext = pnew;}int color(int c){SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c); //更改文字颜色return 0;}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 HideCursor() //隐藏光标显示{CONSOLE_CURSOR_INFO cursor_info = {1, 0 };SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);}void startup()//数据的初始化{//初始化飞机position_x = HIGH / 2; //高度position_y = WIDTH / 2; //宽度//初始化子弹bullet_x = -1;bullet_y = position_y;//初始化敌机enemy_x = 0;enemy_y = position_y;//初始化得分score = 0;}void show()//显示画面{// system("cls"); //清屏函数 gotoxy(0, 0); //使光标回到0,0for (i = 0; i < HIGH; i++) //行{for (j = 0; j < WIDTH; j++) //列{if (i == position_x && j == position_y)//输出飞机{printf("Ж");}else if (i == bullet_x && j == bullet_y)//输出子弹{printf("|");}else if (i == enemy_x && j == enemy_y)//输出敌机{printf("Ψ");}else if ((j==WIDTH-1)||(i==HIGH-1)||(j==0)||(i==0))//打印边界 printf("-");else{printf(" ");}}if((position_x==enemy_x)&&(position_y==enemy_y)){//飞机撞毁 游戏结束 system("cls");gotoxy(43,15);printf("你挂了,游戏结束!!!\n");exit(0);}printf("\n");}}void show_2()//显示画面 复活模式的展示{// system("cls"); //清屏函数 gotoxy(0, 0); //使光标回到0,0for (i = 0; i < HIGH; i++) //行{for (j = 0; j < WIDTH; j++) //列{if (i == position_x && j == position_y)//输出飞机{printf("Ж");}else if (i == bullet_x && j == bullet_y)//输出子弹{printf("|");}else if (i == enemy_x && j == enemy_y)//输出敌机{printf("Ψ");}else if ((j==WIDTH-1)||(i==HIGH-1)||(j==0)||(i==0))//打印边界 printf("-");else{printf(" ");}}printf("\n");}}void bulletMove() //子弹追踪模式{if(bullet_x>enemy_x)bullet_x--;else if(bullet_x<enemy_x)bullet_x++;if(bullet_y>enemy_y)bullet_y--;else if(bullet_y<enemy_y)bullet_y++;}void leavehand()//离手模式{if(position_y==enemy_y&& flag==0){flag=1;bullet_y=position_y;bullet_x=position_x-1;}else if(position_y<enemy_y)position_y++;elseposition_y--;} void updateWitoutIput_1()//与用户输入无关的更新{if (bullet_x > -1) //让子弹向上落{bullet_x--;}if (bullet_x == enemy_x && bullet_y == enemy_y) //命中敌机{score++; //得分+1enemy_x = -1; //生成新的飞机enemy_y = rand() % WIDTH;bullet_x = -1; //让子弹直接出现屏幕外,直到下一次发射子弹}//控制敌机下落速度if (speed < NUM) //每进行NUM次敌机下落一次{speed++;}else{enemy_x++;speed = 0;}if (enemy_x > HIGH) //敌机一直下落到底部{enemy_x = -1; enemy_y = rand() % WIDTH;}}void updateWitoutIput_2()//与用户输入无关的更新{if((bullet_x>-1)&&(bullet_y>-1))bulletMove();if (bullet_x == enemy_x && bullet_y == enemy_y) //命中敌机{score++; //得分+1enemy_x = -1; //生成新的飞机enemy_y = rand() % WIDTH;bullet_x = -1; //让子弹直接出现屏幕外,直到下一次发射子弹}//控制敌机下落速度if (speed < NUM) //每进行NUM次敌机下落一次{speed++;}else{enemy_x++;speed = 0;}if (enemy_x > HIGH) //敌机一直下落到底部{enemy_x = -1; enemy_y = rand() % WIDTH;}}void updateWitoutIput_3()//与用户输入无关的更新{if((bullet_x>-1)&&(bullet_y>-1))leavehand();if(bullet_x>-1)bullet_x--;if (bullet_x == enemy_x && bullet_y == enemy_y) //命中敌机{score++; //得分+1flag=0;//bullet_y=40;enemy_x = -1; //生成新的飞机enemy_y = rand() % WIDTH;bullet_x = -1; //让子弹直接出现屏幕外,直到下一次发射子弹}}void updateWithInput()//与用户输入有关的更新{//用户输入char input;//控制飞机飞行if (_kbhit()){input = _getch();if (input == 'w'&&position_x>1)position_x--;if (input == 's'&&position_x<HIGH-1)position_x++;if (input == 'a'&&position_y>1)position_y--;if (input == 'd'&&position_y<WIDTH-1)position_y++;if (input == ' '){bullet_x = position_x - 1;bullet_y = position_y;}}}void moden()//设置难度{if(vt==1)NUM=10;else if(vt==2)NUM=30;else if(vt==3)NUM=50;else {printf("不要太皮\n");exit(0);}}void scoreandtips()//游戏侧边提示 {gotoxy(50,8);color(14);printf("游戏得分:%d ",score);gotoxy(50,10);printf("用W A S D 分别控制飞机的移动");gotoxy(50,12);printf("按下空格键即为发射炮弹");gotoxy(50,14);printf("Ж 的样子就是敌人的飞机");gotoxy(50,16);printf("撞击会死亡哦");gotoxy(50,18);}void appearance(){system("mode con cols=120 lines=30"); color(15);gotoxy(43,10);printf(" 飞 机 大 战 \n");color(11);gotoxy(42,12);printf("欢迎来到飞机大战 请选择模式\n");gotoxy(45,14);printf(" 1.正常模式 \n");gotoxy(45,15);printf(" 2.破解模式 \n");gotoxy(45,16);printf(" 3.双手离开键盘模式\n");gotoxy(45,17);printf(" 4.随机复活\n");gotoxy(42,18);printf("提示:W A S D 移动,空格发射子弹\n");gotoxy(45,1);printf("ps:大佬们别挑刺");gotoxy(45,20);scanf("%d",&p);//PlaySound(TEXT("D://未来初音.wav"),NULL,SND_FILENAME|SND_ASYNC|SND_LOOP);}void ssb(){system("cls");gotoxy(45,10);printf("别皮!认真玩游戏\n");gotoxy(45,12);printf("还想玩请输入666");gotoxy(45,14);scanf("%d",&k);if(k==666){gotoxy(45,16);color(11);printf("哈");color(12);printf("哈");color(13);printf("哈");color(10);gotoxy(45,17);printf("皮一下很开心");color(14);}}void alive() //自动复活模式{if((position_x==enemy_x)&&(position_y==enemy_y)){AddNode(); //增加节点,即飞机;enemy_x=-1;enemy_y=rand() % WIDTH;position_x = pnew->x;position_y = pnew->y; //飞机撞毁 重新生成飞机; }}int main(){appearance();if(p==1){gotoxy(0,0);system("cls");color(6);gotoxy(43,10);printf("请输入难度\n");gotoxy(43,11);printf("1.简单 (感觉有点不太协调)\n");gotoxy(43,12);printf("2.很简单 (就这样吧)\n");gotoxy(43,13);printf("3.极其简单(太难搞了)\n");gotoxy(43,14);scanf("%d",&vt);moden();//选择模式system("cls");startup();//初始化HideCursor();srand((unsigned)time(NULL));while (1) //循环执行{gotoxy(0,0);scoreandtips();show();//显示画面 updateWitoutIput_1();//与用户输入无关的更新 更新数据 updateWithInput(); //与用户输入有关的更新 输入分析 }system("pause");return 0;}if(p==2){system("cls");startup();//初始化HideCursor();srand((unsigned)time(NULL));while (1){scoreandtips();show();//显示画面 updateWitoutIput_2();//与用户输入无关的更新 //更新数据updateWithInput(); //与用户输入有关的更新 //输入分析}system("pause");//防止闪屏return 0;}if(p==3){system("cls");startup();//初始化HideCursor();srand((unsigned)time(NULL));while (1){scoreandtips();show();//显示画面 leavehand();updateWitoutIput_3();//与用户输入无关的更新 //更新数据}system("pause");//防止闪屏return 0;}if(p==4){system("cls");startup();//初始化HideCursor();srand((unsigned)time(NULL));CreateList();while (1) //循环执行{gotoxy(0,0);scoreandtips();show_2();//显示画面 alive();updateWitoutIput_1();//与用户输入无关的更新 更新数据 updateWithInput(); //与用户输入有关的更新 输入分析 }system("pause");return 0;}else ssb();}

下面是运行界面

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。