Implementation of pinball game based on curses Library

Time:2021-9-13

Find a pinball game realized by curses Library Based on Linux terminal on the Internet. I have also done Gobang games and share them in other articles.

/*
 *Based on curses library pinball game, compile GCC xxx.c - lcurses
 */
#include <curses.h>
#include <sys/time.h>
#include <signal.h>
 
#Define right cols-1 / * the maximum horizontal range of the current screen that the ball can reach*/
#Define bottom lines-1 / * the maximum vertical range of the current screen that the ball can reach*/
#define BOARD_ Length 10 / * baffle length*/
#Define left 0 / * the leftmost part of the current screen*/
#Define top 0 / * the top edge of the current screen*/
char BALL= 'O'; /* Shape of ball*/
char BLANK= ' '; /* Covering the trajectory of the ball*/
 
 
int left_ board; /* Left coordinate of baffle*/
int right_ board; /* Right coordinate of baffle*/
int is_lose=0;
 
 
int hdir; /* Variables that control the horizontal motion of the ball*/
int vdir; /* Variables that control the vertical motion of the ball*/
int pos_ X; /* Abscissa of sphere*/
int pos_ Y; /* Ordinate of the sphere*/
  
int delay=100;
void moveBall();
void init();
void control();
 
int main()
{
 //Initialize curses
 initscr();
 crmode(); /* Interrupt mode*/
 noecho(); /* Turn off echo*/
  
 move(6,28);
 attron(A_BOLD);
 addstr("Welcome to the BallGame!");
 move(8,20);
 attroff(A_BOLD);
 addstr("Help:");
 move(9,23);
 addstr("'N':Start a new game.");
 move(10,23);
 addstr("'Q':Quit game.");
 move(11,23);
 addstr("'KEY_LEFT' :Control baffle left shift.");
 move(12,23);
 addstr("'KEY_RIGHT':Control baffle right shift.");
 move(13,23);
 addstr("'KEY_UP' :Control of the ball speed.");
 move(14,23);
 addstr("'KEY_DOWN' :Control of the ball reducer.");
 int flag=1;
 char choice;
 move(16,24);
 addstr("Please choose your choice!(n/q):");
 refresh();
 choice=getch();
 while(flag){
  if(choice=='q'||choice=='Q'||choice=='n'||choice=='N')
    flag=0;
  else choice=getch();
 }
 If (choice = ='n '| choice = ='n') {/ * start the game*/
  clear();
  move(10,25);
  addstr("BallGame will start! Are you read?");
  refresh();
  sleep(3);
  control();
 }
 Else if (choice = ='q '| choice = ='q') {/ * quit the game*/
  clear();
  move(10,25);
  addstr("You quit the game successfully!");
  refresh();
  sleep(3);
  endwin();
 }
 endwin(); /* End curses*/
 return 0;
}
 
void init(){
 int i,j;
 clear();
 If (start_color() = = OK) {/ * change the color of the ball and baffle*/
  attron(A_BOLD); /* Open bold*/
  init_pair(1,COLOR_YELLOW,COLOR_BLACK);
  attron(COLOR_PAIR(1));
 }
 //Initial ball
 pos_ X =22; /* Abscissa of initial sphere*/
 pos_ Y = BOTTOM-1; /* Initial ordinate of the sphere*/
 //Initializes the direction of motion of the ball, moving towards the upper right
 hdir=1; 
 vdir=-1;
 
 //Initial baffle
 left_board=20;
 right_board=left_board+BOARD_LENGTH;
 For (I = left_board; I < = right_board; I + +) {/ * display bezel*/
  move(BOTTOM,i);
  addch('-');
 }
 
 //Initial refresh time
 signal(SIGALRM,moveBall);
 set_ticker(delay);
 
 keypad(stdscr,TRUE); /* Open keypad keyboard response*/
 attroff(A_BLINK);  /* Close a_ Blink attribute*/
  
 is_lose=0;
 move(pos_Y,pos_X);
 addch(BALL);
 move(LINES-1, COLS-1);
 refresh();
 usleep(100000); /* Sleep*/
 move(LINES-1,COLS-1);
 refresh();
}
 
