0% found this document useful (0 votes)
11 views3 pages

Procg

Uploaded by

bshrijaa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

Procg

Uploaded by

bshrijaa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

INPUT:-

#include <stdio.h>
#include <unistd.h> // For sleep() and usleep() functions
#include <stdlib.h> // For system("clear")
#include <termios.h> // For terminal input
#include <fcntl.h> // For non-blocking input

#define WIDTH 60
#define HEIGHT 30

// Function to check for keyboard input without blocking


int kbhit(void) {
struct termios oldt, newt;
int ch;
int oldf;

tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);

ch = getchar();

tcsetattr(STDIN_FILENO, TCSANOW, &oldt);


fcntl(STDIN_FILENO, F_SETFL, oldf);

if (ch != EOF) {
ungetc(ch, stdin);
return 1;
}

return 0;
}

// Function to set text color using ANSI escape codes


void setColor(int color) {
printf("\033[0;%dm", color); // ANSI escape code format
}

int main() {
int x, y; // Ball coordinates
int x_dir, y_dir; // Ball direction
char again;

do {
x = 0, y = 0;
x_dir = 1, y_dir = 1; // Ball direction: 1 for down/right, -1 for up/left

while (!kbhit()) { // Run until a key is pressed


// Clear screen
system("clear");

// Draw the grid


for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
if (i == y && j == x) {
setColor(31); // Set red color (ANSI color code 31)
printf(""); // Ball
setColor(0); // Reset to default color
} else {
printf(" "); // Empty space
}
}
printf("\n");
}

// Ball movement logic


x += x_dir;
y += y_dir;

// Bounce horizontally
if (x <= 0 || x >= WIDTH - 1) {
x_dir *= -1; // Change direction
}

// Bounce vertically
if (y <= 0 || y >= HEIGHT - 1) {
y_dir *= -1; // Change direction
}

// Control the speed of the ball


usleep(100000); // Delay in microseconds (100ms)
}

// Flush out any key press and clear input buffer


while (kbhit()) getchar();

printf("\nAnimation stopped. Do you want to try again? (y/n): ");


again = getchar();

} while (again == 'y' || again == 'Y');

return 0;

}
OUTPUT:-

You might also like