文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

C语言如何实现空战游戏

2023-06-30 14:06

关注

本篇内容介绍了“C语言如何实现空战游戏”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

一、项目描述和成果展示

项目描述:   在以往的程序中进行了改进。
例如:   可以发射多个子弹
        可以有多个敌机

飞机大战 1.0版
飞机大战 2.0版

现在这个版本是3.0版 这个版本的各个功能基本完善

效果图如下:

C语言如何实现空战游戏

C语言如何实现空战游戏

二、发射多个子弹

代码如下:

#include<stdio.h>#include<string.h>#include<conio.h>#include<Windows.h>#define High 25 //游戏的尺寸#define Width 50//全局变量int position_x,position_y;//飞机的位置int canvas[High][Width]={0};//二维数组存储游戏画布中对应的元素//0为空格 1为飞机  2为子弹  3为敌机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()//数据的初始化{    position_x=High/2;    position_y=Width/2;    canvas[position_x][position_y]=1;}void show()//显示画面{    int i,j;    gotoxy(0,0);//将光标移动到原点位置,以下重画清屏    for(i=0;i<High;i++)    {        for(j=0;j<Width;j++)        {            if(canvas[i][j]==0)                printf(" ");//输出空格            else if(canvas[i][j]==1)                printf("*");//输出空格            else if(canvas[i][j]==2)                printf("|");//输出子弹        }        printf("\n");    }}void updateWithoutInput()//与用户输入无关的更新{    int i,j;    for(i=0;i<High;i++)    {        for(j=0;j<Width;j++)        {            if(canvas[i][j]==2)//子弹向上移动            {                canvas[i][j]=0;                if(i>0)                    canvas[i-1][j]=2;            }        }    }}void updateWithInput()//与用户输入有关的更新{    char input;    if(kbhit())//判断是否有输入    {        input=getch();//根据用户的不同输入来移动        if(input=='a'||input=='A')        {            canvas[position_x][position_y]=0;            position_y--;//位置左移            canvas[position_x][position_y]=1;        }        else if(input=='d'||input=='D')        {            canvas[position_x][position_y]=0;            position_y++;//位置右移            canvas[position_x][position_y]=1;        }        else if(input=='w'||input=='W')        {            canvas[position_x][position_y]=0;            position_x--;//位置上移            canvas[position_x][position_y]=1;        }        else if(input=='s'||input=='S')        {            canvas[position_x][position_y]=0;            position_x++;//位置下移            canvas[position_x][position_y]=1;        }        else if(input==' ')//发射子弹        {            canvas[position_x-1][position_y]=2;            //发射子弹的初始位置在飞机的正上方        }    }}int main(){    startup();//数据的初始化    while(1)//游戏循环执行    {        show();//显示画面        updateWithoutInput();//与用户输入无关的更新        updateWithInput();//与用户输入有关的更新    }    return 0;}

效果图如下:

C语言如何实现空战游戏

三、多个敌机

代码如下:

