C语言实现飞机大战小游戏

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

技术原型

1、void gotoxy(int x, int y) 函数,该函数可以使光标去到(x,y)的位置进行打印;
2、链表,用于存储状态;
3、windows.h中有非阻塞输入,_kbhit();
4、随机生成数;
5、视觉暂留;
6、碰撞检测;
7、清屏函数;
8、设置边界;

技术路线

1、设置一个边界;
2、维护一个子弹的列表;
3、维护一个敌机的列表;
4、初始化飞机的位置;
5、每隔一秒钟生成一架敌机,生成位置x坐标随机,坐标为0;
6、设置点击空格生成子弹;
7、设置while(1)循环,在其中每次进行清屏和更新;
8、每次更新遍历子弹链表,使所有子弹的位置向上移动1;
9、每次更新遍历敌机链表,使所有敌机的位置向下移动1;
10、碰撞检测,遍历子弹和敌机列表,发现碰撞则从各自的链表中移除碰撞的节点;
11、当有敌机碰撞到本机游戏结束;
12、当子弹或者敌机碰撞到边界,则从链表中移除;
13、每次检测到碰撞加一分。

实现效果

花费时间

约6小时30分钟

代码

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include <windows.h>
#include <math.h>

struct node {
    int             x;
    int                y;
    struct node*    next;
};

typedef struct node  node_t;
typedef struct node* nodeptr_t;

void gotoxy(int x, int y); //光标定位函数
void print_plane(int x, int y); //打印飞机
nodeptr_t generate_bullet(nodeptr_t listnode, int x, int y); //生成子弹
void print_bullet(nodeptr_t listnode); //打印子弹
nodeptr_t update_bullet(nodeptr_t listnode); //更新子弹位置
nodeptr_t generate_target(nodeptr_t listnode, int x); //生成敌机
void print_target(nodeptr_t listnode); //打印敌机
nodeptr_t update_target(nodeptr_t listnode); //更新敌机位置
int collision_detection(nodeptr_t bulletlist, nodeptr_t targetlist); //碰撞检测
bool is_gameover(int x,int y, nodeptr_t targetlist); // 游戏结束
void clear(nodeptr_t bulletlist, nodeptr_t targetlist);

int main() {
    int plane_x = 0, plane_y = 19; //飞机位置
    char control; //输入
    bool isfire = 0; //是否开火

    nodeptr_t target = nullptr; //敌机链表
    target = (nodeptr_t)malloc(sizeof(node_t));
    target->next = nullptr;
    target->x = 0;
    target->y = 0;

    nodeptr_t bullet = nullptr; //子弹链表
    bullet = (nodeptr_t)malloc(sizeof(node_t));
    bullet->next = nullptr;
    bullet->x = 0;
    bullet->y = 0;

    int subtime = 0;
    time_t starttime;
    starttime = time(NULL);
    int grade = 0; //分数
    int targetspeed = 0;
    int bulletspeed = 0;

    while(1)
    {
        system("cls");
        time_t currenttime;
        currenttime = time(NULL);
        //每隔一秒生成一架敌机
        if (currenttime - starttime - subtime > 0)
        {
            srand((unsigned)time(NULL));
            unsigned int target_y = rand() % 14 + 3;
            target = generate_target(target, target_y);
        }
        subtime = currenttime - starttime;
        //开火则生成子弹
        if (isfire)
        {
            bullet = generate_bullet(bullet, plane_x, plane_y - 1);
            isfire = 0;
        }
        //打印敌机
        print_target(target);
        targetspeed++;
        if(targetspeed % 2 == 0)
            target = update_target(target);
        //打印子弹
        print_bullet(bullet);
        bulletspeed++;
        if (bulletspeed % 2 == 0)
            bullet = update_bullet(bullet);
        //碰撞检测
        grade = grade + collision_detection(bullet, target);
        gotoxy(0, 25);
        printf("SCORE: %d", grade);
        //打印飞机
        print_plane(plane_x, plane_y);
        //敌机本机是否相撞
        bool isgameover = is_gameover(plane_x, plane_y, target);
        //Sleep(100);
        //非阻塞键盘输入
        if (isgameover)
        {
            clear(target, bullet);
            plane_x = 0;
            plane_y = 19;
            isfire = 0;
            system("cls");
            gotoxy(8, 8);
            printf("SCORE: %d", grade);
            gotoxy(8, 9);
            printf("GAME OVER");
            grade = 0;
            break;
        }
        if (_kbhit())
        {
            control = _getch();
            if (control == ' ')
                isfire = 1;
            else
                isfire = 0;
            if (control == 'w' && plane_y > 0)
                plane_y--;
            if (control == 's' && plane_y < 20)
                plane_y++;
            if (control == 'a' && plane_x > 0)
                plane_x--;
            if (control == 'd' && plane_x < 20)
                plane_x++;
        }

    }
    return 0;
}

void gotoxy(int x, int y)//光标定位函数
{
    COORD p;//定义结构体变量p
    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);//获取当前函数句柄
    p.X = x;
    p.Y = y;//将光标的目标移动位置传递给结构体
    SetConsoleCursorPosition(handle, p);//移动光标
    return;
}

void print_plane(int x, int y) //打印飞机
{
    if (x == 0)
    {
        gotoxy(x, y);
        printf("*");
        gotoxy(x, y + 1);
        printf("***");
        gotoxy(x, y + 2);
        printf(" *");
    }
    else if (x == 1)
    {
        gotoxy(x, y);
        printf("*");
        gotoxy(x-1, y + 1);
        printf("****");
        gotoxy(x-1, y + 2);
        printf("* *");
    }
    else if (x == 20)
    {
        gotoxy(x, y);
        printf("*");
        gotoxy(x - 2, y + 1);
        printf("***");
        gotoxy(x - 1, y + 2);
        printf("* ");
    }
    else if (x == 19)
    {
        gotoxy(x, y);
        printf("*");
        gotoxy(x - 2, y + 1);
        printf("****");
        gotoxy(x - 1, y + 2);
        printf("* *");
    }
    else
    {
        gotoxy(x, y);
        printf("*");
        gotoxy(x - 2, y + 1);
        printf("*****");
        gotoxy(x - 1, y + 2);
        printf("* *");
    }
    return;
}

nodeptr_t generate_bullet(nodeptr_t listnode, int x, int y)
{
    nodeptr_t newbullet = nullptr; //子弹链表
    newbullet = (nodeptr_t)malloc(sizeof(node_t));
    newbullet->next = listnode->next;
    newbullet->x = x;
    newbullet->y = y;
    listnode->next = newbullet;
    return listnode;
}

void print_bullet(nodeptr_t listnode)
{
    nodeptr_t templist = listnode;
    while (templist->next != nullptr)
    {
        gotoxy((templist->next->x), templist->next->y);
        printf("|");
        templist = templist->next;
    }
    return;
}

nodeptr_t update_bullet(nodeptr_t listnode)
{
    nodeptr_t templist = listnode;
    while (templist->next != nullptr)
    {
        if (templist->next->y > 0)
            (templist->next->y)--;
        else
        {
            nodeptr_t tempnode = templist->next;
            templist->next = tempnode->next;
            tempnode->next = nullptr;
            free(tempnode);
        }
        if (templist->next != nullptr)
            templist = templist->next;
        else
            break;
    }
    return listnode;
}

nodeptr_t generate_target(nodeptr_t listnode, int x)
{
    nodeptr_t newtarget = nullptr; //子弹链表
    newtarget = (nodeptr_t)malloc(sizeof(node_t));
    newtarget->next = listnode->next;
    newtarget->x = x;
    newtarget->y = 0;
    listnode->next = newtarget;
    return listnode;
}

void print_target(nodeptr_t listnode)
{
    nodeptr_t templist = listnode;
    while(templist->next != nullptr)
    {
        gotoxy(templist->next->x, templist->next->y);
        printf("+");
        templist = templist->next;
    }
    return;
}

nodeptr_t update_target(nodeptr_t listnode)
{
    nodeptr_t templist = listnode;
    while (templist->next != nullptr)
    {
        if (templist->next->y < 21)
            (templist->next->y)++;
        else
        {
            nodeptr_t tempnode = templist->next;
            templist->next = tempnode->next;
            tempnode->next = nullptr;
            free(tempnode);
        }
        if (templist->next != nullptr)
            templist = templist->next;
        else
            break;
    }
    return listnode;
}

int collision_detection(nodeptr_t bulletlist, nodeptr_t targetlist)
{
    int grade = 0;
    nodeptr_t tempbulletlist = bulletlist;
     while(tempbulletlist->next != nullptr)
     {
        nodeptr_t temptargetlist = targetlist;
         while(temptargetlist->next != nullptr)  
         {
            
             if(temptargetlist->next->x == (tempbulletlist->next->x) && temptargetlist->next->y > tempbulletlist->next->y)
             {
                 nodeptr_t tempnode = temptargetlist->next;
                temptargetlist->next = tempnode->next;
                tempnode->next = nullptr;
                free(tempnode);

                tempnode = tempbulletlist->next;
                tempbulletlist->next = tempnode->next;
                tempnode->next = nullptr;

                 free(tempnode);
                grade++;
                break;

             }
            if (temptargetlist->next != nullptr)
                temptargetlist = temptargetlist->next;
            else
                break;
         }
        if (tempbulletlist->next != nullptr)
            tempbulletlist = tempbulletlist->next;
        else
            break;
     }
    return grade;
}

 bool is_gameover(int x, int y, nodeptr_t targetlist)
 {
     nodeptr_t temptargetlist = targetlist;
     while (temptargetlist->next != nullptr)
     {
         int tempsub = abs((temptargetlist->next->x) - x);
         if (tempsub == 0 && temptargetlist->next->y > y)
         {
             return 1;
         }
         else if(tempsub == 1 && temptargetlist->next->y > y + 1)
         {
             return 1;
         }
         else if (tempsub == 2 && temptargetlist->next->y > y + 1)
         {
             return 1;
         }
         temptargetlist = temptargetlist->next;
     }
     return 0;
 }

 void clear(nodeptr_t bulletlist, nodeptr_t targetlist)
 {
     while (bulletlist->next != nullptr)
     {
         nodeptr_t temp = bulletlist->next;
         bulletlist->next = temp->next;
         //temp->next = nullptr;
         free(temp);
     }
     while (targetlist->next != nullptr)
     {
         nodeptr_t temp = targetlist->next;
         targetlist->next = temp->next;
         //temp->next = nullptr;
         free(temp);
     }
     return;
 }

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

(0)

相关推荐

  • C语言实现简单的飞机大战游戏

    目录 一.项目描述和最终的成果展示 二.输出一个飞机模型 三.实现控制飞机移动的功能 四.添加发射激光功能 五.添加靶子和分数统计功能 本文实例为大家分享了C语言实现简单飞机大战游戏的具体代码,供大家参考,具体内容如下 一.项目描述和最终的成果展示 项目描述: 显示出一个飞机,并可以控制移动,发出激光,打靶练习. 效果展示: 二.输出一个飞机模型 先输出一个飞机模型.后面再进一步完成控制飞机的功能. 代码如下: //输出飞机图案 #include<stdio.h> #include<st

  • C语言实现飞机大战小游戏完整代码

    大一课设做的飞机大战,可以进行登入和注册,这个是利用单链表做的,源代码已经给出,这个是最基本的飞机大战模式,我设置了几个功能,比如排行榜之类的.排行榜是用结构体数组做的,已及冒泡排序,并且在文件里保存信息.比较简单. 这个是注册页面规范: 这个是登入页面: 游戏菜单:  飞机大战页面:  话不多说,直接上代码 以下是源代码  #include"stdio.h" #include"windows.h" //用于获取窗口的句柄与屏幕控制 #include"co

  • C语言之飞机大战游戏

    本文实例为大家分享了C语言之飞机大战游戏的具体代码,供大家参考,具体内容如下 技术原型 1.void gotoxy(int x, int y) 函数,该函数可以使光标去到(x,y)的位置进行打印: 2.链表,用于存储状态: 3.windows.h中有非阻塞输入,_kbhit(): 4.随机生成数: 5.视觉暂留: 6.碰撞检测: 7.清屏函数: 8.设置边界: 技术路线 1.设置一个边界: 2.维护一个子弹的列表: 3.维护一个敌机的列表: 4.初始化飞机的位置: 5.每隔一秒钟生成一架敌机,生

  • C语言实现飞机大战

    本文实例为大家分享了C语言实现飞机大战的具体代码,供大家参考,具体内容如下 #include<stdio.h> #include<stdlib.h> #include<conio.h> #include<Windows.h> int score = 0; int plane_col, plane_row;//·É»úλÖà int bullet_col,bullet_row;//×Óµ¯µÄλÖà int area_height, area_width;

  • C语言控制台实现字符飞机大战

    本文实例为大家分享了C语言实现字符飞机大战的具体代码,供大家参考,具体内容如下 先看看效果吧 大一的时候做的,当时没有好的代码习惯,所以代码有点乱,代码直接复制就能用了,功能可以自行拓展. 代码: #include <stdio.h> #include <stdlib.h> #include <conio.h> int main () { int life=6;//生命 int i,j/*循环*/,plane_x,plane_y/*飞机所在坐标*/,a; plane_x

  • C语言实现简单飞机大战

    本文实例为大家分享了C语言实现飞机大战的具体代码,供大家参考,具体内容如下 定义四个函数实现飞机大战 #include<stdio.h> #include<windows.h> #include<conio.h> //定义全局变量 int high,width; //定义边界 int position_x,position_y; //飞机位置 int bullet_x,bullet_y; //子弹位置 int enemy_x,enemy_y; int score; in

  • C语言版飞机大战游戏

    C语言版飞机大战,供大家参考,具体内容如下 不多说直接上代码 #include<iostream> #include<windows.h> #include<conio.h> #include<time.h> #include<string> using namespace std; /*=============== all the structures ===============*/ typedef struct Frame { COOR

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

    目录 一.项目描述和最终的成果展示 二.用函数进行封装 三.新型的发射子弹功能 四.实现移动的敌机功能和更正屏幕闪烁,清除光标功能 五.订正一些BUG和完成一些美化 本文实例为大家分享了C语言用封装方法实现飞机大战游戏的具体代码,供大家参考,具体内容如下 这是上一次的飞机大战游戏的项目.项目: 最简单的飞机大战游戏 上次没有用函数进行的封装.这次在上次的基础上进行封装和一些功能的优化. 一.项目描述和最终的成果展示 项目描述:   在上一次的基础上用函数进行了封装,对于一些功能也进行了一些优化.

  • C语言实现飞机大战程序设计

    本文实例为大家分享了C语言实现飞机大战的具体代码,供大家参考,具体内容如下 作业要求: 1.游戏当中出现多类型敌人2.游戏中能进行存档和读档3.玩家控制的飞机由多字符组成4.游戏过程中没有出现BUG 5.游戏显示制作者信息6.游戏当中带有BGM7.其他个性化设定 成效: 代码: #include<stdio.h> #include<stdlib.h> #include<conio.h> #include<windows.h> #include<mmsy

  • C语言代码实现飞机大战

    本文实例为大家分享了C语言实现简单飞机大战的具体代码,供大家参考,具体内容如下 这个游戏的功能很单一,也就是"飞机大战",哈哈哈哈.总共只有300多行代码左右,你也可以想想它会有多简陋,把它复制下来编译一下可以直接执行,需要的同学可以自取~ PS:我运行的环境是 dev c++,前提你要在C99的环境中执行 以下是源代码 #include<stdio.h> #include<stdio.h> #include<windows.h> //将用户从键盘获

随机推荐