Tuesday, April 29, 2025
HomeC ProgrammingBouncing an Asterisk | C For Dummies Weblog

Bouncing an Asterisk | C For Dummies Weblog


For final week’s Lesson, I gathered varied strategies to indicate how the terminal display screen might be manipulated straight in C with out utilizing a library like Ncurses. I’ve a number of extra methods to indicate.

The following merchandise I’d like to tug from outdated code is the kbhit() operate, as soon as in style in C programming for older (non-multitasking) working techniques. I wrote about emulating this operate in one other submit. As with varied terminal manipulation magic, recreating the kbhit() operate entails a name to the ioctl() operate to peek into the terminal’s guts. You possibly can learn the small print within the authentic submit.

My function for including the kbhit() operate is to recreate the outdated bouncing asterisk program, which was in style with budding programmers approach again when. The impact is seen as a personality that traverses the display screen, altering instructions when it bumps into the display screen’s edge.

The kbhit() operate is critical to ballot the keyboard to test for a press of the Enter key, which terminates this system. I additionally up to date the code introduced in final week’s Lesson, modifying the find() operate to not solely transfer the cursor however to put a personality on the given place. The brand new operate is named putat(), and it’s proven on this replace to the supply code file:

2024_10_05-Lesson.c


#embody <stdio.h>
#embody <time.h>
#embody <unistd.h>
#embody <sys/ioctl.h>

#outline dwelling() printf("e[H")
#define clear() printf("e[He[2J")


int kbhit(void)
{
    int k;

    ioctl(STDIN_FILENO,FIONREAD,&k);

    return(k);
}


void putat(x,y,c)
{
    printf("e[%d;%dH%c",y,x,c);
}


void delay(int m)
{
    long pause;
    clock_t now,then;

    pause = m*(CLOCKS_PER_SEC/1000);
    now = then = clock();
    while( (now-then) < pause )
        now = clock();
}


int main()
{
    int rows,columns,posx,posy,vert,horz;
    struct winsize w;
    char buffer[BUFSIZ];

    
    ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
    rows = w.ws_row;
    columns = w.ws_col;

    
    setvbuf(stdout,buffer,_IONBF,BUFSIZ);

    
    clear();
    places("Press Enter to finish");
    posx=columns/2;
    posy=rows/2;
    vert=1;
    horz=1;

    
    whereas(1)
    {
        
        if( kbhit() )
        {
            getchar();
            break;
        }
        
        putat(posx,posy,'*');
        delay(125);                
        putat(posx,posy,' ');    
        
        if( posx==columns-1 || posx==1 )
            horz=-horz;            
        posx += horz;
        if( posy==rows-1 || posy==2 )
            vert=-vert;            
        posy += vert;
    }
    
    putat(1,rows-1,'B');
    places("ye!");

    return 0;
}

This code provides 4 new int variables to trace the bouncing asterisk: posx, posy, vert, and horz. The posx and posy variables comprise the asterisk’s column and row positions. The vert and horz variables maintain 1 or -1 to set the course.

The bouncing motion takes place within the infinite whereas loop. The kbhit() operate terminates the loop when the consumer presses the Enter key. (It’s not an ideal substitute for the unique kbhit() as different characters entered seem within the output.)

Inside the loop, the asterisk is positioned, execution delays for 125 milliseconds, then the asterisk is erased. An if take a look at checks for the perimeters of the terminal window. When encountered, the horz or vert variables are inverted, altering the asterisk’s course. This motion repeats till the consumer presses Enter.

Determine 1 reveals a pattern run.

Determine 1. This system’s pattern run.

I proceed messing with these options in subsequent week’s Lesson.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments