坦克 大战

#include "Menu.h"
#include <windows.h>
#include "Game.h"
//显示欢迎界面
void Menu::Show_Welcome()
{
    Map::Colors(白色);
    Map::WriteChar(2,8,"I -         2600              HI -             20000");
    Map::Colors(紫色);
    Map::WriteChar(5, 10,   "▓▓▓▓▓        ▓       ▓▓▓▓▓  ▓▓▓▓▓  ▓        ▓▓▓▓▓");
    Sleep(100);
    Map::WriteChar(6, 10,   "▓       ▓     ▓  ▓         ▓          ▓      ▓        ▓        ");
    Sleep(100);
    Map::WriteChar(7, 10,   "▓       ▓    ▓    ▓        ▓          ▓      ▓        ▓        ");
    Sleep(100);
    Map::WriteChar(8, 10,   "▓▓▓▓▓    ▓      ▓       ▓          ▓      ▓        ▓▓▓▓▓");
    Sleep(100);
    Map::WriteChar(9, 10,   "▓       ▓  ▓▓▓▓▓▓      ▓          ▓      ▓        ▓        ");
    Sleep(100);
    Map::WriteChar(10, 10,  "▓       ▓  ▓        ▓      ▓          ▓      ▓        ▓        ");
    Sleep(100);
    Map::WriteChar(11, 10,  "▓▓▓▓▓   ▓        ▓      ▓          ▓      ▓▓▓▓  ▓▓▓▓▓");
    Sleep(100);
    Map::WriteChar(13, 14,  "    ▓▓▓▓       ▓▓▓▓▓   ▓▓▓▓▓    ▓      ▓ ");
    Sleep(100);
    Map::WriteChar(14, 14,  "  ▓▓      ▓         ▓           ▓        ▓      ▓ ");
    Sleep(100);
    Map::WriteChar(15, 14,  " ▓▓                  ▓           ▓         ▓    ▓  ");
    Sleep(100);
    Map::WriteChar(16, 14,  "▓                     ▓           ▓          ▓▓▓   ");
    Sleep(100);
    Map::WriteChar(17, 14,  " ▓▓                  ▓           ▓            ▓     ");
    Sleep(100);
    Map::WriteChar(18, 14,  "  ▓▓      ▓         ▓           ▓            ▓     ");
    Sleep(100);
    Map::WriteChar(19, 14,  "    ▓▓▓▓       ▓▓▓▓▓       ▓            ▓     ");
    Sleep(200);
}
//静态菜单信息
void Menu::Show_Menu()
{
    Map::Colors(白色);    
    Map::WriteChar(38,22, "@  1990  1980  NAMCO  LTD.");
    Map::WriteChar(39,24, "ALL RIGHTS  RESERVED");
}
//自定义地图赋值及打印
int tool::Init_Map[41][41];
void tool::Init_Info()
{
    int i,j;
    for (i = 0; i < HIGHT; i++)
    {
        for (j = 0; j < WIDTH; j++)
        {
            Init_Map[41][41];


            //初始化边框
            if (Init_Map[i][j] == 边框)
            {
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN
                    | FOREGROUND_RED | FOREGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_BLUE);
                Map::WriteChar(i, j, "■");
            }
            
            if (Init_Map[i][j] == 黄土墙)//被击中变成红土墙
            {
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_RED | BACKGROUND_GREEN | BACKGROUND_RED);
                Map::WriteChar(i, j, "▓");
            }
            if (Init_Map[i][j] == 红土墙)
            {
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED | BACKGROUND_GREEN | BACKGROUND_RED);
                Map::WriteChar(i, j, "▓");
            }
            if (Init_Map[i][j] == 水流)
            {
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | BACKGROUND_BLUE | FOREGROUND_BLUE | FOREGROUND_GREEN);
                Map::WriteChar(i, j, "~");
            }
            if (Init_Map[i][j] == 钢墙)
            {
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN| FOREGROUND_RED | FOREGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_BLUE);
                Map::WriteChar(i, j, "■");
            }
            if (Init_Map[i][j] == 老家)
            {
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_GREEN);
                Map::Map::WriteChar(37, 19, "◣★◢");
                Map::WriteChar(38, 19, "███");
                Map::WriteChar(39, 19, "◢█◣");

            }
            Map::Colors(黑色);
        }
    }
}
void tool::menu()
{
    COORD Coord[3];
    const char mu[3][16] =
    {
        "               ",
         "1  PLAYER",
         "2  PLAYERS",
    };
    for (int i = 0; i < 3; i++)
    {
        Coord[i].Y = 24;
    }
    Coord[0].X = 23;
    Coord[1].X = 25;
    Coord[2].X = 27;

    for (int i = 0; i < 3; i++)
    {
        Map::WriteChar(Coord[i].X, Coord[i].Y, mu[i]);
    }
    Map::Colors(绿色);
    Map::WriteChar(Coord[1].X, Coord[1].Y, mu[1]);
    int Select = 1;
    while (1)
    {
        if (GetAsyncKeyState('W') & 0x8001)
        {
            Map::Colors(白色);
            Map::WriteChar(Coord[Select].X, Coord[Select].Y, mu[Select]);
            if (Select == 1)
                Select = 2;
            else
                Select--;
            Map::Colors(绿色);
            Map::WriteChar(Coord[Select].X, Coord[Select].Y, mu[Select]);
        }
        else if (GetAsyncKeyState('S') & 0x8001)
        {
            Map::Colors(白色);
            Map::WriteChar(Coord[Select].X, Coord[Select].Y, mu[Select]);
            if (Select == 2)
                Select = 1;
            else
                Select++;
            Map::Colors(绿色);
            Map::WriteChar(Coord[Select].X, Coord[Select].Y, mu[Select]);
        }
        else if (GetAsyncKeyState(0xD) & 0x8001)//回车键
        {
            switch (Select)
            {
            case 1:Game::play = 1; break;
            case 2:Game::play = 2; break;
            default:
                break;
            }
            return;
        }
        Sleep(200);
    }
}
#include "Tank.h"
#include "Map.h"
#pragma comment(lib,"winmm.lib")
//坦克mode模型的三维数组
const char* tank_figure[6][3][4] =
{
    {
    {"█┃ █", "█┳ █", "███", "███"},
    {"█●█", "█●█", "━ ●┫", "┣ ●━"},
    {"█┻ █", "█┃ █", "███", "███"}
  },
  {
    {"◢┃ ◣", "◢━ ◣", "◢┳ ◣", "◢┳ ◣"},
    {"┣ ●┫", "┣ ●┫", "━ ●┃", "┃ ●━"},
    {"◥━ ◤", "◥┃ ◤", "◥┻ ◤", "◥┻ ◤"}
  },
  {
    {"┏ ┃ ┓", "┏ ┳ ┓", "┏ ┳ ┓", "┏ ┳ ┓"},
    {"┣ ●┫", "┣ ●┫", "━ ●┫", "┣ ●━"},
    {"┗ ┻ ┛", "┗ ┃ ┛", "┗ ┻ ┛", "┗ ┻ ┛"}
  },
  {
    {"┏ ┃ ┓", "◢━ ◣", "┏ ┳ ◣", "◢┳ ┓"},
    {"┣ ●┫", "┣ ●┫", "━ ●┃", "┃ ●━"},
    {"◥━ ◤", "┗ ┃ ┛", "┗ ┻ ◤", "◥┻ ┛"}
  },
  {
    {"╔ ┃ ╗", "╔ ╦ ╗", "╔ ╦ ╗", "╔ ╦ ╗"},
    {"╠ █╣", "╠ █╣", "━ █╣", "╠ █━"},
    {"╚ ╩ ╝", "╚ ┃ ╝", "╚ ╩ ╝", "╚ ╩ ╝"}
  },
 {
    {"  ■  ", "■  ■", "  ■■", "■■  "},
    {"■●■", "■●■", "■●  ", "  ●■"},
    {"■  ■", "  ▉  ", "  ■■", "■■  "}
 }
};
//坦克构造
Tank::Tank(COORD pos, DIR dir,int mode,int team)
{
    //坐标
    this->pos = pos;
    //方向
    this->dir = dir;
    //图案模型
    this->mode = mode;
    //阵营
    this->team = team;
    //时间坦克移动
    this->CD = 150;
    this->end_time = clock();
    this->music = clock();
    //坦克分数
    this->score = 0;
    //打印坦克
    Show_Tank();
}
//打印坦克
void Tank::Show_Tank()
{
    const char* (*tankModel)[4] = tank_figure[mode];
    for (int i = 0; i < 3; i++)
    {
        //敌方坦克需要彩色,来区别
        if (team == 3)
        {
            Map::Colors(rand()%7+2);
        }
        else
        {
            Map::Colors(紫色);
        }
        Map::WriteChar(pos.X-1+i,pos.Y-1,tankModel[i][dir]);
        Map::dynamic[pos.X - 1 + i][pos.Y - 1] = 坦克;
        Map::dynamic[pos.X - 1 + i][pos.Y ] = 坦克;
        Map::dynamic[pos.X - 1 + i][pos.Y+1] = 坦克;

    }
}
//清理坦克
void Tank::Clear_Tank()
{
    for (int i = 0; i < 3; i++)
    {        
        Map::WriteChar(pos.X - 1 + i, pos.Y - 1, "      ");
        Map::dynamic[pos.X - 1 + i][pos.Y - 1] = 空地;
        Map::dynamic[pos.X - 1 + i][pos.Y] = 空地;
        Map::dynamic[pos.X - 1 + i][pos.Y + 1] = 空地;
    }
}
//坦克移动
void Tank::Move_Tank(DIR dir)
{
    //移动坦克之前检查CD是否冷却
    if (Check_End_Time())
    {
        //清理坦克
        Clear_Tank();
        //检查方向是否一致
        if (this->dir != dir)
        {
            this->dir = dir;
            Show_Tank();
            return;
        }
        else
        {
            //检查是否可以移动
            if (if_Move_Tank())
            {
                switch (dir)
                {
                case 上:pos.X--;
                    break;
                case 下:pos.X++;
                    break;
                case 左:pos.Y--;
                    break;
                case 右:pos.Y++;
                    break;
                default:
                    break;
                }
            }
        }
        Show_Tank();
    }
}
//坦克移动判断
bool Tank::if_Move_Tank()
{
    if (dir == 上)
    {
        return Collide_Check({ pos.X - 2,pos.Y - 1 }) && Collide_Check({ pos.X - 2,pos.Y }) && Collide_Check({pos.X-2,pos.Y+1});
    }
    if (dir == 下)
    {
        return Collide_Check({ pos.X + 2,pos.Y - 1 }) && Collide_Check({ pos.X + 2,pos.Y }) && Collide_Check({ pos.X + 2,pos.Y + 1 });
    }
    if (dir == 左)
    {
        return Collide_Check({ pos.X -1,pos.Y - 2 }) && Collide_Check({ pos.X  ,pos.Y -2}) && Collide_Check({ pos.X + 1,pos.Y - 2 });
    }
    if (dir == 右)
    {
        return Collide_Check({ pos.X + 1,pos.Y + 2 }) && Collide_Check({ pos.X,pos.Y + 2}) && Collide_Check({ pos.X - 1,pos.Y + 2 });
    }
    return false;
}
//辅助坦克移动判断检测
bool Tank::Collide_Check(COORD pos)
{
    if (Map::static_Map[pos.X][pos.Y] == 空地 && Map::dynamic[pos.X][pos.Y] != 坦克)
    {
        return true;
    }

    return false;
}
//CD检测
bool Tank::Check_End_Time()
{
    if (clock() - end_time > this->CD)
    {
        end_time = clock();
        return true;
    }
    return false;
}
//坦克开火
void Tank::Play_Bullet(std::vector<Bullet*>& bull)
{
    /*if (pos.X == 2)
    {
        return;
    }*/
    Sleep(100);
    //播放发子弹音乐D:\Project\TEST_ONE\Music
    Bullet* bullet = new Bullet(this->pos,this-> dir,this->team);
    bull.push_back(bullet);
        //PlaySoundA("D:\\Project\\TEST_ONE\\Music\\boom.wav", NULL, SND_ASYNC | SND_FILENAME);
}#include "Bullet.h"
#include "Game.h"
#include <conio.h>
#pragma comment(lib,"winmm.lib")
//如果子弹遇到水流后loc=1,
int loc;
//子弹类构造函数
Bullet::Bullet(COORD pos,DIR dir,int team)
{
    //子弹坐标初始化
    this->pos = pos;
    //子弹方向初始化
    this->dir = dir;
    //子弹存活
    this->alive = true;

    //子弹阵营
    this->team = team;

    //检查子弹赋值前的地方是否为边框

        if (dir == 上)
        {
            if (Map::static_Map[this->pos.X - 2][this->pos.Y] == 边框)
            {

                alive = false;
            }
        }
        else if (dir == 下)
        {
            if (Map::static_Map[this->pos.X + 2][this->pos.Y] == 边框)
            {

                alive = false;
            }
        }
        else if (dir == 左)
        {
            if (Map::static_Map[this->pos.X][this->pos.Y - 2] == 边框)
            {

                alive = false;

            }
        }
        else if (dir == 右)
        {
            if (Map::static_Map[this->pos.X][this->pos.Y + 2] == 边框)
            {

                alive = false;
            }
        }
        if (alive == true)
        {
            if (Gps_Bullet_1())
            {
                switch (dir)
                {
                case 上:this->pos.X -= 2;
                    break;
                case 下:this->pos.X += 2;
                    break;
                case 左:this->pos.Y -= 2;
                    break;
                case 右:this->pos.Y += 2;
                    break;
                default:
                    break;
                }
                //Show_bullet();
            }

        }
    
}
//子弹移动
void Bullet::Move()
{    //清理子弹
        Clear_Bullet();
        //检测能否移动
        if (Gps_Bullet())
        {//如果经过水流
            if (loc >= 1)
            {
                for (int x = 0; x < 41; x++)
                {
                    for (size_t i = 0; i < 41; i++)
                    {
                        if (Map::static_Map[x][i] == 水流)
                        {
                            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | BACKGROUND_BLUE | FOREGROUND_BLUE | FOREGROUND_GREEN);
                            Map::WriteChar(x, i, "~");
                        }
                    }
                }
                loc--;
            }
            switch (dir)
            {
            case 上:this->pos.X -= 1;
                break;
            case 下:this->pos.X += 1;
                break;
            case 左:this->pos.Y -= 1;
                break;
            case 右:this->pos.Y += 1;
                break;
            default:
                break;
            }
            Show_bullet();
        }
        else
        {
            alive = false;
        }
}
//子弹坐标设置
bool Bullet::Gps_Bullet_1()
{
    if (dir == 上)
    {
        return check({ this->pos.X - 2,this->pos.Y });
    }
    if (dir == 下)
    {
        return check({ this->pos.X + 2,this->pos.Y });
    }
    if (dir == 左)
    {
        return check({ this->pos.X ,this->pos.Y - 2 });
    }
    if (dir == 右)
    {
        return check({ this->pos.X ,this->pos.Y + 2 });
    }
    return false;
}
bool Bullet::Gps_Bullet()
{
    if (dir == 上)
    {
        return check({ this->pos.X - 1,this->pos.Y });
    }
    if (dir == 下)
    {
        return check({ this->pos.X + 1,this->pos.Y });
    }
    if (dir == 左)
    {
        return check({ this->pos.X ,this->pos.Y - 1 });
    }
    if (dir == 右)
    {
        return check({ this->pos.X ,this->pos.Y + 1 });
    }
    return false;
}
//碰撞检测
bool Bullet::check(COORD pos)
{
    播放发子弹音乐D:\Project\TEST_ONE\Music
    //PlaySoundA("D:\\Project\\TEST_ONE\\Music\\hit.wav", NULL, SND_ASYNC |SND_NODEFAULT);
    if (Map::static_Map[pos.X][pos.Y] == 老家)
    {
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_INTENSITY);
        Map::WriteChar(37, 19, "◢  ◣");
        Map::WriteChar(38, 19, "  █  ");
        Map::WriteChar(39, 19, "◥  ◤");
        Sleep(1000);
        //清理地图
        Map::Clear_Map();
        //改变标记
        this->Home = false;
        //将标记传入输赢检测
        vin(this->Home);
        //输出老家被毁
        Map::Clear_Map();
        Map::Colors(红色);
        Map::WriteChar(20, 20, "老家被毁");
        Sleep(1000);
        return false;
    }
    if (Map::static_Map[pos.X][pos.Y] == 边框)
    {
        return false;
    }
    if (Map::dynamic[pos.X][pos.Y] == 坦克)
    {    
        //由于判断体较长,所以封装陈函数
        Over_Tank({pos.X,pos.Y});
        //检测输入,看哪一方先死全
        vin();
        return false;
    }
    if (Map::dynamic[pos.X][pos.Y] == 子弹)
    {
        for (int x = 0; x < Game::bull.size(); x++)
        {
            if (Game::bull[x]->pos.X == pos.X && Game::bull[x]->pos.Y == pos.Y)
            {
                Game::bull[x]->Clear_Bullet();
                Game::bull[x]->alive = false;
            }
        }
        return false;
    }
    if (Map::static_Map[pos.X][pos.Y] == 空地 && Map::dynamic[pos.X][pos.Y] == 空地)
    {
        return true;
    }
    else if (Map::static_Map[pos.X][pos.Y] == 黄土墙)
    {
        Map::static_Map[pos.X][pos.Y] = 红土墙;
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED | BACKGROUND_GREEN | BACKGROUND_RED);
        Map:: WriteChar(pos.X, pos.Y, "▓");
        return false;
    }
    else if (Map::static_Map[pos.X][pos.Y] == 水流)
    {
        loc = 2;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值