0% found this document useful (0 votes)
5 views6 pages

Code

This document is a C program that uses the ncurses library to create a terminal-based animation application. Users can select from three animations: a bouncing ball, a spinning ball, or random dots, and can exit the program by pressing 'q'. The program includes functions for initializing and cleaning up ncurses, drawing and updating ball positions, and handling user input for animation selection.

Uploaded by

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

Code

This document is a C program that uses the ncurses library to create a terminal-based animation application. Users can select from three animations: a bouncing ball, a spinning ball, or random dots, and can exit the program by pressing 'q'. The program includes functions for initializing and cleaning up ncurses, drawing and updating ball positions, and handling user input for animation selection.

Uploaded by

ashish.raj998
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#include <ncurses.

h>

#include <unistd.h>

#include <stdlib.h>

#include <string.h>

#define DELAY 30000 // Microseconds (30 ms)

// Ball struct for animation

typedef struct {

char symbol;

int x, y;

int velocity_x, velocity_y;

} Ball;

// Function prototypes

void init_ncurses();

void cleanup_ncurses();

void draw_ball(Ball *ball);

void update_ball_position(Ball *ball, int width, int height);

void show_menu();

void animate_bouncing_ball();

void animate_spinning_ball();

void animate_random_dots();

void run_animation(int choice);

int main() {

int choice;

init_ncurses();
while (1) {

show_menu();

choice = getch();

if (choice == 'q') {

break;

run_animation(choice);

cleanup_ncurses();

return 0;

// Initialize ncurses

void init_ncurses() {

initscr();

cbreak();

noecho();

curs_set(0);

timeout(0);

keypad(stdscr, TRUE);

// Cleanup ncurses

void cleanup_ncurses() {

endwin();
}

// Draw a ball on the screen

void draw_ball(Ball *ball) {

mvaddch(ball->y, ball->x, ball->symbol);

// Update ball position and handle collisions

void update_ball_position(Ball *ball, int width, int height) {

ball->x += ball->velocity_x;

ball->y += ball->velocity_y;

if (ball->x <= 0 || ball->x >= width - 1) {

ball->velocity_x = -ball->velocity_x;

if (ball->y <= 0 || ball->y >= height - 1) {

ball->velocity_y = -ball->velocity_y;

// Show the main menu

void show_menu() {

clear();

mvprintw(0, 0, "Select Animation:");

mvprintw(1, 0, "1. Bouncing Ball");

mvprintw(2, 0, "2. Spinning Ball");

mvprintw(3, 0, "3. Random Dots");

mvprintw(4, 0, "q. Quit");

refresh();
}

// Animate a bouncing ball

void animate_bouncing_ball() {

int height, width;

getmaxyx(stdscr, height, width);

Ball ball = { 'O', width / 2, height / 2, 1, 1 };

while (1) {

clear();

draw_ball(&ball);

refresh();

update_ball_position(&ball, width, height);

usleep(DELAY);

if (getch() == 'q') {

break;

// Animate a spinning ball

void animate_spinning_ball() {

int height, width;

getmaxyx(stdscr, height, width);

int radius = 5;

int center_x = width / 2;


int center_y = height / 2;

for (int angle = 0; angle < 360; angle += 5) {

clear();

int x = center_x + radius * cos(angle * M_PI / 180);

int y = center_y + radius * sin(angle * M_PI / 180);

mvaddch(y, x, 'O');

refresh();

usleep(DELAY);

if (getch() == 'q') {

break;

// Animate random dots

void animate_random_dots() {

int height, width;

getmaxyx(stdscr, height, width);

srand(time(NULL));

while (1) {

clear();

for (int i = 0; i < 100; i++) {

int x = rand() % width;

int y = rand() % height;

mvaddch(y, x, '.');
}

refresh();

usleep(DELAY);

if (getch() == 'q') {

break;

// Run the selected animation

void run_animation(int choice) {

switch (choice) {

case '1':

animate_bouncing_ball();

break;

case '2':

animate_spinning_ball();

break;

case '3':

animate_random_dots();

break;

default:

mvprintw(5, 0, "Invalid choice, please select again.");

refresh();

usleep(2000000);

break;

You might also like