How can I make one switch run in parallel with another?

Asked

Viewed 39 times

0

I made this code for moving a character to a college assignment, but I wish that when I pressed the 'a', I could still walk and the ship wasn’t erased, just as I could press the 'a' several times and get several shots out,I took a look at the pthread but I do not know how to apply in this case, if you know otherwise it would also help a lot. I do not know if pthread is the most suitable, since I use windows.

struct posicao nave_pos = {0,37,'A'};
struct posicao laser_pos = {0,36,'^'};
 void movimentacao_nave()
{
nave_pos.x = 40;
nave_pos.y = 37;
char C;

do
{
    gotoxy(nave_pos.x,nave_pos.y);
    printf("%c", nave_pos.c);
    C=getch();
    system("cls");
    switch(C)
    {
    case 75:
        nave_pos.x=nave_pos.x-1;
        if(nave_pos.x==0)
            nave_pos.x=nave_pos.x+1;
        break;
    case 77:
        nave_pos.x=nave_pos.x+1;
        if(nave_pos.x>83)
            nave_pos.x=nave_pos.x-1;
        break;
    case 'a':
            laser_pos.x = nave_pos.x;
for(laser_pos.y = 37 ; laser_pos.y > 0 ; laser_pos.y--)
{
    gotoxy(laser_pos.x,laser_pos.y);
    printf("%c", laser_pos.c);
    Sleep(50);
    system("cls");
}
        break;
    }

}
while(1);
 }
  • 1

    There are several points in your question, I’ll try to help: 1. Cannot read more than one key using the function getch, you will have to use a more advanced library for this. 2. as for cleaning the screen, just remove the command system("cls"), but you will have to "erase" everything before changing position and draw again. 3. for multiple shots, you need to use an array to store the positions of multiple items, and follow the same process, delete each one, change the position and draw again.

  • And there’s another point: you’re implementing an action game engine. As an action game, time is an important mechanic. Thus, you cannot go up to the final laser position to then release the reading again. The correct one would be to process each "frame change", plot these frame changes on the screen, and then try to detect user input again. You would need to implement something that remembers this code in Python, would need to implement a "game engine" (game engine)

  • @Ricardopunctual I made the change to an array, but still I can’t , I believe that because n can run the function several times in sequence, reading that I typed it several times, you would know how to read it several times?

  • @Jeffersonquesado, I will try to implement this.

  • Ever thought about using GOTO with another switch in parallel? (It’s unusual but I think it would solve your problem)

No answers

Browser other questions tagged

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