turret

来源:互联网 发布:app生成器软件 编辑:程序博客网 时间:2024/06/06 03:27
#ifndef ENEMY_H#define ENEMY_H#include <cstdio>#include <windows.h>#include <iostream>#include <time.h>using namespace std;const int EMAX_Y = 32;class Enemy {public:    Enemy() {        //randPos();        this -> color = 3;        HP = 3;        des = true;    }    ~Enemy() {}    void randPos(int n)             //位置随机出现    {        srand(n | time(NULL));        position.X = rand() % 56;        if (position.X < 8)            position.X += 8;        if (position.X > 56)            position.X -= 2;        position.Y = 0;    }    void paint(){        setColor(color);        //gotoxy(position.X, position.Y);        cout << "▓";    }    void go(int n)    {        if (n/2 < EMAX_Y-1 && n % 2 )        {            position.Y++;            gotoxy(position.X, position.Y - 1);            cout << " ";            //销毁前一个坐标的图像            gotoxy(position.X, position.Y);            paint();        }        if (n / 2 == EMAX_Y)     //销毁        {            //gotoxy(position.X, position.Y + n/2 - 1);            //cout << " ";            destroy(position.X, position.Y);        }    }    void gotoxy(short x, short y) {        COORD pos;        pos.X = x;        pos.Y = y;        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);     //光标转移    }    void setColor(int color) {  //颜色函数,设置文本颜色        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);    //    }    void changeHealth(int damage) { HP -= damage;}    int getHealth() { return HP; }    void changeDes() { des = false; }    bool ifdestroy() { return des; }    void destroy(short x, short y) {        gotoxy(x, y);        cout << " ";        gotoxy(x, y);        des = true;    }    COORD getPosition() { return position; }private:    COORD position;    int HP;    int color;    //int speed;    bool des;};/**class BigBeast : public Enemy {public:    //BigBeast() {}    ~BigBeast() {}    void paint() {}};**/#endif //ENEMY_H
#ifndef TURRET_H#define TURRET_H#include <string>#include <windows.h>#include <iostream>/*    由于使用的符号是宽字符,故x坐标应是两个占位符*/const int TMAX_Y = 33;const int TLX = 6;const int TRX = 58;using namespace std;typedef struct TurretLevel{ //炮塔等级    int fire;               //一级塔炮弹威力1,范围25,速度1    int num;                //二级塔炮弹威力2,范围30,速度2    int range;    int speed;    int damage;}TurretLevel;           enum FireDir { L,R,T };                 //炮口方向class Turret {public:    Turret() {        level = { 1,1,26,1,1 };    }    Turret(COORD coord) {        level = { 1,1,26,1,1 };        //level.fire = 1;        //level.num = 1;        //level.range = 26;        //level.speed = 1;        desBall = false;        position.X = coord.X;        position.Y = coord.Y;        LRT(coord);        build();    }    ~Turret() {}    void LRT(COORD coord) {        //给fireDir(炮塔方向)赋值        position = coord;        if (position.X == TLX && position.Y < TMAX_Y)        {            fireDir = R;    //方向向右            ballPosition.X = coord.X + 4;            ballPosition.Y = coord.Y;            position.X--, position.X--;        }        else if (position.X == TRX && position.Y < TMAX_Y)        {            fireDir = L;    //方向向左            ballPosition.X = coord.X - 4;            ballPosition.Y = coord.Y;            position.X++, position.X++;        }        else        {            fireDir = T;    //方向向顶            ballPosition.X = coord.X;            ballPosition.Y = coord.Y - 2;            position.Y++;        }    }    void gotoxy(short x, short y) {        COORD pos;        pos.X = x;        pos.Y = y;        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);//光标转移    }    void setColor() {   //颜色函数,设置文本颜色        if (level.num == 1)            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 2);//green        else            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 4);//red    }    int getLevel() { return level.num; }    void levelUp() {        level = { 2,2,30,2,2 };        build();    }    void build() {              //绘制炮塔        setColor();             //设置炮塔颜色        switch (fireDir)        {        case L:            gotoxy(position.X, position.Y - 1);            cout << "■■";            gotoxy(position.X - 4, position.Y);            cout << "●" << "L" << level.num << "■";            gotoxy(position.X, position.Y + 1);            cout << "■■";            gotoxy(position.X, position.Y);            break;        case R:            gotoxy(position.X - 2, position.Y - 1);            cout << "■■";            gotoxy(position.X, position.Y);            cout << "■" << "L" << level.num << "●";            gotoxy(position.X - 2, position.Y + 1);            cout << "■■";            gotoxy(position.X, position.Y);            break;        case T:            gotoxy(position.X, position.Y - 2);            cout << "●";            gotoxy(position.X, position.Y - 1);            cout << "L" << level.num;            gotoxy(position.X - 2, position.Y);            cout << "■■■";            gotoxy(position.X - 2, position.Y + 1);            cout << "■  ■";            gotoxy(position.X, position.Y);            break;        default:            //std::cout << "paint turret error...\n";            break;        }    }    void fire(int n) {      //二营长!开炮!!!        setColor();         //设置炮弹颜色        switch (fireDir)        {        case L:            if (n > level.range / level.num)            {                //ballCurPos.X = ballPosition.X - level.speed*n + level.num * 1;                //ballCurPos.Y = ballPosition.Y;                destroyBall();            }            if (n <= level.range / level.num)            {                ballCurPos.X = ballPosition.X - level.speed*n;                ballCurPos.Y = ballPosition.Y;                  //获取炮弹位置                gotoxy(ballCurPos.X + 2, ballCurPos.Y);                cout << " ";                gotoxy(ballPosition.X - level.speed*n, ballPosition.Y);                cout << "●";            }            break;        case R:            if (n > level.range / level.num)            {                //ballCurPos.X = ballPosition.X + level.speed*n - level.speed * 1;                //ballCurPos.Y = ballPosition.Y;                destroyBall();            }            if (n < level.range / level.num)            {                ballCurPos.X = ballPosition.X + level.speed*n;                ballCurPos.Y = ballPosition.Y;                  //获取炮弹位置                gotoxy(ballCurPos.X - 2, ballCurPos.Y);                cout << " ";                gotoxy(ballPosition.X + level.speed*n, ballPosition.Y);                cout << "●";            }            break;        case T:            if (n > level.range / level.num)            {                //ballCurPos.X = ballPosition.X;                //ballCurPos.Y = ballPosition.Y - level.speed*n + level.speed * 1;                destroyBall();            }            if (n < level.range / level.num)            {                ballCurPos.X = ballPosition.X;                ballCurPos.Y = ballPosition.Y - level.speed*n;  //获取炮弹位置                gotoxy(ballCurPos.X, ballCurPos.Y + level.speed);                cout << " ";                gotoxy(ballPosition.X, ballPosition.Y - level.speed*n);                cout << "●";            }            break;        default:            //std::cout << "fire directiong error...\n";            break;        }    }    int getDamage() {        return level.damage;    }    bool ifdesBall() { return desBall; }    void unDesBall() { desBall = false; }    void delTurret() {}                 //移除炮塔    void destroyBall(){ //销毁炮弹        gotoxy(ballCurPos.X, ballCurPos.Y);        cout << " ";        gotoxy(ballCurPos.X, ballCurPos.Y);        desBall = true;    }           COORD getPosition() { return position; }                COORD getBallPos() {         return ballCurPos;    }           //获取当前炮弹坐标private:    COORD position;             //炮塔坐标    COORD ballPosition;         //炮弹坐标    COORD ballCurPos;           //炮弹当前坐标    TurretLevel level;          //炮塔等级    FireDir fireDir;            //炮口方向    bool desBall;               //炮弹是否销毁};#endif //TURRET_H
/**    *不要瞎想,那么多的静态属性和静态方法    *仅仅是我不想用EVENT的对象而已**/#ifndef EVENT_H#define EVENT_H#include "TURRET.h"#include "ENEMY.h"#include "PLAYER.h"const int MAX_RANGE = 33;const int MAX_Y = 33;const int RX = 58;const int LX = 6;class Event {public:    Event() {}    static void initial()    {        COORD static textPos = { RX + 11, 3 };        system("color 07");             //黑白        for (int i = 3; i < 35; i++)            cout << "      ■                                                  ■\n";        cout << "      ■■■■■■■■■■■■■■■■■■■■■■■■■■■\n";        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), textPos);        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 3);    //red        cout << "欢迎来到《我的炮塔》";        textPos.Y++;        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), textPos);        cout << "规则:建立炮塔阻击敌人!不要让超过30架敌军装甲车闯入你的领土";        textPos.Y++;        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), textPos);        cout << "WASD在城墙上移动,B键建造炮塔,L键升级炮塔";        textPos.Y++;        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), textPos);        cout << "二级炮塔比一级炮塔火力更凶猛,记得升级!";        textPos.Y++;        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), textPos);        cout << "闯入了 " << inEnemyCount << " 架装甲车";        textPos.Y++, textPos.Y++;        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), textPos);        cout << "你的国库有 " << Event::getMoney() << " 金币";        textPos.Y++;        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), textPos);        cout << "你已经坚持了 " << Event::getTime() << " 秒,死守这里!";    }    static void gotoxy(short x, short y) {        COORD pos;        pos.X = x;        pos.Y = y;        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos); //光标转移    }    static bool collide(Turret &t, Enemy &e) {                          //碰撞检测        COORD ballPos = t.getBallPos();        COORD enemyPos = e.getPosition();        if (ballPos.X == enemyPos.X || ballPos.X - 1 == enemyPos.X)        {            if (ballPos.Y == enemyPos.Y || ballPos.Y == enemyPos.Y + 1)            {                t.destroyBall();                e.changeHealth(t.getDamage());                if (e.getHealth() <= 0)                {                    money += 200;                    COORD epos = e.getPosition();                    e.destroy(epos.X,epos.Y);                }                return true;            }        }        return false;    }    static bool build() { return buildHere; }    static bool lvup() { return lvUp; }    static void unBuild() { buildHere = false; }    static void unLvup() { lvUp = false; }    static COORD getCursorPos() { return cursor; }    static void moveCursor(char &c) {                           //光标移动        //若不使用引用或指针,则当无输入时会持续上一个动作        //为避免此情况,将c设置为default的情况        gotoxy(cursor.X, cursor.Y);        switch (c)        {        case 'w':        case 'W':            if ( (cursor.X == LX || cursor.X == RX) && cursor.Y >= 1 )            {                gotoxy(cursor.X, --cursor.Y);                c = ' ';            }            break;        case 's':        case 'S':            if ( (cursor.X == LX || cursor.X == RX) && cursor.Y < MAX_Y - 1 )            {                gotoxy(cursor.X, ++cursor.Y);                c = ' ';            }            break;        case 'a':        case 'A':            if (cursor.Y == MAX_Y - 1 && cursor.X >= LX + 2)            {                gotoxy(--(--cursor.X), cursor.Y);                c = ' ';            }            break;        case 'd':        case 'D':            if (cursor.Y == MAX_Y - 1 && cursor.X <= RX - 2)            {                gotoxy(++(++cursor.X), cursor.Y);                c = ' ';            }            break;        case 'b':        case 'B':            if ((cursor.X == RX) || (cursor.X == LX))            {                                           //角落不能建造                if (cursor.Y != MAX_Y - 1)                {                    if (money - 200 < 0)                        ;                    else                     {                        money -= 200;                        buildHere = true;                    }                }            }            else            {                if (money - 200 < 0)                    ;                else                 {                    money -= 200;                    buildHere = true;                }            }            c = ' ';            break;        case 'l':        case 'L':            if ((cursor.X == RX) || (cursor.X == LX))            {                                           //角落不能建造                if (cursor.Y != MAX_Y - 1)                {                    if (money - 400 < 0)                        ;                    else                     {                        money -= 400;                        buildHere = true;                    }                }            }            else            {                if (money - 400 < 0)                    ;                else                {                    money -= 400;                    lvUp = true;                }            }            c = ' ';            break;        //cheat!!!!!        case 'm':        case 'M':            earnMoney(30000);            c = ' ';            break;        case 't':        case 'T':            addTime(5);            c = ' ';            break;        case 'z':        case 'Z':            inEnemyCount--;            c = ' ';            break;        default:            gotoxy(cursor.X, cursor.Y);            break;        }    }    static void changeInEnemyCount() { inEnemyCount++; }    static int getInEnemyCount() { return inEnemyCount; }    static void earnMoney(int m) { money += m; }    static int getMoney() { return Event::money; }    static double getTime() { return Event::liveTime; }    static void addTime(double t) { liveTime += t; }    static void changeShowMoney() {                     //改变显示的金币        gotoxy(RX + 11, 9);        cout << "你的国库有 " << Event::getMoney() << " 金币";    }    static void changeShowTime() {                      //改变显示的时间        gotoxy(RX + 11, 10);        cout << "你已经坚持了 " << Event::getTime() << " 秒,死守这里!";    }    static void changeShowEnemy() {        gotoxy(RX + 11, 7);        cout << "闯入了 " << inEnemyCount << " 架装甲车";    }private:    static int money;    static int HP;    static double liveTime;    static int inEnemyCount;    static COORD cursor;    static COORD textPos;    static bool buildHere;    static bool lvUp;};int Event::inEnemyCount = 0;int Event::money = 2000;double Event::liveTime = 0.0;COORD Event::cursor = { (LX + LX + RX) / 2 - 1, MAX_Y - 1 };bool Event::buildHere = false;bool Event::lvUp = false;#endif //EVENT_H
#include <iostream>#include <windows.h>    #include <conio.h>      //kbhit()函数的头文件#include "TURRET.h"#include "ENEMY.h"#include "EVENT.h"using namespace std;int main(){    Event::initial();    Turret *t = new Turret[100];    Enemy *e = new Enemy[300];    COORD cursor;    //COORD enemyPos;    COORD tt = { LX,18 };    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), tt);    char c = ' ';   //光标移动    COORD static commandPos = { RX + 4, MAX_Y };    //移动光标命令时在此处显示命令    int ball = 0;                                   //enemy走一次,炮弹发两发    int i = 0;                                      //炮塔数量,按建造增多    int ec = 1;                                     //enemy数量,按照炮塔数量逐渐增多    for (int n = 0; n < MAX_RANGE * 2; n++)    {        Sleep(150);        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 3);                    Event::addTime(0.15);        Event::changeShowTime();        Event::changeShowMoney();        Event::changeShowEnemy();        //cout << "现在有" << ec << "架敌装甲车";        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), commandPos);  //此处接收命令输入        if (kbhit())                                                            //若有输入,无回显地输入值            c = getche();        Event::moveCursor(c);        if(i<99)        {            if (Event::build())            {                       //建造炮塔                //Event::gotoxy()                t[i].LRT(Event::getCursorPos());                t[i].build();                Event::unBuild();                i++;            }            if (Event::lvup())            {                       //若等级为1则升级                if (t[i].getLevel() == 1)                {                    t[i].LRT(Event::getCursorPos());                    t[i].levelUp();                }                Event::unLvup();            }            for (int tn = 0; tn <= i; tn++)            {                       //每个炮塔都发射炮弹                     for (int en = 0; en < ec; en++)                    {                        if ( !Event::collide(t[tn],e[en]) && t[tn].ifdesBall() )                        {                            if (n < MAX_RANGE)                                ball = n;                            else                                ball = n - MAX_RANGE;                            t[tn].fire(ball);                            cursor = Event::getCursorPos();                            Event::gotoxy(cursor.X, cursor.Y);  //光标回到城墙                        }                        else                        {                             if (n == MAX_RANGE)                                t[tn].unDesBall();                        }                    }            }        }        for (int en = 0; en < ec; en++)        {   //每个enemy移动            //enemyPos = e[en].getPosition();            if (e[en].ifdestroy())            {                e[en].randPos(en * 432 + 3432 / ec * 3 - 1223);                e[en].changeDes();            }            else            {                e[en].go(n);                COORD in = e[en].getPosition();                if (in.Y == 31)                {                    Event::changeInEnemyCount();                }                cursor = Event::getCursorPos();                Event::gotoxy(cursor.X, cursor.Y);      //光标回到城墙            }        }        //根据炮塔数量增加敌人        if (i < 6)            ec = 1;        else if (i < 13)            ec = 2 + 2*(i%2);        else if (i < 20)            ec = i - 5;        else            ec = i - 4;        //循环        if (n == MAX_RANGE * 2 - 1)            n = 0;    }    delete[]t;    delete[]e;    std::cin.get();    return 0;}
原创粉丝点击