Untitled

 avatar
unknown
plain_text
6 months ago
3.0 kB
3
Indexable
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <conio.h>  // For _kbhit() and _getch() on Windows

using namespace std;

const int WIDTH = 20;
const int HEIGHT = 10;
int playerPosX = WIDTH / 2;
int bulletX = -1, bulletY = -1;
int score = 0;
bool gameOver = false;

struct Invader {
    int x, y;
    bool alive = true;
};

vector<Invader> invaders;

void setupInvaders() {
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 3; j++) {
            invaders.push_back({i * 4 + 1, j + 1, true});
        }
    }
}

void draw() {
    system("cls");  // Clear the screen (use "clear" for Linux)

    for (int y = 0; y < HEIGHT; y++) {
        for (int x = 0; x < WIDTH; x++) {
            if (y == 0 || y == HEIGHT - 1) {
                cout << "#";  // Draw top and bottom walls
            } else if (x == 0 || x == WIDTH - 1) {
                cout << "#";  // Draw side walls
            } else if (x == playerPosX && y == HEIGHT - 2) {
                cout << "A";  // Draw player
            } else if (x == bulletX && y == bulletY) {
                cout << "|";  // Draw bullet
            } else {
                bool printed = false;
                for (auto &inv : invaders) {
                    if (inv.alive && inv.x == x && inv.y == y) {
                        cout << "M";  // Draw invader
                        printed = true;
                        break;
                    }
                }
                if (!printed) {
                    cout << " ";
                }
            }
        }
        cout << endl;
    }

    cout << "Score: " << score << endl;
}

void input() {
    if (_kbhit()) {
        switch (_getch()) {
        case 'a':
            playerPosX = max(1, playerPosX - 1);
            break;
        case 'd':
            playerPosX = min(WIDTH - 2, playerPosX + 1);
            break;
        case ' ':
            if (bulletY == -1) {  // Fire bullet if no active bullet
                bulletX = playerPosX;
                bulletY = HEIGHT - 3;
            }
            break;
        }
    }
}

void logic() {
    // Move bullet
    if (bulletY != -1) {
        bulletY--;
        if (bulletY == 0) {
            bulletY = -1;  // Reset bullet if it reaches the top
        }

        // Check for invader hit
        for (auto &inv : invaders) {
            if (inv.alive && inv.x == bulletX && inv.y == bulletY) {
                inv.alive = false;
                bulletY = -1;
                score += 10;
                break;
            }
        }
    }

    // Check if all invaders are dead
    bool allDead = true;
    for (auto &inv : invaders) {
        if (inv.alive) {
            allDead = false;
            break;
        }
    }
    if (allDead) {
        gameOver = true;
    }
}

int main() {
    srand(time(0));
    setupInvaders();

    while (!gameOver) {
        draw();
        input();
        logic();
    }

    cout << "Game Over! Final Score: " << score << endl;

    return 0;
}
Editor is loading...
Leave a Comment