void moveBall(){
 if(is_lose) return;
 signal(SIGALRM,moveBall);
 move(pos_Y,pos_X);
 addch(BLANK);
 pos_X += hdir;
 pos_Y += vdir;
 //When changing the direction of the ball
 If (pos_x > = right) {/ * when the abscissa of the ball is greater than the right edge, the ball bounces and moves to the left*/
  hdir = -1;
  beep(); /* The ball makes a sound when it hits the wall*/
 }
 If (pos_x < = left) {/ * when the abscissa of the ball is greater than the left edge, the ball bounces and moves to the right*/
  hdir = 1;
  beep(); /* The ball makes a sound when it hits the wall*/
 }
 If (pos_y < = top) {/ * when the ordinate of the ball is greater than the top edge, the ball bounces downward*/
  vdir = 1;
  beep(); /* The ball makes a sound when it hits the wall*/
 }
 
 //Extra handling when the ball is at the bottom
 if(pos_Y >= BOTTOM-1){
  If (pos_x > = left_board & & pos_x < = right_board) / * the ball is at the baffle*/
   vdir=-1;
  Else{/ * the ball is not at the baffle*/
   is_lose=1;
   move(pos_Y,pos_X);
   addch(BALL);
   move(LINES-1, COLS-1);
   refresh();
   usleep(delay*1000); /* Sleep*/
   move(pos_Y,pos_X);
   addch(BLANK);
   pos_X += hdir;
   pos_Y += vdir;
   move(pos_Y,pos_X);
   addch(BALL);
   move(LINES-1, COLS-1);
   refresh();
  }
 }
 //Without changing the direction of the ball
 move(pos_Y,pos_X);
 addch(BALL);
 move(LINES-1, COLS-1);
 refresh();
}
void control(){
 init();
 int cmd;
 while (1)
 {  
  if(!is_lose){
   cmd=getch();
   if(cmd=='q'||cmd=='Q'||cmd==27) break; // Forced exit from the game
   //Baffle moves left
   if(cmd==KEY_LEFT){
    if(left_board>0){
     move(BOTTOM,right_board);
     addch(' ');
     right_board--;
     left_board--;      
     move(BOTTOM,left_board);
     addch('-');
     move(BOTTOM,RIGHT);
     refresh();
    }
   }  
   //The baffle moves to the right
   else if(cmd==KEY_RIGHT){
    if(right_board<RIGHT){
     move(BOTTOM,left_board);
     addch(' ');
     right_board++;
     left_board++;
     move(BOTTOM,right_board);
     addch('-');
     move(BOTTOM,RIGHT);
     refresh();
    }
   }
   //Accelerate the ball
   else if(cmd==KEY_UP){ 
    delay/=2;  
    set_ticker(delay);
   }
   //Slow down the ball
   else if(cmd==KEY_DOWN){ 
    delay*=2;
    set_ticker(delay);
   }
   
  }
  else{
   //Treatment after losing the ball
   int flag=1;
   char choice;
   move(8,15);
   addstr("Game Over!try again?(y/n):");
   refresh();
   choice=getch();
 
   while(flag){
    if(choice=='y'||choice=='Y'||choice=='n'||choice=='N')
      flag=0;
    else choice=getch();
   }
   If (choice = ='y '| choice = ='y') {/ * the game starts again*/
     delay=100; /* Restore the initial speed of the ball*/
     init();
     continue;
   }
   Else if (choice = ='n '| choice = ='n') {/ * end the game*/
     break;
   }
  }
 }
}
//Set timer
int set_ticker(int n_msecs){
 struct itimerval new_timeset;  
 long n_sec,n_usecs;
 n_sec=n_msecs/1000;
 n_usecs=(n_msecs%1000)*1000L;
 new_timeset.it_interval.tv_sec=n_sec;
 new_timeset.it_interval.tv_usec=n_usecs;
 new_timeset.it_value.tv_sec=n_sec;
 new_timeset.it_value.tv_usec=n_usecs;
 return setitimer(ITIMER_REAL,&new_timeset,NULL); 
}

After compiling with GCC bounceball. C – lcurses, generate bounceball.out and run. / bounceball.out. The screenshot of the game is as follows:

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support developpaer.