用c++ 写会移动的坦克

#include <iostream>

#include <windows.h>

#include <conio.h>

#include "asd.h"

using namespace std;

char tank_up[10][10] = {

    {'.','#','.','.','.','.','.','.','.','.'},

    {'#','#','#','.','.','.','.','.','.','.'},

    {'#','#','#','.','.','.','.','.','.','.'},

    {'#','#','#','.','.','.','.','.','.','.'},

    {'.','.','.','.','.','.','.','.','.','.'},

    {'.','.','.','.','.','.','.','.','.','.'},

    {'.','.','.','.','.','.','.','.','.','.'},

    {'.','.','.','.','.','.','.','.','.','.'},

    {'.','.','.','.','.','.','.','.','.','.'},

    {'.','.','.','.','.','.','.','.','.','.'},

};

char tank_down[10][10] = {

    {'#','#','#','.','.','.','.','.','.','.'},

    {'#','#','#','.','.','.','.','.','.','.'},

    {'#','#','#','.','.','.','.','.','.','.'},

    {'.','#','.','.','.','.','.','.','.','.'},

    {'.','.','.','.','.','.','.','.','.','.'},

    {'.','.','.','.','.','.','.','.','.','.'},

    {'.','.','.','.','.','.','.','.','.','.'},

    {'.','.','.','.','.','.','.','.','.','.'},

    {'.','.','.','.','.','.','.','.','.','.'},

    {'.','.','.','.','.','.','.','.','.','.'},

};

char tank_left[10][10] = {

    {'.','#','#','#','.','.','.','.','.','.'},

    {'#','#','#','#','.','.','.','.','.','.'},

    {'.','#','#','#','.','.','.','.','.','.'},

    {'.','.','.','.','.','.','.','.','.','.'},

    {'.','.','.','.','.','.','.','.','.','.'},

    {'.','.','.','.','.','.','.','.','.','.'},

    {'.','.','.','.','.','.','.','.','.','.'},

    {'.','.','.','.','.','.','.','.','.','.'},

    {'.','.','.','.','.','.','.','.','.','.'},

    {'.','.','.','.','.','.','.','.','.','.'},

};

char tank_right[10][10] = {

    {'#','#','#','.','.','.','.','.','.','.'},

    {'#','#','#','#','.','.','.','.','.','.'},

    {'#','#','#','.','.','.','.','.','.','.'},

    {'.','.','.','.','.','.','.','.','.','.'},

    {'.','.','.','.','.','.','.','.','.','.'},

    {'.','.','.','.','.','.','.','.','.','.'},

    {'.','.','.','.','.','.','.','.','.','.'},

    {'.','.','.','.','.','.','.','.','.','.'},

    {'.','.','.','.','.','.','.','.','.','.'},

    {'.','.','.','.','.','.','.','.','.','.'},

};

char static_bmp[10][10] = {

    {'.','#','.','#','.','.','.','.','.','.'},

    {'#','#','#','#','#','.','.','.','.','.'},

    {'#','#','#','#','#','.','.','.','.','.'},

    {'.','#','#','#','.','.','.','.','.','.'},

    {'.','.','#','.','.','.','.','.','.','.'},

    {'.','.','.','.','.','.','.','.','.','.'},

    {'.','.','.','.','.','.','.','.','.','.'},

    {'.','.','.','.','.','.','.','.','.','.'},

    {'.','.','.','.','.','.','.','.','.','.'},

    {'.','.','.','.','.','.','.','.','.','.'},

};

char static_bmp1[10][10] = {

    {'.','.','#','.','.','.','.','.','.','.'},

    {'.','.','#','.','.','.','.','.','.','.'},

    {'#','#','#','#','#','.','.','.','.','.'},

    {'.','.','#','.','.','.','.','.','.','.'},

    {'.','.','#','.','.','.','.','.','.','.'},

    {'.','.','.','.','.','.','.','.','.','.'},

    {'.','.','.','.','.','.','.','.','.','.'},

    {'.','.','.','.','.','.','.','.','.','.'},

    {'.','.','.','.','.','.','.','.','.','.'},

    {'.','.','.','.','.','.','.','.','.','.'},

};



 

class Rect

{

public:

   

    Rect(int a, int b, int y, int x, char static_bmp[10][10])

    {

        h = a;

        w = b;

       

        posy = y;

        posx = x;

       

        bmp=static_bmp;

    }

   

    ~Rect()

    {

       

    }

   

    int up()

    {

        bmp=tank_up;

        posy--;

        return 0;

    }

    int down()

    {

        bmp=tank_down;

        posy++;

        return 0;

    }

    int left()

    {

        bmp=tank_left;

        posx--;

        return 0;

    }

    int right()

    {

        bmp=tank_right;

        posx++;

        return 0;

    }

   

    int w;

    int h;

    int posx;

    int posy;

   

    char (*bmp)[10];

};

class Map

{

public:

   

    Map()

    {

        clear();

    }

   

    int draw(Rect *p_rc)

    {

        int i, j;

        for(i = 0; i < p_rc->h; i++)

        {

            for(j = 0; j < p_rc->w; j++)

            {

                if(p_rc->bmp[i][j] != '.')

                {

                    if(p_rc->posy+i < 20 && p_rc->posx+j < 20)

                    {

                        if(p_rc->posy+i >= 0 && p_rc->posx+j >= 0 )

                        {

                            map[p_rc->posy+i][p_rc->posx+j] = p_rc->bmp[i][j];

                        }

                    }

                }

                   

            }

        }

       

        return 0;

    }

   

    int render()

    {

        for(int i = 0; i < 20; i++)

        {

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

            {

                cout << map[i][j] << ' ';

            }

            cout << endl;

        }

       

        return 0;

    }

   

    int clear()

    {

        for(int i = 0; i < 20; i++)

        {

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

            {

                map[i][j] = '.';

            }

        }

       

        return 0;

    }

   

    char map[20][20];

   

};

void HideCursor() {

   HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);

   CONSOLE_CURSOR_INFO cursorInfo;

   GetConsoleCursorInfo(handle, &cursorInfo);

   cursorInfo.bVisible = false; // 隐藏光标

   SetConsoleCursorInfo(handle, &cursorInfo);

}

void MoveCursor(int x, int y) {

    COORD coord = { (SHORT)x, (SHORT)y };

    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);

}

int main() {

   

    Rect *p_rc1 = new Rect(5,5,2,2,tank_up);

    //Rect *p_rc2 = new Rect(5,5,3,5,static_bmp);

    Map *p_map = new Map();

    HideCursor();

    while(1)

    {    MoveCursor(0,0);

        p_map->draw(p_rc1);

        p_map->render();

        Sleep(100);

        //system("cls");

        p_map->clear();

       

        if(GetAsyncKeyState('W') & 0x8000)

        {

            p_rc1->up();

        }

        else if(GetAsyncKeyState('S') & 0x8000)

        {

            p_rc1->down();

        }

        else if(GetAsyncKeyState('A') & 0x8000)

        {

            p_rc1->left();

        }

        else if(GetAsyncKeyState('D') & 0x8000)

        {

            p_rc1->right();

        }

    }

   

   

    delete p_rc1;

    delete p_map;

   

    return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值