JavaScript
主页 > 网络编程 > JavaScript >

c++之使用easyx做出大飞机游戏

2022-08-20 | 佚名 | 点击:

效果图

这个打飞机小游戏素材都很一般,直接网上抠图下来的。

但我们应该学习一下怎么入门这一款经典小游戏。

游戏对象

首先游戏对象就这几个东西

1

2

3

4

5

6

// 全局画板

IMAGE bk;

IMAGE BK;

IMAGE Plane;

IMAGE Diren;

IMAGE Zidan;

这个游戏用到游戏插件easyX,我们想载入这几个图片。

1

2

3

4

5

6

7

8

9

//预加载资源,需要加载了之后才能用

void loadRes()

{

    loadimage(&bk, _T("res\\bg.png"));

    loadimage(&BK, _T("res\\bg.png"));

    loadimage(&Plane, _T("res\\plane.png"));

    loadimage(&Diren, _T("res\\diren.png"));

    loadimage(&Zidan, _T("res\\zidan.png"));

}

子弹和敌人的结构体先设计好,其实就是用来控制他们的位置的。

1

2

3

4

5

6

7

8

9

10

struct ZIDAN

{

    int x;

    int y;

};

struct DIREN

{

    int x;

    int y;

};

子弹和敌人是否碰撞,这里需要写碰撞检测,其实就是几点两点之间的距离就可以,这是最经典的碰撞算法。

1

2

3

4

5

6

7

8

9

bool isPeng(int x2,int y2,int x1,int y1)

{

    int result=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);

    if(result<2500)

    {

        return true;

    }

    return false;

}

子弹与敌人碰撞之后,敌人就毁灭。这里面只需要把敌人移出屏幕就可以,因为子弹和敌人都是可以重复利用的,所以最好做一个对象池,可以重复用上。

1

2

3

4

5

6

7

8

9

10

11

//判断子弹和飞机是否相撞

    for(i=0;i<8;i++)

    {

        for(int j=0;j<5;j++)

        {

            if(isPeng(zidans[j].x,zidans[j].y,direns[i].x+25,direns[i].y+15))

            {

                direns[i].y = -100;

            }

        }

    }

需要wsad进行控制摇杆,飞机飞行

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

if (_kbhit())

{

    char ch = _getch();

    if (ch == 'w')

    {

        planeY-=5;

    }

    if(ch == 's')

    {

        planeY+=5;

    }

    if(ch == 'a')

    {

        planeX-=5;

    }

    if(ch == 'd')

    {

        planeX+=5;

    }

}

原文链接:https://juejin.cn/post/7130890872136138788
相关文章
最新更新