0

bắn máy bay

đã đăng vào 7, Tháng 7, 2026, 14:07

include <iostream>

include <windows.h>

include <conio.h>

include

include <ctime>

using namespace std;

const int WIDTH = 40; const int HEIGHT = 20;

struct Point { int x, y; };

class Bullet { public: Point pos; bool isDead;

Bullet(int startX, int startY) {
    pos.x = startX;
    pos.y = startY;
    isDead = false;
}

void update() {
    pos.y--;
    if (pos.y <= 0) isDead = true;
}

}; class Enemy { public: Point pos; bool isDead;

Enemy(int startX, int startY) {
    pos.x = startX;
    pos.y = startY;
    isDead = false;
}

void update() {
    pos.y++;
    if (pos.y >= HEIGHT - 1) isDead = true;
}

};

class Player { public: Point pos; int hp;

Player() {
    pos.x = WIDTH / 2;
    pos.y = HEIGHT - 2;
    hp = 10;
}

void move(int dx) {
    pos.x += dx;
    if (pos.x <= 0) pos.x = 1;
    if (pos.x >= WIDTH - 1) pos.x = WIDTH - 2;
}

};

class GameEngine { private: Player player; vector<Bullet> bullets; vector<Enemy> enemies; int score; bool gameOver; int spawnTimer; void gotoXY(int x, int y) { COORD coord; coord.X = x; coord.Y = y; SetConsoleCursorPosition(GetStdHandle(STDOUTPUTHANDLE), coord); }

public: GameEngine() { score = 0; gameOver = false; spawnTimer = 0; srand(time(0)); }

void input() {
    if (_kbhit()) {
        switch (_getch()) {
            case 'a': case 'A': player.move(-2); break;
            case 'd': case 'D': player.move(2); break;
            case ' ':
                bullets.push_back(Bullet(player.pos.x, player.pos.y - 1));
                break;
            case 'x': case 'X': gameOver = true; break;
        }
    }
}

void logic() {
spawnTimer++;
if (spawnTimer >= 50) { 
    int rx = rand() % (WIDTH - 2) + 1;
    enemies.push_back(Enemy(rx, 1));
    spawnTimer = 0;
}
for (size_t i = 0; i < bullets.size(); i++) {
    bullets[i].update();
    if (bullets[i].isDead) {
        bullets.erase(bullets.begin() + i);
        i--;
    }
}
static int enemyMoveTimer = 0;
enemyMoveTimer++;

if (enemyMoveTimer >= 3) {
    for (size_t i = 0; i < enemies.size(); i++) {
        enemies[i].update();

        if (enemies[i].pos.y >= HEIGHT - 1) {
            player.hp--;
            enemies.erase(enemies.begin() + i);
            i--;
            if (player.hp <= 0) gameOver = true;
        }
    }
    enemyMoveTimer = 0;
}
    for (size_t b = 0; b < bullets.size(); b++) {
        for (size_t e = 0; e < enemies.size(); e++) {
            if (bullets[b].pos.x == enemies[e].pos.x && bullets[b].pos.y == enemies[e].pos.y) {
                score += 10;
                bullets.erase(bullets.begin() + b);
                enemies.erase(enemies.begin() + e);
                b--;
                break;
            }
        }
    }
    for (size_t e = 0; e < enemies.size(); e++) {
        if (enemies[e].pos.x == player.pos.x && enemies[e].pos.y == player.pos.y) {
            player.hp--;
            enemies.erase(enemies.begin() + e);
            e--;
            if (player.hp <= 0) gameOver = true;
        }
    }
}

void draw() {
    gotoXY(0, 0);

    for (int i = 0; i < HEIGHT; i++) {
        for (int j = 0; j < WIDTH; j++) {
            if (i == 0 || i == HEIGHT - 1 || j == 0 || j == WIDTH - 1) {
                cout << "#";
            }
            else if (j == player.pos.x && i == player.pos.y) {
                cout << "^";
            }
            else {
                bool isPrinted = false;
                for (const auto& b : bullets) {
                    if (b.pos.x == j && b.pos.y == i) {
                        cout << "|";
                        isPrinted = true;
                        break;
                    }
                }
                if (!isPrinted) {
                    for (const auto& e : enemies) {
                        if (e.pos.x == j && e.pos.y == i) {
                            cout << "V";
                            isPrinted = true;
                            break;
                        }
                    }
                }

                if (!isPrinted) cout << " ";
            }
        }
        cout << "\n";
    }
    cout << "DIEM SO: " << score << "  |  MANG: " << player.hp << "       \n";
}

void run() {
    HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_CURSOR_INFO info;
    info.dwSize = 100;
    info.bVisible = FALSE;
    SetConsoleCursorInfo(consoleHandle, &info);
    while (!gameOver) {
        draw();
        input();
        logic();
        Sleep(40);
    }
    system("cls");
    cout << "=================================\n";
    cout << "           GAME OVER             \n";
    cout << "       Diem so cua ban: " << score << "     \n";
    cout << "=================================\n";
    system("pause");
}

};

int main() { GameEngine game; game.run(); return 0; }


Bình luận

Hãy đọc nội quy trước khi bình luận.


Không có bình luận tại thời điểm này.