飞机大战(C语言版)

2023-11-19

 大一下要交课程设计,于是就用C语言写了一个飞机大战小游戏 ,没有用到第三方库,飞机和子弹的移动使用的光标移动函数,所以没有卡顿

其中w s a d分别表示上下左右(包括大写),空格发射子弹,游戏结束后可选择是否储存游戏数据,该程序复制后可直接使用,也可根据喜好进行修改 

 运行截图如下:

 

 代码如下:

#include <stdio.h>

#include <string.h>

#include <windows.h> //包含对光标进行的操作

#include <conio.h>   //包含输入函数getch()

#include <stdlib.h>  //包含随机函数

#include <time.h>    //获取系统当前时间

#define HIGHT 30     //操作界面高度

#define WIGHT 40     //操作界面宽度

typedef struct

{

    int x;

    int y;

} BULLET;

typedef struct

{

    int x;

    int y;

    int speed;

    int life;

    BULLET bullet[20]; //子弹数量

    int num;           //子弹计数

} PLANE;

typedef struct

{

    int x;

    int y;

    int life;

    int move_step;

} ENEMY;

typedef struct

{

    int num;              //敌机个数

    int produce_interval; //敌机产生间隔

    int produce_step;

    int move_interval; //控制敌机移动

} ENEMY_DATA;



//定义全局变量

int choice; //菜单页选项

int record; //决定是否记录分数

int a[30][120];

int score;  //分数

int escape; //未击落的敌机数

PLANE plane;

ENEMY_DATA en_da;

ENEMY enemy[10];



void Hide() //隐藏光标

{

    CONSOLE_CURSOR_INFO cursor_info = {1, 0};

    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);

}

void goto_xy(int x, int y) //移动光标

{

    HANDLE hOut;

    hOut = GetStdHandle(STD_OUTPUT_HANDLE);

    COORD pos = {x, y};

    SetConsoleCursorPosition(hOut, pos);

}

void create_enemy() //创建敌机

{

    srand((unsigned int)time(NULL));

    enemy[en_da.num].y = 1;

    do

    {

        enemy[en_da.num].x = rand() % WIGHT;

    } while (enemy[en_da.num].x == 0 || enemy[en_da.num].x == WIGHT); //防止生成的敌机位于边界

    enemy[en_da.num].life = 1;

    if (en_da.num == 9)

        en_da.num = 0;

    else

        en_da.num++;

}

void initialize_data() //初始化数据

{

    plane.life = 3;

    plane.speed = 1;

    plane.x = WIGHT / 2 - 1;

    plane.y = HIGHT - 6;

    for (int i = 0; i < 20; i++)

    {

        plane.bullet[i].x = plane.x;

        plane.bullet[i].y = 0;

    }

    en_da.produce_step = 0;

    en_da.produce_interval = 20;

    en_da.num = 0;

    for (int i = 0; i < 10; i++) //初始化敌机

    {

        enemy[i].life = 0;

        enemy[i].move_step = 0;

        enemy[i].y = 0;

    }

    en_da.move_interval = 5;

    plane.num = 0;

    score = 0;

    escape = 0;

    create_enemy();

}

void update() //更新数据

