#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <termios.h>

// Function to set the terminal to non-canonical mode for single keypress input
void set_terminal_mode() {
    struct termios t;
    tcgetattr(STDIN_FILENO, &t);
    t.c_lflag &= ~(ICANON | ECHO);
    tcsetattr(STDIN_FILENO, TCSANOW, &t);
}

// Function to reset the terminal mode
void reset_terminal_mode() {
    struct termios t;
    tcgetattr(STDIN_FILENO, &t);
    t.c_lflag |= ICANON | ECHO;
    tcsetattr(STDIN_FILENO, TCSANOW, &t);
}

// Function to get a single keypress
char get_key() {
    char key;
    set_terminal_mode();
    key = getchar();
    reset_terminal_mode();
    return key;
}

// Function to clear the screen
void clear_screen() {
    printf("\033[2J\033[1;1H");
}

// Function to draw the paddle
void draw_paddle(int x, int y) {
    printf("\033[%d;%dH=====", y, x);
}

// Function to draw the ball
void draw_ball(int x, int y) {
    printf("\033[%d;%dHO", y, x);
}

int main() {
    int paddle_x = 10, paddle_y = 10;
    int ball_x = 5, ball_y = 5;
    int ball_speed_x = 1, ball_speed_y = 1;

    clear_screen();
    draw_paddle(paddle_x, paddle_y);
    draw_ball(ball_x, ball_y);

    while (1) {
        char input = get_key();

        // Update paddle position based on user input
        if (input == 'a' && paddle_x > 1) {
            paddle_x--;
        } else if (input == 'd' && paddle_x < 80) {
            paddle_x++;
        }

        // Update ball position
        ball_x += ball_speed_x;
        ball_y += ball_speed_y;

        // Bounce the ball off walls
        if (ball_x <= 1 || ball_x >= 80) {
            ball_speed_x = -ball_speed_x;
        }

        if (ball_y <= 1 || ball_y >= 24) {
            ball_speed_y = -ball_speed_y;
        }

        // Bounce the ball off the paddle
        if (ball_x >= paddle_x && ball_x <= paddle_x + 5 && ball_y == paddle_y) {
            ball_speed_y = -ball_speed_y;
        }

        // Clear the screen and redraw the paddle and ball
        clear_screen();
        draw_paddle(paddle_x, paddle_y);
        draw_ball(ball_x, ball_y);

        // Introduce a delay to control the game speed
        usleep(100000);
    }

    return 0;
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: