C语言用封装方法实现飞机大战游戏

 更新时间:2022年05月16日 11:27:41   作者:辉小歌  
这篇文章主要为大家详细介绍了C语言用封装方法实现飞机大战游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了C语言用封装方法实现飞机大战游戏的具体代码,供大家参考,具体内容如下

这是上一次的飞机大战游戏的项目。项目: 最简单的飞机大战游戏

上次没有用函数进行的封装。这次在上次的基础上进行封装和一些功能的优化。

一、项目描述和最终的成果展示

项目描述:   在上一次的基础上用函数进行了封装,对于一些功能也进行了一些优化。

最终效果图如下:

二、用函数进行封装

代码如下:

#include<stdio.h>
#include<stdlib.h>
#include<Windows.h>
#include<conio.h>

//全局变量
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<high;i++)
    {
        for(j=0;j<width;j++)
        {
            if( (i == position_x) && (j== position_y))//输出飞机
                printf("☆");
            else
                printf(" ");
        }
        printf("\n");
    }
}

void updateWithoutInput()//与用户输入无关的更新
{

}

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++;//下移
    }
}


int main(void)
{
    startup();  //数据的初始化
    while(1)
    {
        show();//显示画面
        updateWithoutInput();//与用户输入无关的更新
        updateWithInput();//与用户输入有关的更新
    }
    return 0;
}

三、新型的发射子弹功能

代码如下:

#include<stdio.h>
#include<stdlib.h>
#include<Windows.h>
#include<conio.h>

//全局变量
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<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
                printf(" ");
        }
        printf("\n");
    }
}

void updateWithoutInput()//与用户输入无关的更新
{
    if(bullet_x>-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<stdio.h>
#include<stdlib.h>
#include<Windows.h>
#include<conio.h>

//全局变量
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<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
                printf(" ");//输出空格
        }
        printf("\n");
    }
    printf("得分:%d\n",score);
}

void updateWithoutInput()//与用户输入无关的更新
{
    static int speed=0;
    if(bullet_x>-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<stdio.h>
#include<stdlib.h>
#include<Windows.h>
#include<conio.h>

//全局变量
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<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==position_x)
                //飞机那一行,因为有飞机多占一格,所以要删除前面的一个空格
                printf("\b+");
            else if(j==width-1)
                printf("+");
            else if(i==high-1)
                printf("-");
            else
                printf(" ");//输出空格
        }
        printf("\n");
    }
    printf("得分:%d\n",score);
    printf("按1键游戏暂停\n");
}

void updateWithoutInput()//与用户输入无关的更新
{
    static int speed=0;
    if(bullet_x>-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;
}

效果图如下:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 打印菱形以及斐波纳契数列的几种解法介绍

    打印菱形以及斐波纳契数列的几种解法介绍

    本篇文章是对打印菱形及斐波纳契数列的几种解法进行了详细的分析介绍,需要的朋友参考下
    2013-06-06
  • C++实现四则运算器(无括号)

    C++实现四则运算器(无括号)

    这篇文章主要为大家详细介绍了C++实现四则运算器,无括号的计算器,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-11-11
  • C++中getline()、gets()等函数的用法详解

    C++中getline()、gets()等函数的用法详解

    这篇文章主要介绍了C++中getline()、gets()等函数的用法,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-02-02
  • swift Character类型详解及实例

    swift Character类型详解及实例

    这篇文章主要介绍了 swift Character类型详解及实例的相关资料,需要的朋友可以参考下
    2017-06-06
  • c++冒泡排序详解

    c++冒泡排序详解

    冒泡排序(Bubble Sort),是一种计算机科学领域的较简单的排序算法。它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来。走访数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完成。
    2017-05-05
  • C++高性能服务器框架之协程调度模块

    C++高性能服务器框架之协程调度模块

    这篇文章主要介绍了C++高性能服务器框架中的协程调度模块,文中通过代码示例介绍的非常详细,对我们的学习或工作有一定的帮助,需要的朋友可以参考下
    2023-06-06
  • Linux搭建C++开发调试环境的方法步骤

    Linux搭建C++开发调试环境的方法步骤

    这篇文章主要介绍了Linux搭建C++开发调试环境的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-10-10
  • C++实现打地鼠游戏设计

    C++实现打地鼠游戏设计

    这篇文章主要为大家详细介绍了C++实现打地鼠游戏设计,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-12-12
  • CMake 生成静态库与动态库的方法步骤

    CMake 生成静态库与动态库的方法步骤

    本文主要介绍了CMake 生成静态库与动态库的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-06-06
  • C++ Primer 标准库vector示例详解

    C++ Primer 标准库vector示例详解

    该文章主要介绍了C++标准库中的vector类型,包括其定义、初始化、成员函数以及常见操作,文章详细解释了如何使用vector来存储和操作对象集合,并提供了代码示例来说明vector的使用方法,感兴趣的朋友一起看看吧
    2025-03-03

最新评论