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

目录
  • 一、项目描述和最终的成果展示
  • 二、用函数进行封装
  • 三、新型的发射子弹功能
  • 四、实现移动的敌机功能和更正屏幕闪烁,清除光标功能
  • 五、订正一些BUG和完成一些美化

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

效果图如下:

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

(0)

相关推荐

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

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

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

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

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

  • 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语言实现简单飞机大战

    本文实例为大家分享了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语言用封装方法实现飞机大战游戏

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

  • 基于Java语言在窗体上实现飞机大战小游戏的完整步骤

    目录 小组项目 模块需求描述 总体开发思想 功能实现 1.登录与结束界面 2.播放音乐 3.子弹 运行测试 登陆界面 发射子弹 总结 小组项目 飞机大战:用 Java 语言在窗体上实现飞机大战小游戏,运行程序后,出现一个窗体,在窗体上用鼠标控制英雄机的移动,通过子弹打击敌机进行分数的计算,以及英雄机血量的计算等. 主要模块:登陆界面.音乐.子弹.敌机.英雄机.背景图.结束界面.BOSS 机.分数计算.血量计算. 负责模块:登陆界面.音乐.子弹.结束界面. 模块需求描述 登陆界面:运行程序后,弹出

  • python实现飞机大战游戏

    飞机大战(Python)代码分为两个python文件,工具类和主类,需要安装pygame模块,能完美运行(网上好多不完整的,调试得心累.实现出来,成就感还是满满的),如图所示: 完整代码如下: 1.工具类plane_sprites.py import random import pygame # 屏幕大小的常量 SCREEN_RECT = pygame.Rect(0, 0, 480, 700) # 刷新的帧率 FRAME_PER_SEC = 60 # 创建敌机的定时器常量 CREATE_ENEM

  • python实现飞机大战游戏(pygame版)

    简介 使用python实现pygame版的飞机大战游戏: 环境:Windows系统+python3.8.0 游戏规则: 1.点击"PLAY"或者按键"P"开始游戏: 2.敌机根据设置频率从顶部随机位置生成,生成后向下移动: 3.飞船在底部中间生成,玩家使用上下左右键控制飞船移动,敲击空格键发射子弹: 4.子弹打到敌机,该敌机产生爆炸效果并累计分数到右上角: 5.消灭10只飞机后,等级升高,敌机生成频率变快,下落速度也变快: 6.当三条命都消失了,游戏结束. 游戏运行

  • Python3实现飞机大战游戏

    本文实例为大家分享了Python3实现飞机大战游戏的具体代码,供大家参考,具体内容如下 1.主程序:plane_main.py import pygame from plane_sprites import * class PlaneGame(object): #飞机大战主游戏类 def __init__(self): print("游戏初始化") #1.创建游戏的窗口 self.screen = pygame.display.set_mode(SCREEN_RECT.size) #2

  • Unity3D实现飞机大战游戏(2)

    本文为大家分享了Unity3D飞机大战游戏第一部分的实现代码,供大家参考,具体内容如下 让飞机可以发射子弹 准备工作: 1.将子弹设置成预制体 2.在飞机下新建一个子物体Gun 3.调整好位置以后,将子弹设置成预制体 //发射子弹的速率 public float rate = 0.2f; public GameObject bullet;//子弹的类型 //发射子弹的方法 public void fire() { //初始化一个子弹预制体 GameObject.Instantiate(bulle

  • js实现飞机大战游戏

    本文实例为大家分享了js实现飞机大战游戏的具体代码,供大家参考,具体内容如下 CSS部分的代码: <style> * { margin: 0px; padding: 0px; } canvas{ border: 1px solid #000; display: block; margin: auto; } </style> JavaScript代码: <!-- 先创建一个画布 --> <canvas id="canvas" width=&quo

  • python飞机大战游戏实例讲解

    记得刚学python那会,作过一个飞机大战小项目,这个项目非常经典,可以帮助初学者提高动手能力,今天把它分享出来. 一.项目介绍 先放几张图片 二.项目实现 1.首先安装库 pip install pygame 2.主要python代码 import pygame from pygame.locals import * import random #https://blog.csdn.net/qq_36079986/article/details/110395731 class HeroPlan

随机推荐