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

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

服务器之家 - 编程语言 - C/C++ - c++使用Easyx图形库实现飞机大战

c++使用Easyx图形库实现飞机大战

2022-07-29 10:34考拉爱睡觉鸭~ C/C++

本文详细讲解了c++使用Easyx图形库实现飞机大战,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

c++使用Easyx图形库实现飞机大战

公共的头文件        common.h

?
1
2
3
4
5
6
7
8
9
10
11
12
#pragma once
#include <graphics.h>
#include <iostream>
#include <string>
#include <map>
#include <list>
#include <thread>
#include <vector>
#include <ctime>
#include <mmsystem.h>
#pragma comment(lib,"winmm.lib")
using namespace std;

管理资源 res.h --- 单例设计模式

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#pragma once
#include "common.h"
class res
{
private:
    res();    //构造函数私有化
public:       //提供公有接口
    static int width(string name);    //通过图片的键,获取资源宽度
    static int height(string name);
    static res* getinstance();        //获取资源(类)的对象---唯一的对象
    static void drawimg(int x, int y, string name);//画图片的位置,通过键去找画哪张图片
//画角色---位置,通过姓名的方式去找  不同类型的角色,用preindex去做标识
    static void drawrole(int x, int y, string name, int preindex);
//播放音乐---windows多线程 dword类型,winapi修饰
    static dword winapi playmusic(lpvoid lparame);
    ~res();
public:
    static map<string, image*> img;       //图片资源---图片存在map中
    static map<string, string> music; //音乐资源
};

res.cpp

?
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
#include "res.h"
map<string, image*> res::img;   //图片资源    静态数据成员在类外初始化,类名限定
map<string, string> res::music;   //音乐资源
res::res()                      //构造函数为数据成员初始化---路径下处理
{
//背景
    string background = "./res/background.jpg";
//角色---4张---背景图+掩码图
    string roleimg[4] = { "./res/planenormal_1.jpg","./res/planenormal_2.jpg",
    "./res/planeexplode_1.jpg","./res/planeexplode_2.jpg" };
//子弹
    string ballimg[2] = { "./res/bullet1.jpg","./res/bullet2.jpg" };
//敌机
    string enemyimg[4] = { "./res/enemy_1.jpg","./res/enemy_2.jpg","./res/enemyplane1.jpg","./res/enemyplane2.jpg" };
    //string --->image* 本来就是指针,不需要取地址
    img["背景"] = new image;
    img["角色"] = new image[4];
    img["子弹"] = new image[2];
    img["敌机"] = new image[4];
    loadimage(img["背景"], background.c_str());    //加载图片    路径 项目属性多字节
    for (int i = 0; i < 4; i++)
    {
/*假设img["角色"]为p,则p=new image [4];则img["角色"]+i  等效: p+i*/
        loadimage(img["角色"] + i, roleimg[i].data());    //用.data或.cst()转换为字符串
        loadimage(img["敌机"] + i, enemyimg[i].data());  
    }
    for (int i = 0; i < 2; i++)
    {
        loadimage(img["子弹"] + i, ballimg[i].c_str());
    }
 
}
//获取图片的宽度---碰撞的时候需要---返回对象指针,对象指针调用(img类型)数据成员,有一个成员函数
int res::width(string name)
{
//获取对象,获取什么样的属性,(img类型)数据成员有一个getwidth()成员函数---是库中的成员函数
    return getinstance()->img[name]->getwidth();
}
//获取图片的高度
int res::height(string name)
{
    return getinstance()->img[name]->getheight();
}
res* res::getinstance()
{
    static res* res = new res;
    return res;
}
//只有一张图片的贴图: 背景图贴图
void res::drawimg(int x, int y, string name)
{
    putimage(x, y, getinstance()->img[name]);    //贴图 在x,y位置贴对象里面的图片
}
void res::drawrole(int x, int y, string name, int preindex)
{
//多张图片贴图---透明贴图---去背景
    putimage(x, y, getinstance()->img[name] + preindex, notsrcerase);//贴第几张---帧数
    putimage(x, y, getinstance()->img[name] + preindex+1, srcinvert);
}
dword __stdcall res::playmusic(lpvoid lparame)
{
    int key = (int)lparame;    //线程处理函数的参数---强转为int
    switch (key)               //不同的音乐,类型不一样
    {
    case 1:
        mcisendstring("close ./res/f_gun.mp3", 0, 0, 0);    //播放前先关闭
        mcisendstring("open ./res/f_gun.mp3", 0, 0, 0);     //先打开,后播放
        mcisendstring("play ./res/f_gun.mp3", 0, 0, 0);
        break;
    case 2:
        mcisendstring("close ./res/5.mp3", 0, 0, 0);
        mcisendstring("open ./res/5.mp3", 0, 0, 0);
        mcisendstring("play ./res/5.mp3", 0, 0, 0);
        break;
    case 3:
        mcisendstring("close ./res/10.mp3", 0, 0, 0);
        mcisendstring("open ./res/10.mp3", 0, 0, 0);
        mcisendstring("play ./res/10.mp3", 0, 0, 0);
        break;
    }
    return 0;
}
res::~res()
{
    delete img["背景"];
    delete[] img["角色"];
    delete[] img["敌机"];
    delete[] img["子弹"];
}

打飞机游戏.cpp        主函数部分

?
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
#include "control.h"
#include "graph.h"
#include "role.h"
#include "enemy.h"
int main()
{
    srand((unsigned int)time(null));    //随机函数种子---位置不同
    graph* pmap = new graph;
    role* prole = new role;
    enemy* penemy = new enemy;
    control* pcontrol = new control(pmap, prole, penemy);
    beginbatchdraw();         //双缓冲绘图
    while (1)
    {
        cleardevice();        //清屏
        pcontrol->drawmap();  //画地图
        pcontrol->drawrole(); //画角色
        pcontrol->drawenemy();//打敌机前画敌机
        pcontrol->playeemey();
 
        flushbatchdraw();     //显示 执行未完成的绘制任务
    }
    endbatchdraw();
    return 0;
}

point.h        无论是飞机移动还是子弹移动,本质都是坐标的改变 - - - 处理点的变化

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#pragma once
#include "common.h"
class point
{
public:
    enum dir {left,right,down,up}; //枚举点移动的方向,不同的移动方向点的改变是不一样的
    point(int x = 0, int y = 0);
    point(const point& point);     //拷贝构造---点与点之间的赋值
    int& getx();                   //外部接口,获取点的坐标
    int& gety();
    //移动点
    void move(int speed, dir dir); //移动速度 方向---决定坐标如何变化
 
protected:
    int x;
    int y;
};
/* 敌机从上往下移动   角色可以上下左右移动
   s形敌机可以增加x方向增量,增加y方向增量 */

point.cpp

?
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
#include "point.h"
point::point(int x, int y):x(x),y(y)
{
}
point::point(const point& point)
{
    this->x = point.x;
    this->y = point.y;
}
int& point::getx()
{
    // todo: 在此处插入 return 语句
    return x;
}
int& point::gety()
{
    // todo: 在此处插入 return 语句
    return y;
}
void point::move(int speed, dir dir)    //根据方向移动
{
    switch (dir)
    {
    case point::left:
        this->x -= speed;
        break;
    case point::right:
        this->x += speed;
        break;
    case point::up:
        this->y -= speed;
        break;
    case point::down:
        this->y += speed;
        break;
    }
}

element.h        敌机和飞机方向不同,其他基本相同 - - - 抽象元素类

?
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
#pragma once
#include "common.h"
#include "point.h"
//所有的敌机和角色 都是由这个类进行派生的
class element
{
public:
    virtual ~element();    //子类对象初始化父类指针
    element();
    element(int x, int y, string name, bool live, int hp = 0);
    int& getx();
    int& gety();
    bool& getlive();
    int& gethp();
    int getwidth();        //获取宽度,高度
    int getheight();
    void drawelement(int pre);    //画元素---画第几张
    void moveelement(int speed, point::dir dir); //移动元素
 
protected:
    point point;        //元素在窗口上的位置
    string name;        //元素的名字
    bool live;          //存在的标记---敌机/子弹会消失
    int hp;             //血量
};

element.cpp

?
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
#include "element.h"
#include "res.h"
int element::getwidth()
{
    return res::getinstance()->width(name);
//  return res::getinstance()->img[name]->getwidth();   在类image中为公有属性
}
int element::getheight()
{
    return res::getinstance()->height(name);
}
//在资源文件中封装了移动过程,调用资源中的函数即可---画角色
void element::drawelement(int pre)
{
    res::getinstance()->drawrole(point.getx(), point.gety(), name, pre);
}
//点的移动
void element::moveelement(int speed, point::dir dir)
{
    point.move(speed, dir);
}
element::~element()
{
}
element::element()
{
 
}
//类的组合必须采用初始化参数列表
element::element(int x, int y, string name, bool live, int hp):point(x,y),name(name)
{
    this->live = live;
    this->hp = hp;
}
int& element::getx()
{
    // todo: 在此处插入 return 语句
    return point.getx();    //用point存放这个点,获取这个点应返回point里面的x坐标
}
int& element::gety()
{
    // todo: 在此处插入 return 语句
    return point.gety();
}
bool& element::getlive()
{
    // todo: 在此处插入 return 语句
    return live;
}
int& element::gethp()
{
    // todo: 在此处插入 return 语句
    return hp;
}

role.h - - - 角色移动、角色发射子弹

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#pragma once
#include "common.h"
class element;
class role
{
public:
    role();
    ~role();
    void drawplane(int preindex);    //第几帧
    void moveplane(int speed);       //速度
    void drawbullet();               //画子弹
    void movebullet(int speed);      //移动子弹---移动速度
    element*& getplane();            //外部做按键操作,需要访问飞机和子弹
    list<element*>& getbullet(); 
 
protected:
    element* plane;         //角色---用元素实例化一个角色---角色也是元素之一
    list<element*> bullet;    //子弹---一个飞机有多个子弹(包含多个元素的对象)子弹也是元素
};

c++使用Easyx图形库实现飞机大战

c++使用Easyx图形库实现飞机大战

role.cpp

?
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
#include "role.h"
#include "res.h"
#include "element.h"
#include "timer.hpp"
role::role()            //new一个元素类即可---把飞机放窗口正中间
{
    plane = new element(
        res::getinstance()->width("背景") / 2 - res::getinstance()->width("角色") / 2  //x
        , res::getinstance()->height("背景") - res::getinstance()->height("角色"),      //y
        "角色",   //name
        true,   //live
        100);   //hp
}
role::~role()
{
 
}
void role::drawplane(int preindex)    //画飞机
{
    plane->drawelement(preindex);
}
void role::moveplane(int speed)       //移动飞机---结合按键控制   异步处理的按键操作
{
    if (getasynckeystate(vk_up) && plane->gety() >= 0) //往上走y不能超过上边界
    {
        plane->moveelement(speed, point::up);          //改变飞机的点---移动元素
    }
                                                       //往下走<=(背景高度-角色高度)
    if (getasynckeystate(vk_down) &&                  
        plane->gety()<=res::getinstance()->height("背景")-res::getinstance()->height("角色"))
    {
        plane->moveelement(speed, point::down);
    }
                                                       //往右走<=(背景宽度-角色宽度)
    if (getasynckeystate(vk_right) &&
        plane->getx() <= res::getinstance()->width("背景") - res::getinstance()->width("角色"))
    {
        plane->moveelement(speed, point::right);
    }
                                                        //往左走x不能小于左边界
    if (getasynckeystate(vk_left) && plane->getx() >= 0)
    {
        plane->moveelement(speed, point::left);
    }
    //子弹产生---按空格发射子弹---用定时器控制速度---100毫秒产生1颗子弹
    if (getasynckeystate(vk_space)&&mytimer::timer(100,0))   
    {
//添加音乐  调用windows中的创建线程函数---函数指针 传入线程处理函数---播放第一个音乐
        handle threadid = createthread(null, 0, res::playmusic, (int*)1, 0, 0);
        bullet.push_back(new element(plane->getx() + 45, plane->gety() - 10, "子弹", 1));    //尾插法 按一下空格new一个子弹    子弹的坐标在飞机坐标的正上方的中间
        closehandle(threadid);    //通过返回值关闭线程
    }
    movebullet(1);    //移动子弹
    drawbullet();     //画子弹
}
void role::drawbullet()    //子弹存在容器中,每颗子弹都要画出来
{
    for (auto v : bullet)  //迭代器遍历
    {
        if (v->getlive())      //判断子弹能否画出来
        {
            v->drawelement(0); //序号0,子弹只有2张
        }
    }
}
void role::movebullet(int speed)    //每颗子弹都要移动---从下往上走
{
    for (auto v : bullet)     
    {
        v->gety() -= speed;
    }
}
element*& role::getplane()
{
    // todo: 在此处插入 return 语句
    return plane;
}
list<element*>& role::getbullet()
{
    // todo: 在此处插入 return 语句
    return bullet;
}
//每产生一个子弹就播放音乐,返回值为handle类型

