服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - C++编写实现飞机大战

C++编写实现飞机大战

2022-12-20 16:531coder. C/C++

这篇文章主要为大家详细介绍了C++编写实现飞机大战,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了C++编写实现飞机大战的具体代码,供大家参考,具体内容如下

前几天看大佬写了个神经网络训练AI玩飞机大战,我想,凭我现有知识能不能也写一个飞机大战,就进行了尝试,成果如下。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<iostream>
#include<ctime>
#include<stdlib.h>
#include<windows.h>
using namespace std;
const int mapx = 40, mapy = 35, cost = 2, prise = 5;   //cost: cost of bullet,   prise: prise of killing a enemy.
class plane
{
    public:
        void start();
    private:
        void reset();
        void get_enemy(int &y);
        void print() const;
        void update_print();
        char map[mapx][mapy];/*      plane model:      /=|=\           */
        int plane_y, plane_x, score, cont;
};

到此我们设计了飞机的模型(我水平不够 整个游戏就用一个类了- -,这个类其实是整个游戏的类 不是飞机类)关于变量cont的说明我放在后面了 接下来我写了一个初始化函数,为类内变量初始化。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void plane::reset()
{
    for(int i = 0; i < mapx; i++)
    {
        for(int j = 0; j < mapy; j++)
        {
            if(!i || !j || j == mapy - 1)
            {
                map[i][j] = '#';
            }
            else
                map[i][j] = ' ';
        }
    }
    plane_x = mapx - 1;
    plane_y = mapy/2 - 2;
    score = cont = 0;
    map[plane_x][plane_y] = '/';
    map[plane_x][plane_y + 1] = map[plane_x][plane_y + 3] = '=';
    map[plane_x][plane_y + 2] = '|';
    map[plane_x][plane_y + 4] = '\\';
}

然后我利用时间参数的随机数得到敌机的位置,这里其实有个问题,因为时间是按一定顺序均匀变化的,我们如果直接用时间作随机数种子的话,敌机的出现会非常均匀,因此我引入了一个cont变量,用来打乱我们均匀的时间参数的个位数。具体使用见后文。

?
1
2
3
4
5
6
7
8
9
10
void plane::get_enemy(int &y) const
{
    srand(int(time(0)));
    int n = rand();
    if(cont%2)
        n -= cont;
    else
        n += cont;
    y = n % (mapy - 2) + 1;
}

这个函数就是随机生成敌机的位置,cont在此就起到打乱随机生成数的个位数的目的,每更新一次,cont++,为防止cont过大,我规定cont==10时,就将cont = 0,使其能在1到9变化,影响个位数。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
void plane::print() const
{
    system("cls");
    for(int i = 0; i < mapx; i++)
    {
        for(int j = 0; j < mapy; j++)
        {
            cout<<map[i][j];
        }
        cout<<endl;
    }
    cout<<"Score : "<<score<<'.'<<endl<<"Pay "<<cost<<" scores to send '+' and get "<<prise<<" scores by killing enemies."<<endl;
}

这里是一个打印的函数,不赘述。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
void plane::update_print()
{
    for(int i = 1; i < mapx; i++)
    {
        for(int j = 1; j < mapy - 1; j++)
        {
            if(map[i][j] == 'M')
            {
                if(i == mapx - 1)
                    map[i][j] = ' ';
                else if(map[i + 1][j] == '+')
                {
                    map[i][j] = map[i+1][j] = ' ';
                    score += prise;
                }
            }
            else if(map[i][j] == '+')
            {
                map[i][j] = ' ';
                if(i != 1)
                    map[i-1][j] = '+';
            }
        }
    }
    for(int i = mapx - 2; i > 0; i--)
    {
        for(int j = 1; j < mapy - 1; j++)
        {
            if(map[i][j] == 'M')
            {
                if(i != mapx - 1)
                    if(map[i+1][j] == '+')
                    {
                        map[i + 1][j] = ' ';
                        score += prise;
                    }
                    else
                        map[i + 1][j] = 'M';
                map[i][j] = ' ';
            }
        }
    }
    int enemy_y;
    get_enemy(enemy_y);
    if(map[1][enemy_y] == '+')
    {
        map[1][enemy_y] = ' ';
        score += prise;
    }
    else
        map[1][enemy_y] = 'M';
        
    for(int i = 0; i < 5; i++)
    {
        if(map[plane_x][plane_y + i] != 'M')
            map[plane_x][plane_y + i] = ' ';
    }
    bool jleft, jright, jup, jdown;
    jleft = jright = jup = jdown = false;
 
    if(GetAsyncKeyState(VK_LEFT) & 0x8000)
        if(plane_y != 1)
            jleft = true;
    if(GetAsyncKeyState(VK_RIGHT) & 0x8000)
        if(plane_y + 4 != mapy - 2)
            jright = true;
    if(GetAsyncKeyState(VK_UP) & 0x8000)
        if(plane_x != 1)
            jup = true;
    if(GetAsyncKeyState(VK_DOWN) & 0x8000)
        if(plane_x != mapx - 1)
            jdown = true;
    if(!(jleft && jright))
    {
        if(jleft)
            plane_y--;
        if(jright)
            plane_y++;
    }
    if(!(jup && jdown))
    {
        if(jup)
            plane_x--;
        if(jdown)
            plane_x++;
    }
    if(GetAsyncKeyState(VK_SPACE) & 0x8000)
        {
            score -= cost;
            if(map[plane_x - 1][plane_y + 2] == ' ')
                map[plane_x - 1][plane_y + 2] = '+';
            else if(map[plane_x - 1][plane_y + 2] == 'M')
            {
                map[plane_x - 1][plane_y + 2] = ' ';
                score += prise;
            }
        }
 
    if(map[plane_x][plane_y]=='M'||map[plane_x][plane_y+1]=='M'||
    map[plane_x][plane_y+2]=='M'||map[plane_x][plane_y+3]=='M'||map[plane_x][plane_y+4]=='M')
    {
        system("cls");
        for(int i = 0; i < mapx; i++)
        {
            cout<<"GAME OVER."<<endl;
        }
        cout<<"Your final scores are "<<score<<'.'<<endl;
        system("pause");
        exit(1);
    }
    map[plane_x][plane_y] = '/';
    map[plane_x][plane_y + 1] = map[plane_x][plane_y + 3] = '=';
    map[plane_x][plane_y + 2] = '|';
    map[plane_x][plane_y + 4] = '\\';
    cont++;
    if(cont == 10)
            cont = 0;
    print();
}

这个函数我其实感觉自己写的太大了,应该进一步分装,这确实是个不足之处。具体操作就是每轮对飞机的移动,还有子弹和敌机的前进以及判断子弹是否达到敌机和我们的飞机是否撞到敌机。其中我用到了windows.h文件中的GetAsyncKeyState函数,其参数为键盘某个键的VK值(可查表),返回一个16个位的数(因操作系统不同而不同,我的计算机是返回16位)。若该键在上次判断到此次判断之间被按下过,则0号位为1,反之为0;若该键正在被按下,则15号位为1,反之为0.将返回值与0x8000作“与&”操作,则第一位的数字决定了我们&操作的结果。因为XXXX XXXX XXXX XXXX & 1000 0000 0000 0000 == X000 0000 0000 0000.从而操控我们的飞机。

?
1
2
3
4
5
6
7
8
9
void plane::start()
{
    reset();
    while(1)
    {
        Sleep(50);
        update_print();
    }
}

开始函数,用以从类外部访问类内的private函数,并且组织起循环。
然后 用主函数运行即可。

?
1
2
3
4
5
6
int main()
{
    plane plane_game;
    plane_game.start();
    return 0;
}

效果如下:

C++编写实现飞机大战

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

原文链接:https://blog.csdn.net/weixin_52104555/article/details/115174594

延伸 · 阅读

精彩推荐
  • C/C++C++存储方案和动态分配

    C++存储方案和动态分配

    这篇文章主要介绍了C++存储方案和动态分配,下面来看文章对此的详细介绍吧,需要的小伙伴可以参考一下...

    Coder梁(ID:Coder_LT)10632022-08-04
  • C/C++详解C++中StringBuilder类的实现及其性能优化

    详解C++中StringBuilder类的实现及其性能优化

    在Java和C#中,StringBuilder可以创造可变字符序列来动态地扩充字符串,那么在C++中我们同样也可以实现一个StringBuilder并且用来提升性能,下面就来详解C++中Stri...

    jimmyjmh, monkee7612021-04-04
  • C/C++C++编写非侵入式接口

    C++编写非侵入式接口

    这篇文章主要介绍了C++编写非侵入式接口的相关资料,需要的朋友可以参考下...

    huaxiazhihuo11542021-05-24
  • C/C++Qt中QtWebEngine加载本地网页跨域问题的总结

    Qt中QtWebEngine加载本地网页跨域问题的总结

    本文主要介绍了Qt中QtWebEngine加载本地网页跨域问题的总结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋...

    charlee443772022-11-20
  • C/C++C语言实现简单的扫雷游戏

    C语言实现简单的扫雷游戏

    这篇文章主要为大家详细介绍了C语言实现简单的扫雷游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    惊喜狍子4862021-10-14
  • C/C++适合初学者的C语言数据类型的讲解

    适合初学者的C语言数据类型的讲解

    在 C 语言中,数据类型指的是用于声明不同类型的变量或函数的一个广泛的系统。变量的类型决定了变量存储占用的空间,以及如何解释存储的位模式。...

    每天都要学一点哦5752022-11-04
  • C/C++从汇编看c++中变量类型的深入分析

    从汇编看c++中变量类型的深入分析

    本篇文章是对c++中的变量类型进行了详细的分析介绍。需要的朋友参考下...

    C++教程网3142020-11-27
  • C/C++C/C++的各种字符串函数你知道几个

    C/C++的各种字符串函数你知道几个

    这篇文章主要为大家详细介绍了C/C++的各种字符串函数,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够...

    蔡高数4222022-10-10