Загрузка данных


#include <ncurses.h>  // Только эта библиотека добавлена
#include <stdio.h>

#define WIDTH 81
#define HEIGHT 25
#define ROCKET_HEIGHT 3
#define WIN_SCORE 21

void print_field(int rocket1_y, int rocket2_y, float ball_x, float ball_y, int player1_score,
                 int player2_score) {
    clear();

    // Верхняя граница
    for (int i = 0; i < WIDTH; i++) mvprintw(0, i, "-");

    // Счет
    mvprintw(1, 2, "Player 1: %02d", player1_score);
    mvprintw(1, WIDTH / 2 + 2, "Player 2: %02d", player2_score);

    // Вторая верхняя граница
    for (int i = 0; i < WIDTH; i++) mvprintw(2, i, "-");

    // Игровое поле
    for (int y = 1; y < HEIGHT - 1; y++) {
        for (int x = 0; x < WIDTH; x++) {
            if (x == 0 || x == WIDTH - 1) {
                mvprintw(y + 2, x, "|");
            } else if (x == WIDTH / 2) {
                mvprintw(y + 2, x, "|");
            } else if (x == 1 && y >= rocket1_y && y < rocket1_y + ROCKET_HEIGHT) {
                attron(COLOR_PAIR(1));
                mvprintw(y + 2, x, "|");
                attroff(COLOR_PAIR(1));
            } else if (x == WIDTH - 2 && y >= rocket2_y && y < rocket2_y + ROCKET_HEIGHT) {
                attron(COLOR_PAIR(2));
                mvprintw(y + 2, x, "|");
                attroff(COLOR_PAIR(2));
            } else if (x == (int)ball_x && y == (int)ball_y) {
                attron(COLOR_PAIR(3));
                mvprintw(y + 2, x, "o");
                attroff(COLOR_PAIR(3));
            } else {
                mvprintw(y + 2, x, " ");
            }
        }
    }

    // Нижняя граница
    for (int i = 0; i < WIDTH; i++) mvprintw(HEIGHT + 1, i, "-");

    // Управление
    mvprintw(HEIGHT + 2, 2, "Controls: A/Z (Player 1) | K/M (Player 2) | Q (quit)");

    refresh();  // Обновляем экран
}

void show_game_result(int score1, int score2) {
    clear();

    int center_x = WIDTH / 2;

    // Создаем красивый экран окончания
    mvprintw(HEIGHT / 2 - 5, center_x - 20, "+--------------------------------------------------+");
    mvprintw(HEIGHT / 2 - 4, center_x - 20, "|                                                  |");
    mvprintw(HEIGHT / 2 - 3, center_x - 20, "|                     GAME OVER                    |");
    mvprintw(HEIGHT / 2 - 2, center_x - 20, "|                                                  |");

    // Победитель
    if (score1 >= WIN_SCORE) {
        mvprintw(HEIGHT / 2 - 1, center_x - 20, "|                  WINNER: PLAYER 1                |");
    } else {
        mvprintw(HEIGHT / 2 - 1, center_x - 20, "|                  WINNER: PLAYER 2                |");
    }

    // Счет (используем прямое форматирование)
    mvprintw(HEIGHT / 2, center_x - 20, "|                  SCORE: %02d - %02d                  |", score1,
             score2);

    mvprintw(HEIGHT / 2 + 1, center_x - 20, "|                                                  |");
    mvprintw(HEIGHT / 2 + 2, center_x - 20, "|                                                  |");
    mvprintw(HEIGHT / 2 + 3, center_x - 20, "|                 Press 'Q' to exit                |");
    mvprintw(HEIGHT / 2 + 4, center_x - 20, "|                                                  |");
    mvprintw(HEIGHT / 2 + 5, center_x - 20, "+--------------------------------------------------+");

    refresh();

    // Меняем режим ввода на блокирующий для экрана окончания
    nodelay(stdscr, FALSE);  // Блокирующий ввод

    // Ждем нажатия Q для выхода
    int ch;
    while (1) {
        ch = getch();
        if (ch == 'q' || ch == 'Q') {
            break;
        }
    }
}

int main() {
    // Инициализация ncurses
    initscr();              // Инициализация экрана
    cbreak();               // Отключаем буферизацию строк
    noecho();               // Не отображаем ввод
    nodelay(stdscr, TRUE);  // Неблокирующий ввод
    keypad(stdscr, TRUE);   // Включаем специальные клавиши
    curs_set(0);            // Скрываем курсор

    if (has_colors()) {
        start_color();
        init_pair(1, COLOR_CYAN, COLOR_BLACK);    // Ракетка 1
        init_pair(2, COLOR_GREEN, COLOR_BLACK);     // Ракетка 2
        init_pair(3, COLOR_RED, COLOR_BLACK);  // Мяч
    }

    float ball_x = 40.0, ball_y = 12.0;
    float ball_dx = -1.0, ball_dy = 1.0;
    int rocket1_y = 11, rocket2_y = 11;
    int score1 = 0, score2 = 0;
    int game_over = 0;
    int ch;

    print_field(rocket1_y, rocket2_y, ball_x, ball_y, score1, score2);

    while (!game_over) {
        ch = getch();

        if (ch == 'a' || ch == 'A') {
            if (rocket1_y > 1) rocket1_y--;
        } else if (ch == 'z' || ch == 'Z') {
            if (rocket1_y < HEIGHT - ROCKET_HEIGHT - 1) rocket1_y++;
        } else if (ch == 'k' || ch == 'K') {
            if (rocket2_y > 1) rocket2_y--;
        } else if (ch == 'm' || ch == 'M') {
            if (rocket2_y < HEIGHT - ROCKET_HEIGHT - 1) rocket2_y++;
        } else if (ch == 'q' || ch == 'Q') {
            game_over = 1;
        } else if (ch == ' ') {
            // Пробел - ничего не делаем
        }

        // Движение мяча
        ball_x += ball_dx;
        ball_y += ball_dy;

        // Отскок от стен
        if (ball_y <= 1 || ball_y >= HEIGHT - 2) {
            ball_dy = -ball_dy;
        }

        // Отскок от ракеток
        if (ball_x <= 2) {
            if (ball_y >= rocket1_y && ball_y < rocket1_y + ROCKET_HEIGHT) {
                ball_dx = -ball_dx;
                ball_x = 3;
            }
        }

        if (ball_x >= WIDTH - 3) {
            if (ball_y >= rocket2_y && ball_y < rocket2_y + ROCKET_HEIGHT) {
                ball_dx = -ball_dx;
                ball_x = WIDTH - 4;
            }
        }

        // Голы
        if (ball_x <= 0) {
            score2++;
            ball_x = WIDTH / 2;
            ball_y = HEIGHT / 2;
            ball_dx = 1.0;
            ball_dy = 1.0;
        }

        if (ball_x >= WIDTH - 1) {
            score1++;
            ball_x = WIDTH / 2;
            ball_y = HEIGHT / 2;
            ball_dx = -1.0;
            ball_dy = 1.0;
        }

        // Проверка победы
        if (score1 >= WIN_SCORE || score2 >= WIN_SCORE) {
            game_over = 1;
        }

        // Отрисовка
        print_field(rocket1_y, rocket2_y, ball_x, ball_y, score1, score2);

        napms(10);
    }

    // Показываем результат и ждем Q для выхода
    show_game_result(score1, score2);

    // Завершение ncurses
    endwin();

    return 0;
}