control.h         控制整个游戏的运行 - - - 中驱

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#pragma once
class graph;
class role;
class enemy;
class control
{
public:
    control(graph* pmap = nullptr, role* prole = nullptr,enemy* penemy=nullptr);
    ~control();
    void drawmap();
    void drawrole();
    void drawenemy();    //画敌机
    void playeemey();    //打敌机
 
protected:
    //所有组成部分
    role* prole;    //角色
    graph* pmap;    //地图
    enemy* penemy;  //敌机
};

control.cpp - - - 封装实现细节 - - - 主函数中调用控制类对象即可

c++使用Easyx图形库实现飞机大战

?
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
#include "control.h"
#include "role.h"
#include "graph.h"    //地图
#include "timer.hpp"
#include "enemy.h"
#include "element.h"
#include "res.h"
 
control::control(graph* pmap, role* prole, enemy* penemy )
{
    this->pmap = pmap;
    this->prole = prole;
    this->penemy = penemy;
}
control::~control()
{
 
}
void control::drawmap()
{
    pmap->drawmap();
}
 
void control::drawrole()
{
    prole->drawplane(0);    //第0帧
    prole->moveplane(1);    //速度
}
//每一秒产生一个敌机(产生不能过于频繁)---产生一个敌机就把它放到容器中
void control::drawenemy()
{
    if (mytimer::timer(1000, 1))
    {
        penemy->getenemy().push_back(penemy->createenemy());
    }
    if (mytimer::timer(10, 2))   //十毫秒移动一个飞机   2号定时器
    {
        penemy->moveenemy(1);    //速度
    }
    penemy->drawenemy(0);        //只画1种敌机
}
 