{

    if (en_da.produce_step == en_da.produce_interval) //产生敌机

    {

        create_enemy();

        en_da.produce_step = 0;

    }

    else

        en_da.produce_step++;

    for (int i = 1; i < 30; i++)

    {

        for (int j = 0; j < 120; j++)

            a[i][j] = 0;

    }

    //边界

    for (int i = 1; i < HIGHT; i++)

    {

        a[i][0] = 8;

        a[i][WIGHT] = 8;

    }

    //飞机位置刷新

    a[plane.y][plane.x] = 1;

    a[plane.y + 1][plane.x - 2] = 1;

    a[plane.y + 1][plane.x - 1] = 1;

    a[plane.y + 1][plane.x] = 1;

    a[plane.y + 1][plane.x + 1] = 1;

    a[plane.y + 1][plane.x + 2] = 1;

    a[plane.y + 2][plane.x - 1] = 1;

    a[plane.y + 2][plane.x + 1] = 1;



    //子弹位置刷新

    for (int i = 0; i < 20; i++)

    {

        a[plane.bullet[i].y][plane.bullet[i].x] = 2;

        if (plane.bullet[i].y > 0) //子弹移动

            plane.bullet[i].y--;

    }

    //敌机位置刷新

    for (int i = 0; i <= 10; i++)

    {

        if (enemy[i].y < HIGHT && enemy[i].life == 1)

        {

            if (enemy[i].move_step == en_da.move_interval)

            {

                enemy[i].y++;

                enemy[i].move_step = 0;

            }

            else

                enemy[i].move_step++;

        }

    }

    //碰到飞机或是敌机越界后飞机减血

    for (int i = 0; i <= 10; i++)

    {

        if (enemy[i].y == HIGHT) //越界

        {

            escape++;

            enemy[i].y = 0;

        }

        //碰到飞机

        if ((enemy[i].y == plane.y && enemy[i].x == plane.x) ||

            (enemy[i].y == plane.y + 1 && enemy[i].x == plane.x - 2) ||

            (enemy[i].y == plane.y + 1 && enemy[i].x == plane.x - 1) ||

            (enemy[i].y == plane.y + 1 && enemy[i].x == plane.x + 1) ||

            (enemy[i].y == plane.y + 1 && enemy[i].x == plane.x + 2))

        {

            plane.life--;

            enemy[i].life = 0;

            enemy[i].y = 0;

        }

    }

    //击毁敌机后加分

    for (int i = 0; i < 20; i++)

    {

        for (int j = 0; j <= 10; j++)

        {

            if ((enemy[j].y == plane.bullet[i].y && enemy[j].x == plane.bullet[i].x) && enemy[j].life == 1)

            {

                score++;

                enemy[j].life = 0;

                enemy[j].y = 0;

                plane.bullet[i].y = 0; //子弹消失

            }

        }

    }

    //敌机位置刷新

    for (int i = 0; i < 10; i++)

        a[enemy[i].y][enemy[i].x] = -1;

}

void Print() //打印界面

{

    for (int i = 0; i < 16; i++)

        printf("-");

    printf("飞机大战");

    for (int i = 0; i < 16; i++)

        printf("-");

    printf("\n");

    for (int i = 1; i < HIGHT - 1; i++)

    {

        for (int j = 0; j < 120; j++)

        {

            if (a[i][j] == -1)

                printf("+");

            else if (a[i][j] == 1)

                printf("*");

            else if (a[i][j] == 2)

                printf("|");

            else if (a[i][j] == 0)

                printf(" ");

            else if (a[i][j] == 8)

                printf("#");

        }

    }

    printf("score:%d    life:%d    escape:%d", score, plane.life, escape);

}

void move() //控制飞机移动

{

    char key;

    if (kbhit())

    {

        key = getch();

        if (key == 'w' || key == 'W')

        {

            if (plane.y > 1) //控制飞机不超过上界

                plane.y -= plane.speed;

        }

        if (key == 'a' || key == 'A')

        {

            if (plane.x > 1) //控制飞机不超过左界

                plane.x -= plane.speed;

        }

        if (key == 's' || key == 'S')

        {

            if (plane.y < HIGHT - 4) //控制飞机不超过下界

                plane.y += plane.speed;

        }

        if (key == 'd' || key == 'D')

        {

            if (plane.x < WIGHT - 1) //控制飞机不超过右界

                plane.x += plane.speed;

        }

        if (key == ' ')

        {

            plane.bullet[plane.num].x = plane.x;

            plane.bullet[plane.num].y = plane.y - 1;

            plane.num++;

            if (plane.num == 20)

                plane.num = 0;

        }

    }

}

void view_history()

{

    FILE *history;

    history = fopen("biubiubiu_score.txt", "r");

    if (history == NULL)

        printf("文件不存在,请先创建!\n");

    else

    {

        char ch;

        ch = getc(history);

        while (ch != EOF)

        {

            putchar(ch);

            ch = getc(history);

        }

        printf("\n");

    }

    fclose(history);

}

void record_history()

