转载于:https://www.jb51.net/article/170405.htm
这篇文章主要为大家详细介绍了C++实现简单射击小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
使用c++制作简单的横板射击小游戏,供大家参考,具体内容如下
#include
#include
#include
class Bullet;
class Tank;
class E_Bullet;
class Boss;
bool dead = false;
bool wined = false;
struct pos//坐标类
{
int a;
int b;
};
class E_Bullet//敌人打出的子弹
{
public:
clock_t d;
int x;
int y;
bool on = false;
pos show()//画出新的位置
{
setfillcolor(RGB(255, 180, 20));
fillrectangle(x - 5, y - 5, x + 5, y + 5);
return pos{ x,y };
}
pos del()//覆盖原来的位置
{
setfillcolor(0);
setlinecolor(0);
fillrectangle(x - 5, y - 5, x + 5, y + 5);
rectangle(x - 5, y - 5, x + 5, y + 5);
return pos{ x,y };
}
pos move()//左移
{
x -= 3;
return pos{ x,y };
}
};
class Bullet//玩家打出的子弹,同上
{
public:
clock_t d;
int x;
int y;
bool on = false;
pos show()
{
setfillcolor(RGB(150, 180, 210));
fillrectangle(x - 5, y - 5, x + 5, y + 5);
return pos{ x,y };
}
pos del()
{
setfillcolor(0);
setlinecolor(0);
fillrectangle(x - 5, y - 5, x + 5, y + 5);
rectangle(x - 5, y - 5, x + 5, y + 5);
return pos{ x,y };
}
pos move()//右移
{
x += 3;
return pos{ x,y };
}
};
class Boss//敌人
{
public:
bool hurting = false;
clock_t d_hurt;
COLORREF clr = RGB(0, 130, 125);
int x;
int y;
int hp = 100;//生命
clock_t d;//判断举例上一次执行某一函数过了多久
clock_t att_d;
bool angle = false;//方向
pos show()
{
setfillcolor(clr);
fillrectangle(x - 20, y - 40, x + 20, y + 40);
return pos{ x,y };
}
pos del()
{
setfillcolor(0);
setlinecolor(0);
rectangle(x - 20, y - 40, x + 20, y + 40);
fillrectangle(x - 20, y - 40, x + 20, y + 40);
return pos{ x,y };
}
void fire(E_Bullet& but)//攻击
{
but.on = true;//放置一个子弹
but.x = x - 20;
but.y = y;
but.d = clock();
}
void move()//上上下下得移动
{
if (angle == true)
y -= 5;
if (angle == false)
y += 5;
if (y >= 440)
angle = true;
if (y 28 && (clock() - delay) >= 40)
{
tk.del(); tk.y -= 3; tk.show(); delay = clock();
}
}
if (GetAsyncKeyState('k') & 0x8000)//玩家开火
{
for (int i = 0; i < BT_MAX; i++)
{
if (bt[i].on == false && (clock() - d_f) > 800)
{
bt[i].on = true;
tk.fire(bt[i]);
d_f = clock();
break;
}
}
}
if (GetAsyncKeyState('K') & 0x8000)//玩家开火
{
for (int i = 0; i < BT_MAX; i++)
{
if (bt[i].on == false && (clock() - d_f) > 800)
{
tk.fire(bt[i]);
d_f = clock();
break;
}
}
}
if (GetAsyncKeyState('S') & 0x8000)//玩家移动
{
if (tk.y < 452 && (clock() - delay) >= 40)
{
tk.del(); tk.y += 3; tk.show(); delay = clock();
}
}
if (GetAsyncKeyState('s') & 0x8000)//玩家移动
if (tk.y < 452 && (clock() - delay) >= 40)
{
tk.del(); tk.y += 3; tk.show(); delay = clock();
}
for (int i = 0; i < BT_MAX; i++)//遍历子弹,使子弹刷新
{
if (bt[i].on == true && (clock() - bt[i].d) > 20)
{
bt[i].del();
bt[i].move();
bt[i].show();
bt[i].d = clock();
if (bt[i].x >= 635)
bt[i].on = false, bt[i].del();//到达了屏幕最右端
if ((bt[i].x + 5 >= bo.x - 20 && bt[i].x - 5 bo.y - 40))
//击中敌人
bt[i].on = false, bo.hurt(), bt[i].del();
}
}
if (clock() - bo.att_d > 700)//敌人自动开火
{
for (int i = 0; i < BT_MAX; i++)
{
if (ebt[i].on == false)
{
bo.fire(ebt[i]); break;
}
}
bo.att_d = clock();
}
for (int i = 0; i < BT_MAX; i++)//敌人子弹刷新,同上
{
if (ebt[i].on == true && (clock() - ebt[i].d > 20))
{
ebt[i].del();
ebt[i].move();
ebt[i].show();
ebt[i].d = clock();
if (ebt[i].x < 5)
ebt[i].del(), ebt[i].on = false;
if (ebt[i].x - 5 < tk.x + 25 && ebt[i].x + 5 > tk.x - 25 && ebt[i].y - 5 < tk.y + 25 && ebt[i].y + 5 > tk.y - 25)
{
ebt[i].on = false, tk.hurt(), ebt[i].del();
}
}
}
if (tk.hurting == true)//玩家受伤闪烁0.1秒
if (clock() - tk.d_hurt > 100)
{
tk.clr = RGB(150, 180, 210), tk.show(), tk.hurting = false;
}
else
tk.clr = RGB(255, 0, 0), tk.show();
if (bo.hurting == true)//敌人受伤闪烁0.1秒
if (clock() - bo.d_hurt > 100)
{
bo.clr = RGB(0, 130, 125), bo.show(), bo.hurting = false;
}
else
bo.clr = RGB(0, 255, 0), bo.show();
if (clock() - bo.d > 50)//敌人移动延时;
bo.del(), bo.move(), bo.show(), bo.d = clock();
}
if (wined)//胜负已分
{
settextcolor(RGB(0, 254, 0));
settextstyle(35, 0, _T("黑体"));
outtextxy(150, 200, _T("你打败了boss!你赢了!!"));
}
else
{
settextcolor(RGB(254, 0, 0));
settextstyle(35, 0, _T("黑体"));
outtextxy(140, 200, _T("你被boss打败了!"));
}
Sleep(5000);
closegraph();
return 0;
}
|