坦克大战3.0

以下是一个简单的C++坦克大战游戏示例:


#include <iostream>
#include <conio.h>
#include <windows.h>

using namespace std;

class Tank {
private:
    int x, y;
public:
    Tank(int startX, int startY) {
        x = startX;
        y = startY;
    }

    void moveUp() {
        y--;
    }

    void moveDown() {
        y++;
    }

    void moveLeft() {
        x--;
    }

    void moveRight() {
        x++;
    }

    void draw() {
        COORD coord;
        coord.X = x;
        coord.Y = y;
        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
        cout << "T";
    }
};

int main() {
    const int WIDTH = 50;
    const int HEIGHT = 20;

    Tank tank(WIDTH / 2, HEIGHT / 2);

    while (true) {
        system("cls");

        for (int i = 0; i < WIDTH; i++) {
            cout << "#";
        }
        cout << endl;

        for (int i = 0; i < HEIGHT; i++) {
            for (int j = 0; j < WIDTH; j++) {
                if (j == 0 || j == WIDTH - 1) {
                    cout << "#";
                }
                else {
                    cout << " ";
                }
            }
            cout << endl;
        }

        for (int i = 0; i < WIDTH; i++) {
            cout << "#";
        }
        cout << endl;

        tank.draw();

        if (_kbhit()) {
            switch (_getch()) {
            case 'w':
                tank.moveUp();
                break;
            case 's':
                tank.moveDown();
                break;
            case 'a':
                tank.moveLeft();
                break;
            case 'd':
                tank.moveRight();
                break;
            }
        }

        Sleep(100);
    }

    return 0;
}

千万别打开c6c6.cc(你打开了当我没说)

  • 10
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值