Untitled

mail@pastecode.io avatar
unknown
plain_text
4 months ago
5.3 kB
2
Indexable
#include <iostream>
#include <conio.h>
#include <windows.h>
#include <vector> 

using namespace std; 

// Screen dimensions
const int width = 20;
const int height = 20; 

// Game objects
int playerX, playerY;
vector<pair<int, int>> bullets;
vector<pair<int, int>> enemies;
bool gameOver = false;
int score = 0;
int aeroplanesDestroyed = 0; // New variable to track destroyed planes 

// Function to play sound effects
void playSound(int frequency, int duration) {
    Beep(frequency, duration);
} 

// Draw the game screen
void draw() {
    system("cls");
    for (int i = 0; i < width + 2; i++) cout << "#";
    cout << endl; 

    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            bool printed = false; 

            if (x == playerX && y == playerY) {
                cout << "A";  // Player airplane
                printed = true;
            } 

            for (const auto& bullet : bullets) {
                if (bullet.first == x && bullet.second == y) {
                    cout << "|";
                    printed = true;
                    break;
                }
            } 

            if (!printed) {
                for (const auto& enemy : enemies) {
                    if (enemy.first == x && enemy.second == y) {
                        cout << "E";
                        printed = true;
                        break;
                    }
                }
            } 

            if (!printed) cout << " ";
            if (x == width - 1) cout << "#";
        }
        cout << endl;
    } 

    for (int i = 0; i < width + 2; i++) cout << "#";
    cout << endl; 

    cout << "Score: " << score << endl;
    cout << "Aeroplanë të shkatërruar: " << aeroplanesDestroyed << endl; // Display the count of destroyed planes
} 

// Update the game state
void update() {
    // Move bullets
    for (int i = 0; i < bullets.size(); i++) {
        bullets[i].second--;
        if (bullets[i].second < 0) {
            bullets.erase(bullets.begin() + i);
            i--;
        }
    } 

    // Move enemies and check collisions
    for (int i = 0; i < enemies.size(); ) {
        enemies[i].second++;
        if (enemies[i].second >= height || (enemies[i].second == playerY && enemies[i].first == playerX)) {
            gameOver = true;
            break;
        } 

        bool hit = false;
        for (int j = 0; j < bullets.size(); ) {
            if (bullets[j].first == enemies[i].first && bullets[j].second == enemies[i].second) {
                bullets.erase(bullets.begin() + j);
                enemies.erase(enemies.begin() + i);
                score += 10;
                aeroplanesDestroyed++; // Increment the number of destroyed planes
                playSound(1000, 200); // Play sound when an enemy is destroyed
                hit = true;
                break;
            } else {
                j++;
            }
        } 

        if (!hit) {
            i++;
        }
    } 

    // Add new enemies
    if (rand() % 10 < 2) {
        enemies.push_back({rand() % width, 0});
    }
} 

// Get user input
void input() {
    if (_kbhit()) {
        char ch = _getch();
        switch (ch) {
            case 'a':
                if (playerX > 0) playerX--;
                break;
            case 'd':
                if (playerX < width - 1) playerX++;
                break;
            case 'w':
                if (playerY > 0) playerY--;
                break;
            case 's':
                if (playerY < height - 1) playerY++;
                break;
            case ' ':
                bullets.push_back({playerX, playerY - 1});
                playSound(500, 100); // Play sound when firing a bullet
                break;
            case 'x':
                gameOver = true;
                break;
        }
    }
} 

// Game loop
int main() {
    playerX = width / 2;
    playerY = height - 1; 

    while (!gameOver) {
        draw();
        input();
        update();
        Sleep(100);
    } 

    cout << "Game Over! Final Score: " << score << endl;
    cout << "Total Aeroplanë të shkatërruar: " << aeroplanesDestroyed << endl; // Display the final count of destroyed planes
    playSound(1500, 500); // Play sound when game is over
    return 0;
}
Leave a Comment