射击游戏(转载自https://www.jb51.net/article/170405.htm)

#include <easyx.h>
#include <time.h>
#include <conio.h>

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 <= 40)
            angle = false;
    }

    void hurt() { // 受伤
        hp -= 4;
        d_hurt = clock();
        setfillcolor(0);
        setlinecolor(WHITE);
        fillrectangle(160, 485, 560, 510); // 更新血条
        rectangle(160, 485, 160 + hp * 4, 510);
        setfillcolor(RGB(230, 0, 1));
        setlinecolor(RGB(255, 255, 255));
        fillrectangle(160, 485, 160 + hp * 4, 510);
        rectangle(160, 485, 160 + hp * 4, 510);
        hurting = true;
        if (hp <= 0) // 死亡
            wined = true;
    }
};

class Tank { // 玩家类,同上
public:
    bool hurting = false;
    int hp = 100;
    int x;
    COLORREF clr = RGB(150, 180, 210);
    int y;
    clock_t d_hurt;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值