{

    FILE *history;

    history = fopen("biubiubiu_score.txt", "a");

    if (history == NULL)

        printf("打开文件失败!\n");

    else

    {

        char date[30];

        //获取系统当前时间

        time_t t;

        struct tm *now;

        t = time(NULL);

        now = localtime(&t);

        strcpy(date, asctime(now));

        int len = strlen(date);

        date[len - 1] = '\0'; //去除时间结尾的换行符

        fputs(date, history);

        fputs("     ", history);

        fprintf(history, "score:%d", score);

        fputs("     ", history);

        fprintf(history, "escape:%d\n", escape);

    }

    fclose(history);

}

void menu()

{

    printf("   主菜单\n");

    printf("0、退出游戏\n");

    printf("1、开始游戏\n");

    printf("2、查看历史分数\n");

    scanf("%d", &choice);

    switch (choice)

    {

    case 0:

        exit(0); //退出程序

        break;

    case 1:

        system("cls");

        return;

        break;

    case 2:

        system("cls");

        view_history();

        system("pause");

        system("cls");

        menu(); //递归调用

        break;

    default:

        printf("输入错误,请重新输入!\n");

        system("pause");

        system("cls");

        menu(); //递归调用

        break;

    }

}

void end()

{

    system("cls");

    printf("GAME OVER!\n");

    printf("你的得分是:%d\n", score);

    printf("是否记录分数?\n");

    printf("1、yes\n");

    printf("2、no\n");

    scanf("%d", &record);

    switch (record)

    {

    case 1:

        record_history();

        printf("记录成功!");

        system("pause");

        break;

    case 2:

        printf("未记录!");

        system("pause");

        return;

        break;

    default:

        printf("输入错误,请重新输入!\n");

        system("pause");

        system("cls");

        end(); //递归调用

        break;

    }

}

int main()

{

    system("color 9f");

    while (1)

    {

        menu();

        Hide();

        initialize_data();

        while (1)

        {

            update();

            if (plane.life <= 0)

                break; //生命值为零退出游戏

            Print();

            goto_xy(0, 0);

            move();

        }

        end();

        system("cls");

    }

    return 0;

} 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

飞机大战(C语言版) 的相关文章