void control::playeemey()        //矩形与矩形的判断
{
    //1.碰撞处理: 碰到子弹,把子弹的live置为0---只处理标记
    for (auto bullet : prole->getbullet()) //获取子弹的信息
    {
        if (bullet->getlive() == 0)        //如果子弹标记==0继续往下找
            continue;
        if (bullet->gety() < 0)            //如果超出窗口边界,把标记置为false
            bullet->getlive() = false;
        for (auto enemy : penemy->getenemy()) //与子弹的碰撞处理 大前提---敌机存在
        {
            if(enemy->getlive()&&
                bullet->getx()>enemy->getx()&&
                bullet->getx()<enemy->getx()+res::width("敌机")&&
                bullet->gety() > enemy->gety() &&
                bullet->gety() < enemy->gety() + res::height("敌机"))
            {
                enemy->gethp()--;            //相交,血量减少
                if (enemy->gethp() <= 0)
                {
                    enemy->getlive() = false; //把敌机标记置为false---不做消失处理
                }
                bullet->getlive() = false;    //碰到敌机后子弹要消失  
            }
            //敌机出了窗口边界要消失
            if (enemy->gety() >= res::height("背景"))
            {
                enemy->getlive() = false;
            }
        }
    }
    //2.通过标记去删除相应的数据--->内存管理    遍历敌机
    for (auto itere = penemy->getenemy().begin(); itere != penemy->getenemy().end();)
    {
        if ((*itere)->getlive() == false)     //当前敌机标记为false--->删除敌机
        {
            itere = penemy->getenemy().erase(itere);
        }
        else
        {
            itere++;        //++不要写在for循环中
        }
    }
//遍历子弹---子弹删除
    for (auto iterb = prole->getbullet().begin(); iterb != prole->getbullet().end();)
    {
        if ((*iterb)->getlive() == false)
        {
            iterb = prole->getbullet().erase(iterb);
        }
        else
        {
            iterb++;
        }
    }
}

graph.h - - - 地图(窗口类)

?
1
2
3
4
5
6
7
8
#pragma once
class graph
{
public:
    graph();
    ~graph();
    void drawmap();
};

graph.cpp

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "graph.h"
#include "res.h"
graph::graph()
{
    initgraph(res::getinstance()->img["背景"]->getwidth(),
        res::getinstance()->img["背景"]->getheight());
    mcisendstring("open ./res/game_music.mp3", 0, 0, 0); //在窗口创建出来后添加音乐
    mcisendstring("play ./res/game_music.mp3 repeat", 0, 0, 0); //重复播放
}
 
graph::~graph()
{
    closegraph();    //对象没了就关闭窗口
}
 
void graph::drawmap()
{
    res::drawimg(0, 0, "背景");
}

time.hpp - - - 定义和实现写一起,用hpp做结尾      用时间控制子弹的发射 - - - 定时器

c++使用Easyx图形库实现飞机大战

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#pragma once
#include "common.h"
class mytimer
{
public:
    static bool timer(int duration, int id)     //时间间隔    id
    {
        static int starttime[10];               //开始时间---静态变量自动初始化为0
        int endtime = clock();                  //结束时间
        if (endtime - starttime[id] >= duration)//结束时间-开始时间>=时间间隔
        {
            starttime[id] = endtime;            //把原来的结束时间改为新的开始时间
            return true;
        }
        return false;
    }
 
};

enemy.h        敌机

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#pragma once
#include "common.h"
class element;
class enemy
{
public:
    enemy();
    ~enemy();
    void drawenemy(int preindex);  //画第几张
    void moveenemy(int speed);
    element* createenemy();        //创建敌机
    list<element*>& getenemy();    //访问敌机---需要做碰撞检测
protected:
    list<element*> enemyplane;     //(存储)多个敌机
};

enemy.cpp

?
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
#include "enemy.h"
#include "element.h"
#include "res.h"
enemy::enemy()
{
 
}
enemy::~enemy()
{
 
}
void enemy::drawenemy(int preindex)
{
    for (auto v : enemyplane) //画元素
    {
        if (v->getlive())     //判断敌机是否存在
        {
            v->drawelement(preindex);
        }
    }
}
void enemy::moveenemy(int speed)
{
    for (auto v : enemyplane)
    {
        v->moveelement(speed, point::down);    //速度    方向
    }
}
element* enemy::createenemy()  //获取窗口宽高---随机x,y坐标 从窗口上边界出来 速度 血量
{
    return new element(rand()%res::getinstance()->width("背景"),
        -1*res::getinstance()->height("敌机"),"敌机",1, 3);
}
list<element*>& enemy::getenemy()
{
    // todo: 在此处插入 return 语句
    return enemyplane;    //返回一个容器
}

采用多线程的方式播放音乐

c++使用Easyx图形库实现飞机大战

到此这篇关于c++使用easyx图形库实现飞机大战的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/weixin_60569662/article/details/122138393

延伸 · 阅读

精彩推荐