打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
使用c语言实现飞机游戏

在这里,我主要使用scanf函数和printf函数来实现一个简单的飞机游戏,并且通过函数的形式实现。

整体思路:main函数

在这里,主要是使用一个简易的游戏框架,来减小开发游戏时的难度

int main()

{

    startup();//初始化

    while (1//游戏循环进行

    {

        show();//显示画面  

        updateWitoutIput();//与用户输入无关的更新

        updateWithInput(); //与用户输入有关的更新

    }

    system('pause');

    return 0;

}

全局变量的定义

为了方便之后代码的书写,在这里,我们使用宏定义代替部分全局变量

//全局变量的定义

#define HIGH 20  //游戏界面高度

#define WIDTH 30  // 游戏界面宽度

#define NUM 20  //敌机下落速度



int position_x, position_y;  //飞机位置

int bullet_x, bullet_y;  //子弹位置

int enemy_x, enemy_y;  //敌机位置

int score;  //得分

全局变量的初始化

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;

}

显示函数show()

输出飞机、敌机、子弹等

使用循环语句,在满足飞机、敌机、子弹位置条件时输出对应的图案(飞机: * 敌机: @ 子弹: | ),其余位置输出‘ ’或‘\n’

//输出每一行每一列

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', score);

清屏

在输出输出飞机等图标时,需要使用清屏函数使得光标回到 (0,0) 处,方便下一次输入

清屏函数

#include <windows.h>

system('cls');

使光标回到 (0,0)

gotoxy(00); 
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);

}

隐藏光标显示

为了防止光标闪烁的过于频繁,我们使用隐藏光标显示函数隐藏光标

oid HideCursor()//隐藏光标显示函数

{

    CONSOLE_CURSOR_INFO cursor_info = { 10 };

    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);

}

//该函数只需调用一次即可

与用户输入无关的部分

子弹上落

if (bullet_x > -1//让子弹向上落

{

    bullet_x--;

}

敌机下落,到达底部后,生成新的敌机

//创建一个静态变量,当静态变量满足一定条件时,敌机下落一次,这样可以控制敌机下降的速度同时不影响飞机移动的速度

static int speed = 0//控制敌机下落速度

if (speed < NUM)  //每进行NUM次敌机下落一次

{

    speed++;

}

else

{

    enemy_x++;  //敌机下落一次

    speed = 0;

}

if (enemy_x > HIGH)  //敌机一直下落到底部

{

    enemy_x = -1;   //生成新的飞机

    enemy_y = rand() % WIDTH;

}

命中敌机,得分+1,同时生成新的敌机

if (bullet_x == enemy_x && bullet_y == enemy_y)  //命中

{

    score++;  //得分+1



    enemy_x = -1;   //生成新的飞机

    enemy_y = rand() % WIDTH;



    bullet_x = -1;  //让子弹直接出现屏幕外,直到下一次发射子弹

}

与用户输入有关的部分

在这里,我们可以使用scanf函数或者getch函数实现在用户输入 ‘w’ ‘s’ ‘a’ ‘d’ 时对上下左右的控制

scanf函数

scanf('%c', &input);

if (input == 'w')

    position_x--;

if (input == 's')

    position_x++;

if (input == 'a')

    position_y--;

if (input == 'd')

    position_y++;

if (input == ' ')

{

    bullet_x = position_x - 1;

    bullet_y = position_y;

}

getch函数

if (_kbhit())  //kbhit()函数:检查当前是否有键盘输入,若有则返回一个非0值,否则返回0,头文件<conio.h>

{

    input = _getch();  //getch()是一个不回显函数,当用户按下某个字符时,函数自动读取,但是不会显示在屏幕上,无需按回车,

    if (input == 'w')

        position_x--;

    if (input == 's')

        position_x++;

    if (input == 'a')

        position_y--;

    if (input == 'd')

        position_y++;

    if (input == ' ')

    {

        bullet_x = position_x - 1;

        bullet_y = position_y;

    }

}

在这里,我们选择使用getch()函数,从而使得程序更加方便(减少用户回车的输入及屏幕上的显示)

注意事项

在实现代码时需要注意在使用各个函数时对头文件的调用尽量减少对全局变量的创建

完整代码

#include <stdio.h>

#include <windows.h>

#include <stdlib.h>

#include <conio.h>

#include <time.h>



//全局变量的定义

#define HIGH 20  //游戏界面高度

#define WIDTH 30  // 游戏界面宽度

#define NUM 10  //敌机下落速度



int position_x, position_y;  //飞机位置

int bullet_x, bullet_y;  //子弹位置

int enemy_x, enemy_y;  //敌机位置

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 HideCursor()  //隐藏光标显示

{

    CONSOLE_CURSOR_INFO cursor_info = { 10 };

    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(00); //使光标回到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', score);

}



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

{

    if (bullet_x > -1//让子弹向上落

    {

        bullet_x--;

    }



    if (bullet_x == enemy_x && bullet_y == enemy_y) //命中敌机

    {

        score++;  //得分+1



        enemy_x = -1;   //生成新的飞机

        enemy_y = rand() % WIDTH;



        bullet_x = -1;  //让子弹直接出现屏幕外,直到下一次发射子弹

    }



    static int speed = 0//控制敌机下落速度

    if (speed < NUM)  //每进行NUM次敌机下落一次

    {

        speed++;

    }

    else

    {

        enemy_x++;

        speed = 0;

    }

    if (enemy_x > HIGH)  //敌机一直下落到底部

    {

        enemy_x = -1

        enemy_y = rand() % WIDTH;

    }



}



void updateWithInput()//与用户输入有关的更新

{

    //用户输入

    char input;

    if (_kbhit())

    {

        input = _getch();

        if (input == 'w')

            position_x--;

        if (input == 's')

            position_x++;

        if (input == 'a')

            position_y--;

        if (input == 'd')

            position_y++;

        if (input == ' ')

        {

            bullet_x = position_x - 1;

            bullet_y = position_y;

        }

    }

}

int main()

{

    startup();//初始化

    HideCursor();

    srand((unsigned)time(NULL));

    while (1)

    {

        show();//显示画面  

        updateWitoutIput();//与用户输入无关的更新  //更新数据

        updateWithInput(); //与用户输入有关的更新  //输入分析

    }

    system('pause');

    return 0;

}

程序效果

总结

虽然完成了一个简单的飞机游戏,但是很多的功能都未能实现,如改变飞机的形状,增加游戏的难度,飞机生命的减少等等,任需要继续的努力。

--------------------- 

作者:木头i 

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
【cocos2d-x入门实战】微信飞机大战之九:碰撞检测
用 Python 实现微信版飞机大战
unity3初学往鼠标点击的方向发射子弹
飞机大战编程
Bite the bullet不是“咬子弹”,翻译错就尴尬了!
古老的子弹(7 photos)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服