随机推荐

  • Linux上开发常见问题整理

    Linux上开发常见问题整理 1 java工程在linux上运行测试 首先要有一个main方法作为主类 程序的入口 右键 gt Run As gt javaapplication生成配置文件入口 右键该工程 gt Export gt Run
  • LaTeX 多行公式、公式对齐以及输入矩阵的方法

    一 LaTex显示 大括号连接的 多行公式 公式组合 使用cases环境实现公式的组合 分隔公式和条件 具体LaTex代码如下 D x begin cases lim limits x to 0 frac a x b c x lt 3 pi
  • tshark 解析pcap中带TLS协议的数据包

    tshark的简单用法参考 tshark解析本地pcap数据包提取五元组 src ip src port proto dst ip dst port 与时间戳 包长 详细用法 官方DOC 比如提取一个数据包 my pcap 中全部带有TLS
  • Oracle数据恢复:强制Resetlogs的可能数据损失

    Oracle数据恢复 强制Resetlogs的可能数据损失 Oracle数据恢复 格式化 ASM及字典损坏案例三则 ORA 00600 kcratr nab less than odr案例一则 SMON recover undo segme
  • 简易DOCKER/K8S使用心得

    1 DOCKER安装 1 1 前置环境 首先 如果使用CentOS 你至少需要7 4以上 从内核角度来说 建议使用8 2及以上 如果是7 4以下的版本 可以通过设置仓库到7 4以上版本 再 yum install centos releas
  • 计算机内存数值存储方式-原码、反码、补码、数值溢出

    计算机内存数值存储方式 1 原码 一个数的原码 原始的二进制码 有如下特点 最高位做为符号位 0表示正 为1表示负 其它数值部分就是数值本身绝对值的二进制数 负数的原码是在其绝对值的基础上 最高位变为1 下面数值以1字节的大小描述 原码表示
  • 手把手教你实现一个向量

    文章目录 什么是向量 向量提供哪些接口 实现 宏定义 定义类 成员变量 构造函数与析构函数 构造函数 析构函数 成员函数 size get r put r e expand insert r e remove lo hi remove r
  • nodejs获取时间戳

    可以使用 JavaScript 内置的 Date 对象来获取当前的时间戳 可以使用 Date now 方法来获取当前的时间戳 consttimestamp Date now console log timestamp 也可以使用 Date
  • 轻松掌握Python自动化工具,解锁PyAutoGUI的强大功能

    前言 PyAutoGUI是一个用于图像识别和鼠标 键盘控制的Python库 它提供了一组函数和方法 用于自动化屏幕上的鼠标移动 点击 拖拽和键盘输入 以及执行图像识别和处理 本文旨在帮助读者入门 PyAutoGUI 理解其基础概念和掌握最佳
  • js逆向-国密SM2初探

    目录 前言 目标网站 加密分析 加密定位 结尾 本文仅供学习使用 切勿非法使用 如有侵权请联系作者及时删除 前言 无意中看到一个网站采用国密SM2算法进行登陆参数进行加密 之前接触的加密基本都是国外的算法 例如RSA DES MD5等等 国
  • 13位时间戳单位为毫秒,10位字符串单位为秒。时间戳转换日期数字格式100%全乎

    时间戳转换为年月日时分秒数字格式 注意时间戳有2种 13位时间戳 单位为毫秒 10位字符串 单位为秒 接口返回1616160878418 微秒 期望格式2021 03 19 21 34 35 标签 java new Date 变成GMT G
  • tar打包备份+ubuntu系统的修复

    如果ubuntu系统出问题 通过备份文件可以简单快速地修复ubuntu系统 tar打包十分必要 一 tar打包备份 1 进入管理员账户 sudo su 2 打包 tar cvPzf 20191203ubuntu1804 tgz exclud
  • 关于static 的各种数据类型 及在面向对象编程中的应用

    一 按存储区域分 全局变量 静态全局变量和静态局部变量都存放在内存的静态存储区域 局部变量存放在内存的栈区 1定义全局静态变量的好处 lt 1 gt 不会被其他文件所访问 修改 lt 2 gt 其他文件中可以使用相同名字的变量 不会发生冲突
  • 2023杭电暑假多校4 题解

    3 Simple Set Problem 题意 k 个多重集合 每个集合选出一个数形成新集合A 求 m a x A m
  • macos多合一系统安装u盘制作器_U 盘多系统安装盘制作神器YUMI

    通常我们一个 U 盘只能制作成一个系统安装盘 比如制作好一个 Windows 10 安装盘 日后想要用到 Linux WinPE 等安装盘时 只能重新制作一遍 非常浪费时间 而且现在 U 盘容量都很大 如果只放一个系统 同样就会白白浪费 U
  • 软考-操作系统

    考点梳理 进程管理 考点1 进程的状态 考法分析 本考点主要考查形式主要是根据图示判断相关状态位置或状态变迁条件 要点分析 操作系统三态模型如下图所示 操作系统五态模型 备考点拨 掌握操作系统三态模型 五态模型的状态位置及其状态变迁条件 考
  • arcgis 10.8 for win10安装教程

    本文主要记录arcgis安装步骤 遇到的问题等 文章目录 一 前提 二 下载与安装 1 下载 2 安装 参考资料 一 前提 下载前需要确定 1 ArcGIS对系统的要求 安装之前系统需安装 net framework 4 5 以上版本 我的
  • 【翻译】 WireGuard 何去何从?

    请考虑订阅 LWN订阅是 LWN net 的生命线 如果您喜欢这些内容并希望看到更多 您的订阅将有助于确保 LWN 继续蓬勃发展 请访问此页面加入我们 让 LWN 继续在网络上传播 作者 Jonathan Corbet 2019年3月25日
  • Angular2 之 单元测试

    单元测试需要掌握的知识点 karma conf js的配置 具体了解到每一项的意义 这样才能真正的了解这个配置是如何配置的 甚至才可以做到自己的配置 组件的测试 单独的service测试 Angular的测试工具 Angular的测试工具类
  • 飞机大战(C语言版)

    大一下要交课程设计 于是就用C语言写了一个飞机大战小游戏 没有用到第三方库 飞机和子弹的移动使用的光标移动函数 所以没有卡顿 其中w s a d分别表示上下左右 包括大写 空格发射子弹 游戏结束后可选择是否储存游戏数据 该程序复制后可直接使