温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C语言如何实现空战游戏

发布时间:2022-05-16 14:26:30 来源:亿速云 阅读:225 作者:iii 栏目:开发技术

本篇内容介绍了“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语言如何实现空战游戏”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI