广告
返回顶部
首页 > 资讯 > 后端开发 > 其他教程 >C语言实现飞机大战小游戏
  • 280
分享到

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

2024-04-02 19:04:59 280人浏览 泡泡鱼
摘要

本文实例为大家分享了C语言实现飞机大战小游戏的具体代码,供大家参考,具体内容如下 技术原型 1、void Gotoxy(int x, int y) 函数,该函数可以使光标去到(x,y

本文实例为大家分享了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;
 }

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

--结束END--

本文标题: C语言实现飞机大战小游戏

本文链接: https://www.lsjlt.com/news/150842.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • C语言实现飞机大战小游戏
    本文实例为大家分享了C语言实现飞机大战小游戏的具体代码,供大家参考,具体内容如下 技术原型 1、void gotoxy(int x, int y) 函数,该函数可以使光标去到(x,y...
    99+
    2022-11-13
  • C语言实现飞机大战小游戏完整代码
     大一课设做的飞机大战,可以进行登入和注册,这个是利用单链表做的,源代码已经给出,这个是最基本的飞机大战模式,我设置了几个功能,比如排行榜之类的。排行榜是用结构体数组做的,...
    99+
    2022-11-13
  • C语言实现简单的飞机大战游戏
    目录一、项目描述和最终的成果展示二、输出一个飞机模型三、实现控制飞机移动的功能四、添加发射激光功能五、添加靶子和分数统计功能本文实例为大家分享了C语言实现简单飞机大战游戏的具体代码,...
    99+
    2022-11-13
  • C++实现飞机大战游戏
    本文实例为大家分享了C++实现飞机大战游戏的具体代码,供大家参考,具体内容如下 代码是单线程执行,无界面,(博主下一步学习QT之后融入)还有待改进。先放张界面图: 话不多说 上...
    99+
    2022-11-13
  • javascript实现飞机大战小游戏
    本文实例为大家分享了javascript实现飞机大战游戏的具体代码,供大家参考,具体内容如下 文档结构如下 其中tool文件中只使用了随机数,audio中是存放的音乐文件,imag...
    99+
    2022-11-13
  • Vue实现飞机大战小游戏
    目录使用 Vue 开发一个简略版的飞机大战小游戏一、实现思路二、所需知识点三、实现步骤使用 Vue 开发一个简略版的飞机大战小游戏 如题,假设你为了向更多访问你博客的人展示你的技术,...
    99+
    2022-11-13
  • java实现飞机大战小游戏
    本文实例为大家分享了java实现飞机大战游戏的具体代码,供大家参考,具体内容如下 MyPanel类 package  P; import java.awt.Font; import...
    99+
    2022-11-13
  • C语言用封装方法实现飞机大战游戏
    目录一、项目描述和最终的成果展示二、用函数进行封装三、新型的发射子弹功能四、实现移动的敌机功能和更正屏幕闪烁,清除光标功能五、订正一些BUG和完成一些美化本文实例为大家分享了C语言用...
    99+
    2022-11-13
  • 用JS实现飞机大战小游戏
    本文实例为大家分享了JS实现飞机大战小游戏的具体代码,供大家参考,具体内容如下 小的时候玩的飞机大战感觉还蛮神奇,今天自己就学着做了一个 先制作好要做好的几步以及背景样式 var...
    99+
    2022-11-12
  • Python飞机大战小游戏
    游戏规则:键盘上下左右键控制飞机移动 游戏展示图片: 源码: 第一个py命名为:plane_main.py import pygamefrom plane_sprites import *class PlaneGame(object): ...
    99+
    2023-09-08
    python
  • C语言实现飞机大战
    本文实例为大家分享了C语言实现飞机大战的具体代码,供大家参考,具体内容如下 #include<stdio.h> #include<stdlib.h> #in...
    99+
    2022-11-13
  • C++基于EasyX框架实现飞机大战小游戏
    正式使用Easyx之前,你需要先安装他!! EasyX 2022 版 (2022-9-1 更新) - EasyX 选择合适的版本安装 安装结束后就可以开始敲代码啦! 这里作者使用的...
    99+
    2023-01-06
    C++ EasyX飞机大战游戏 C++ 飞机大战游戏 C++ EasyX游戏
  • C语言怎么用封装方法实现飞机大战游戏
    本文小编为大家详细介绍“C语言怎么用封装方法实现飞机大战游戏”,内容详细,步骤清晰,细节处理妥当,希望这篇“C语言怎么用封装方法实现飞机大战游戏”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。一、项目描述和最终的成...
    99+
    2023-06-30
  • C语言实现飞机游戏(1)
    本文实例为大家分享了C语言实现飞机游戏的具体代码,供大家参考,具体内容如下 本节我们将在上一节 弹跳小球 实现基础上完成简单的飞机游戏。 scanf 控制飞机移动 我们可以使用 sc...
    99+
    2022-11-13
  • C语言实现飞机游戏(2)
    本文实例为大家分享了C语言实现飞机游戏的具体实现代码,供大家参考,具体内容如下 本节我们将在上一节的基础上对飞机游戏进行改造完善。 基本框架 从本节起,为了避免把所有代码都放进mai...
    99+
    2022-11-13
  • python实现简单飞机大战小游戏
    为了熟悉Python基础语法,学习了一个经典的案例:飞机大战,最后实现效果如下: 实现步骤: ①下载64位对应python版本的pygame:pygame-1.9.6-cp38-c...
    99+
    2022-11-11
  • 原生JS实现飞机大战小游戏
    本文实例为大家分享了JS实现飞机大战小游戏的具体代码,供大家参考,具体内容如下 <html> <head> <title> 飞机大战 &...
    99+
    2022-11-12
  • javascript实现简单飞机大战小游戏
    本文实例为大家分享了javascript实现飞机大战小游戏的具体代码,供大家参考,具体内容如下 效果图 html文件 <!DOCTYPE html> <html ...
    99+
    2022-11-13
  • JavaScript实现前端飞机大战小游戏
    本文实例为大家分享了JavaScript实现前端飞机大战的具体代码,供大家参考,具体内容如下 html: <!DOCTYPE html> <html>    ...
    99+
    2022-11-13
  • java如何实现飞机大战小游戏
    本篇内容介绍了“java如何实现飞机大战小游戏”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!MyPanel类package &nb...
    99+
    2023-07-01
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作