Make two functions act simultaneously

Asked

Viewed 51 times

0

I need to make a Duck Hunt style game in C is almost everything ready, but we are not able to make the target move at the same time as the projectile, what happens is that we press to shoot, and only after the shoot function finish executing the target function runs, which makes it impossible to hit the target, since it only appears after the projectile disappears from the screen

that’s the code to the gun:

   arcoEflecha();
   while(1){
       while(kbhit()){
           parabola();
       }
       tecla = getch();
       if(tecla == 'd'){
           system("cls");
           arcoEflechaDireito();
           int b = 0;
           int c = 0;
           for(a = 0; a < 10; a++){
               gotoxy(67+b,24-c);
               printf("/");
               Sleep(60);
               gotoxy(67+b,24-c);
               printf(" ");
               b = b + 2;
               c = c + 2;
           }
       }
      
       if(tecla == 'a'){
           system("cls");
           arcoEflechaEsquerdo();
           int b = 0;
           int c = 0;
           for(a = 0; a < 10; a++){
               gotoxy(53-b,24-c);
               printf("\\");
               Sleep(60);
               gotoxy(53-b,24-c);
               printf(" ");
               b = b + 2;
               c = c + 2;
           }
       }
       
       
       if(tecla =='p'){           //ATIRAR O PROJETIL
           system("cls");
           for(j = 1; j < 20; j++){
               arcoEflecha();
               gotoxy(*pi+5,*pk-j);
               printf("|");
               Sleep(45);
               gotoxy(*pi+5,*pk-j);
               printf(" ");
               j++;
           }
       
       }
       
   }

and that of the target:

void parabola(){
    for(int g = 0; g<3;g++){   
        int z,m;
        int i = 25;
        int j = 0;
        m = 5 + ( rand() % 10 );
        for(z = 0; z < 6; z++){
            for(int k = 0; k < 6; k++){
                gotoxy(i+j,m-z);
                printf("Q");
                Sleep(45);
                gotoxy(i+j,m-z);
                printf("  ");
                j++;
            } 
        }
        for(int z = 0; z < 7; z++){
            for(int k = 0; k < 6; k++){
                gotoxy(i+j,(m+z)-6);
                printf("Q ");
                Sleep(45);
                gotoxy(i+j,(m+z)-6);
                printf("  ");
                j++;
            } 
        }
    }
}
  • Please clarify your problem or provide additional details in order to highlight exactly what you need. The way it’s written these days it’s hard to tell exactly what you’re asking.

  • the target appears on the screen from left to right in a movement of a parable, and we need to move the weapon to be able to hit, but the target appears only after we press to shoot and after the bullet completes its path, as if the target was only released after the player shoot

  • You have to separate the keyboard reading from the screen composition and the character model. Probably the keyboard reading of the screen composition should run in different threads. The keyboard reading should only collect the keyboard data and update the character model and the screen composition function from the character model will print the entire screen.

  • Maybe you can avoid the complexity of using multiple threads using just one event loop. I didn’t say what system you’re using, and that makes a big difference. Anyway program the input to read what you have and of course do not wait for enter, and in the main loop see if something came from the keyboard and continue with the game

  • on linux you use ioctl and things like that. disable echo and use VMIN and VTIME as 0 so not for the program in reading. On Windows use SetConsoleMode or something like that and turn off the LINE INPUT.

  • A tip: never use threads or Forks, to control this, unless you are developing an AAA+ game. The output is to keep a variable that controls the "passage of time" in the game (avoid global variables, but use if necessary), and each time frame you update all objects (characters, scenery, etc.) based on increment of this "step-by-step" variable, because the game itself is only a finite state machine. There is a great explanation for this in this book (free to read online, in English, by going to "Web" button "Read Now"): https://gameprogrammingpatterns.com/

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.