#include<stdio.h>#include<string.h>#include<conio.h>#include<Windows.h>#define High 25 //游戏的尺寸#define Width 50#define EnemyNum 5//敌机的个数//全局变量int position_x,position_y;//飞机的位置int enemy_x[EnemyNum],enemy_y[EnemyNum];//敌机的位置int canvas[High][Width]={0};//二维数组存储游戏画布中对应的元素//0为空格 1为飞机  2为子弹  3为敌机int score;//得分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()//数据的初始化{    int k;    position_x=High-1;    position_y=Width/2;    canvas[position_x][position_y]=1;    for(k=0;k<EnemyNum;k++)    {        enemy_x[k]=rand()%2;        enemy_y[k]=rand()%Width;        canvas[enemy_x[k]][enemy_y[k]]=3;    }    score=0;}void show()//显示画面{    int i,j;    gotoxy(0,0);//将光标移动到原点位置,以下重画清屏    for(i=0;i<High;i++)    {        for(j=0;j<Width;j++)        {            if(canvas[i][j]==0)                printf(" ");//输出空格            else if(canvas[i][j]==1)                printf("*");//输出空格            else if(canvas[i][j]==2)                printf("|");//输出子弹            else if(canvas[i][j]==3)                printf("+");//输出敌机        }        printf("\n");    }    printf("得分:%3d\n",score);    Sleep(20);}void updateWithoutInput()//与用户输入无关的更新{    int i,j,k;    for(i=0;i<High;i++)    {        for(j=0;j<Width;j++)        {            if(canvas[i][j]==2)//子弹向上移动            {                for(k=0;k<EnemyNum;k++)                {                    if( (i==enemy_x[k])&&(j==enemy_y[k]) )//子弹击中敌机                    {                        score++;//分数加一                        canvas[enemy_x[k]][enemy_y[k]]=0;                        enemy_x[k]=rand()%2;//产生新的飞机                        enemy_y[k]=rand()%Width;                        canvas[enemy_x[k]][enemy_y[k]]=3;                        canvas[i][j]=0;//子弹消失                    }                }                //子弹向上移动                canvas[i][j]=0;                if(i>0)                canvas[i-1][j]=2;            }        }    }    for(k=0;k<EnemyNum;k++)    {        if( (position_x==enemy_x[k])&&(position_y==enemy_y[k]) )//敌机撞到我机        {            printf("失败\n");            Sleep(3000);            system("pause");            exit(0);        }        if(enemy_x[k]>High)//敌机跑出屏幕        {            canvas[enemy_x[k]][enemy_y[k]]=0;            enemy_x[k]=rand()%2;//产生新的飞机            enemy_y[k]=rand()%Width;            canvas[enemy_x[k]][enemy_y[k]]=3;            score--;//减分        }        static int speed=0;        if(speed<36)            speed++;        if(speed==36)        {            //敌机下落            for(k=0;k<EnemyNum;k++)            {                canvas[enemy_x[k]][enemy_y[k]]=0;                enemy_x[k]++;                speed=0;                canvas[enemy_x[k]][enemy_y[k]]=3;            }        }    }}void updateWithInput()//与用户输入有关的更新{    char input;    if(kbhit())//判断是否有输入    {        input=getch();//根据用户的不同输入来移动        if(input=='a'||input=='A')        {            canvas[position_x][position_y]=0;            position_y--;//位置左移            canvas[position_x][position_y]=1;        }        else if(input=='d'||input=='D')        {            canvas[position_x][position_y]=0;            position_y++;//位置右移            canvas[position_x][position_y]=1;        }        else if(input=='w'||input=='W')        {            canvas[position_x][position_y]=0;            position_x--;//位置上移            canvas[position_x][position_y]=1;        }        else if(input=='s'||input=='S')        {            canvas[position_x][position_y]=0;            position_x++;//位置下移            canvas[position_x][position_y]=1;        }        else if(input==' ')//发射子弹        {            canvas[position_x-1][position_y]=2;            //发射子弹的初始位置在飞机的正上方        }    }}int main(){    startup();//数据的初始化    while(1)//游戏循环执行    {        show();//显示画面        updateWithoutInput();//与用户输入无关的更新        updateWithInput();//与用户输入有关的更新    }    return 0;}

效果图如下:

C语言如何实现空战游戏

四、增加子弹升级功能

当分数达到一定的值自动升级炮弹,当分数下降一定数值后炮弹下降一个等级。

代码如下:

#include<stdio.h>#include<string.h>#include<conio.h>#include<Windows.h>#define High 25 //游戏的尺寸#define Width 40#define EnemyNum 5//敌机的个数//全局变量int position_x,position_y;//飞机的位置int enemy_x[EnemyNum],enemy_y[EnemyNum];//敌机的位置int canvas[High][Width]={0};//二维数组存储游戏画布中对应的元素//0为空格 1为飞机  2为子弹  3为敌机int score;//得分int BulletWidth;//子弹的宽度int EnemyMoveSpeed;//敌机的移动速度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()//数据的初始化{    int k;    position_x=High-1;    position_y=Width/2;    canvas[position_x][position_y]=1;    for(k=0;k<EnemyNum;k++)    {        enemy_x[k]=rand()%2;        enemy_y[k]=rand()%Width;        canvas[enemy_x[k]][enemy_y[k]]=3;    }    score=0;    BulletWidth=0;    EnemyMoveSpeed=20;}void show()//显示画面{    int i,j;    gotoxy(0,0);//将光标移动到原点位置,以下重画清屏    for(i=0;i<High;i++)    {        for(j=0;j<Width;j++)        {            if(canvas[i][j]==0)                printf(" ");//输出空格            else if(canvas[i][j]==1)                printf("*");//输出空格            else if(canvas[i][j]==2)                printf("|");//输出子弹            else if(canvas[i][j]==3)                printf("+");//输出敌机        }        printf("\n");    }    printf("════════════════════════════════════════\n");    printf("得分:%3d\n",score);    system("date /t");    system("time /t");    Sleep(20);}//定义隐藏光标函数void HideCursor(){    CONSOLE_CURSOR_INFO cursor;        cursor.bVisible = FALSE;        cursor.dwSize = sizeof(cursor);        HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);        SetConsoleCursorInfo(handle, &cursor);}void updateWithoutInput()//与用户输入无关的更新{    int i,j,k;    for(i=0;i<High;i++)    {        for(j=0;j<Width;j++)        {            if(canvas[i][j]==2)//子弹向上移动            {                for(k=0;k<EnemyNum;k++)                {                    if( (i-1==enemy_x[k])&&(j==enemy_y[k]) )//子弹击中敌机                    {                        score++;//分数加一                        if(score%5==0&&EnemyMoveSpeed>3)//达到一定积分后敌机变快                            EnemyMoveSpeed--;                        if(score/5)//达到一定积分后子弹变厉害                        {                            BulletWidth=0;                            BulletWidth=BulletWidth+score/5;                            if(BulletWidth>5)//子弹加到一定威力后,会固定不变                                BulletWidth=5;                        }                        canvas[enemy_x[k]][enemy_y[k]]=0;                        enemy_x[k]=rand()%2;//产生新的飞机                        enemy_y[k]=rand()%Width;                        canvas[enemy_x[k]][enemy_y[k]]=3;                        canvas[i][j]=0;//子弹消失                    }                }                //子弹向上移动                canvas[i][j]=0;                if(i>0)                canvas[i-1][j]=2;            }        }    }    for(k=0;k<EnemyNum;k++)    {        if( (position_x==enemy_x[k])&&(position_y==enemy_y[k]) )//敌机撞到我机        {            printf("失败\n");            Sleep(3000);            system("pause");            exit(0);        }        if(enemy_x[k]>High)//敌机跑出屏幕        {            canvas[enemy_x[k]][enemy_y[k]]=0;            enemy_x[k]=rand()%2;//产生新的飞机            enemy_y[k]=rand()%Width;            canvas[enemy_x[k]][enemy_y[k]]=3;            score--;//减分            if(score%5==0&&score>0)//减的分数过多,子弹威力下降            {                BulletWidth--;                EnemyMoveSpeed++;            }        }        static int speed=0;        if(speed<EnemyMoveSpeed)            speed++;        if(speed==EnemyMoveSpeed)        {            //敌机下落            for(k=0;k<EnemyNum;k++)            {                canvas[enemy_x[k]][enemy_y[k]]=0;                enemy_x[k]++;                speed=0;                canvas[enemy_x[k]][enemy_y[k]]=3;            }        }    }}void updateWithInput()//与用户输入有关的更新{    char input;    if(kbhit())//判断是否有输入    {        input=getch();//根据用户的不同输入来移动        if(input=='a'||input=='A')        {            canvas[position_x][position_y]=0;            position_y--;//位置左移            if(position_y==0)                position_y++;            canvas[position_x][position_y]=1;        }        else if(input=='d'||input=='D')        {            canvas[position_x][position_y]=0;            position_y++;//位置右移            if(position_y==Width-1)                position_y--;            canvas[position_x][position_y]=1;        }        else if(input=='w'||input=='W')        {            canvas[position_x][position_y]=0;            position_x--;//位置上移            if(position_x==0)                position_x++;            canvas[position_x][position_y]=1;        }        else if(input=='s'||input=='S')        {            canvas[position_x][position_y]=0;            position_x++;//位置下移            if(position_x==High)                position_x--;            canvas[position_x][position_y]=1;        }        else if(input==' ')//发射子弹        {            int left =position_y-BulletWidth;            int right=position_y+BulletWidth;            if(left<0)                left=0;            if(right>Width-1)                right=Width-1;            int k;            for(k=left;k<=right;k++)//发射子弹            canvas[position_x-1][k]=2;            //发射子弹的初始位置在飞机的正上方        }    }}int main(){    startup();//数据的初始化    HideCursor();    system("title 游戏中");    system("color 09");    while(1)//游戏循环执行    {        show();//显示画面        updateWithoutInput();//与用户输入无关的更新        updateWithInput();//与用户输入有关的更新    }    return 0;}

效果图如下:

C语言如何实现空战游戏

五、增加一些小功能

增加一个主菜单
里面增加了一些小的功能

代码如下:

#include<stdio.h>#include<string.h>#include<conio.h>#include<Windows.h>#define High 25 //游戏的尺寸#define Width 40#define EnemyNum 5//敌机的个数//全局变量int position_x,position_y;//飞机的位置int enemy_x[EnemyNum],enemy_y[EnemyNum];//敌机的位置int canvas[High][Width]={0};//二维数组存储游戏画布中对应的元素//0为空格 1为飞机  2为子弹  3为敌机int score;//得分int BulletWidth;//子弹的宽度int EnemyMoveSpeed;//敌机的移动速度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()//数据的初始化{    int k;    position_x=High-1;    position_y=Width/2;    canvas[position_x][position_y]=1;    for(k=0;k<EnemyNum;k++)    {        enemy_x[k]=rand()%2;        enemy_y[k]=rand()%Width;        canvas[enemy_x[k]][enemy_y[k]]=3;    }    score=0;    BulletWidth=0;    EnemyMoveSpeed=20;}void show()//显示画面{    int i,j;    gotoxy(0,0);//将光标移动到原点位置,以下重画清屏    for(i=0;i<High;i++)    {        for(j=0;j<Width;j++)        {            if(canvas[i][j]==0)                printf(" ");//输出空格            else if(canvas[i][j]==1)                printf("*");//输出空格            else if(canvas[i][j]==2)                printf("|");//输出子弹            else if(canvas[i][j]==3)                printf("+");//输出敌机        }        printf("\n");    }    printf("════════════════════════════════════════\n");    printf("得分:%3d\n",score);    system("date /t");    system("time /t");    printf("提示:按 z 键回到主菜单");    Sleep(20);}//定义隐藏光标函数void HideCursor(){    CONSOLE_CURSOR_INFO cursor;        cursor.bVisible = FALSE;        cursor.dwSize = sizeof(cursor);        HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);        SetConsoleCursorInfo(handle, &cursor);}int updateWithoutInput()//与用户输入无关的更新{    int i,j,k;    for(i=0;i<High;i++)    {        for(j=0;j<Width;j++)        {            if(canvas[i][j]==2)//子弹向上移动            {                for(k=0;k<EnemyNum;k++)                {                    if( (i-1==enemy_x[k])&&(j==enemy_y[k]) )//子弹击中敌机                    {                        score++;//分数加一                        if(score%5==0&&EnemyMoveSpeed>3)//达到一定积分后敌机变快                            EnemyMoveSpeed--;                        if(score/5)//达到一定积分后子弹变厉害                        {                            BulletWidth=0;                            BulletWidth=BulletWidth+score/5;                            if(BulletWidth>5)//子弹加到一定威力后,会固定不变                                BulletWidth=5;                        }                        canvas[enemy_x[k]][enemy_y[k]]=0;                        enemy_x[k]=rand()%2;//产生新的飞机                        enemy_y[k]=rand()%Width;                        canvas[enemy_x[k]][enemy_y[k]]=3;                        canvas[i][j]=0;//子弹消失                    }                }                //子弹向上移动                canvas[i][j]=0;                if(i>0)                canvas[i-1][j]=2;            }        }    }    for(k=0;k<EnemyNum;k++)    {        if( (position_x==enemy_x[k])&&(position_y==enemy_y[k]) )//敌机撞到我机        {            system("cls");            printf("失败\n");            printf("得分:%d\n",score);            printf("正在返回主菜单\n");            Sleep(3000);            return 1;        }        if(enemy_x[k]>High)//敌机跑出屏幕        {            canvas[enemy_x[k]][enemy_y[k]]=0;            enemy_x[k]=rand()%2;//产生新的飞机            enemy_y[k]=rand()%Width;            canvas[enemy_x[k]][enemy_y[k]]=3;            score--;//减分            if(score%5==0&&score>0)//减的分数过多,子弹威力下降            {                BulletWidth--;                EnemyMoveSpeed++;            }        }        static int speed=0;        if(speed<EnemyMoveSpeed)            speed++;        if(speed==EnemyMoveSpeed)        {            //敌机下落            for(k=0;k<EnemyNum;k++)            {                canvas[enemy_x[k]][enemy_y[k]]=0;                enemy_x[k]++;                speed=0;                canvas[enemy_x[k]][enemy_y[k]]=3;            }        }    }    return 0;}void updateWithInput(char input)//与用户输入有关的更新{        if(input=='a'||input=='A')        {            canvas[position_x][position_y]=0;            position_y--;//位置左移            if(position_y==0)                position_y++;            canvas[position_x][position_y]=1;        }        else if(input=='d'||input=='D')        {            canvas[position_x][position_y]=0;            position_y++;//位置右移            if(position_y==Width-1)                position_y--;            canvas[position_x][position_y]=1;        }        else if(input=='w'||input=='W')        {            canvas[position_x][position_y]=0;            position_x--;//位置上移            if(position_x==0)                position_x++;            canvas[position_x][position_y]=1;        }        else if(input=='s'||input=='S')        {            canvas[position_x][position_y]=0;            position_x++;//位置下移            if(position_x==High)                position_x--;            canvas[position_x][position_y]=1;        }        else if(input==' ')//发射子弹        {            int left =position_y-BulletWidth;            int right=position_y+BulletWidth;            if(left<0)                left=0;            if(right>Width-1)                right=Width-1;            int k;            for(k=left;k<=right;k++)//发射子弹            canvas[position_x-1][k]=2;            //发射子弹的初始位置在飞机的正上方        }}void gamemenu()//游戏菜单{    int temp=0;    int i,j;    char input;    for(i=0;i<High;i++)//数组初始化    {        for(j=0;j<Width;j++)        {            canvas[i][j]=0;        }        printf("\n");    }    startup();//数据的初始化    system("cls");    while(1)    {        show();//显示画面        temp=updateWithoutInput();//与用户输入无关的更新        if(kbhit())//判断是否有输入        {            input=getch();            updateWithInput(input);//与用户输入有关的更新            if(input=='z'||input=='Z')                temp=1;        }        if(temp==1)        {            system("cls");            break;        }    }}void help()//帮助菜单{    char input;    system("cls");    printf("\n\n\n\n\n\n\n\n");    printf("---------------------------------------\n");    printf("                 帮助菜单                \n\n");    printf("      1. 按空格发射炮弹                 \n");    printf("      2. a 键左移                      \n");    printf("      3. d 键右移                      \n");    printf("      4. w 键上移                      \n");    printf("      5. s 键下移                      \n");    printf("---------------------------------------\n");    printf("\n\n提示:按 z 键回到主菜单\n");    printf("\n\n               祝您玩的愉快!\n");    while(1)    {        input=getch();        if(input=='z')        {            break;        }    }}void quit()//退出菜单{    exit(0);}void menu()//主菜单{    char change;    system("cls");    printf("\n--------------------------------------------\n");    printf("                   8\n");    printf("                   88\n");     printf("                    888\n");    printf("                     8888\n");    printf("                  8888888888888\n");    printf("                     8888\n");    printf("                    888\n");    printf("                   88\n");    printf("                   8\n");    printf("\n\n\n               Welcome to fly war!\n");    printf("\n\n\n\n");    printf("                 主菜单                \n\n");     printf("      1. 进入游戏                 \n");    printf("      2. 帮助菜单                      \n");    printf("      3. 退出游戏                      \n");    printf("---------------------------------------\n");    printf("\n\n               祝您玩的愉快!\n");    change=getch();    switch(change)    {    case '1':        gamemenu();//游戏菜单        break;    case '2':        help();//帮助菜单        break;    case '3':        quit();//退出菜单        break;          }}int main(){    HideCursor();    system("title 游戏中");    system("color 09");    system("mode 50,30");    while(1)//游戏循环执行    {        menu();    }    return 0;}

效果图如下:

C语言如何实现空战游戏

C语言如何实现空战游戏

“C语言如何实现空战游戏”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注编程网网站,小编将为大家输出更多高质量的实用文章!

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     807人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     351人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     314人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     433人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     221人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