0

I'm trying to get a quick time event type of interaction with the console and I managed to obtain it using the conio library. Sadly the project I'm working on requires the code to be compilable on both Windows and Linux and I can't figure out a way to change it.

Is there anything I can do to get the desired effect or should I give up on this notion? Below is the code for the function I created.

#include <stdio.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
void battle(int x) {
    srand(time(0));
    printf("Use Q to dodge projectile attacks, E to block melee attacks and W to attack when the boss is stunned. Button mashing will not work! Press any key to start the battle.\n\nYou face Brutus, Lord Incarcerator!\n\n");
    getchar();
    bool ok;
    int dealt = 0 ,recieved = 0 , state, prev = 0;
    time_t start, end;
    double elapsed;
    while(dealt < 5 && recieved < 3)
    {
        do
        {
            state = rand() % 3 + 1;
        }
        while(prev == state);
        prev = state;
        time(&start);
        switch(state)
        {
        case(1):
            ok = 1;
            printf("Brutus uses Hook Attack!\n\n");
            do
            {
                time(&end);
                elapsed = difftime(end, start);
                if(kbhit())
                {
                    if( getchar() == 'q')
                    {
                        printf("Dodged!\n\n");
                        ok = 0;
                        break;
                    }
                    else
                    {
                        break;
                    }
                }
            }
            while(elapsed < 3);
            if(ok)
            {
            printf("You took damage!\n\n");
            recieved++;
            break;
            }
            break;
        case(2):
            ok = 1;
            printf("Brutus is stunned!\n\n");
            do 
            {
                time(&end);
                elapsed = difftime(end, start);
                if(kbhit())
                {
                    if( getchar() == 'w')
                    {
                        printf("You dealt damage!\n\n");
                        dealt++;
                        ok = 0;
                        break;
                    }
                    else
                    {
                        break;
                    }
                }
            }
            while(elapsed < 3);
            if(ok)
            {
            printf("Too slow!\n\n");
            break;
            }
            break;
        case(3):
            ok = 1;
            printf("Brutus uses Blood Slam!\n\n");
            do 
            {
                time(&end);
                elapsed = difftime(end, start);
                if(kbhit())
                {
                    if( getchar() == 'e')
                    {
                        printf("Blocked!\n\n");
                        ok = 0;
                        break;
                    }
                    else
                    {
                        break;
                    }
                }
            }
            while(elapsed < 3);
            if(ok)
            {
            printf("You took damage!\n\n");
            recieved++;
            break;
            }
            break;
        }
    }
    if(recieved >= 3)
    {
        printf("Battle lost; you will now respawn in town.");
        getchar();
    }
    else
    {
        printf("Battle won!");
        getchar();
    }
}
int main()
{
    battle(2);
}
user3481786
  • 1
  • 1
  • 1
  • You should be using _kbhit(). kbhit has been deprecated since 1994. There is no equivalent in standard C. Have a look at http://cboard.cprogramming.com/c-programming/63166-kbhit-linux.html for the Linux equivalent. – cup Mar 31 '14 at 16:13

4 Answers4

2

You could write your own getch().

#include <termios.h>
#include <unistd.h>
#include <stdio.h>

/* reads from keypress, doesn't echo */
int getch(void)
{
    struct termios oldattr, newattr;
    int ch;
    tcgetattr( STDIN_FILENO, &oldattr );
    newattr = oldattr;
    newattr.c_lflag &= ~( ICANON | ECHO );
    tcsetattr( STDIN_FILENO, TCSANOW, &newattr );
    ch = getchar();
    tcsetattr( STDIN_FILENO, TCSANOW, &oldattr );
    return ch;
}
agent420
  • 3,291
  • 20
  • 27
1

Not really, since they can't be implemented on some implementations, and can only be implemented at great cost on others.

While not standard, there are portable libraries like ncurses which support these sort of operations.

James Kanze
  • 150,581
  • 18
  • 184
  • 329
0

There are two choices

  1. Find some cross-platform library that abstracts the details away for you (SDL might be good for this.)
  2. Do the abstracting yourself.

To do 2. you will have to define your own version of the input functions. You then create different implementation for each platform. This means your core code remains common across the various platforms.

doron
  • 27,972
  • 12
  • 65
  • 103
0

I think you need to use ncurses library.

here is similar question:Create a function to check for key press in unix using ncurses

Community
  • 1
  • 1
Sandro
  • 2,707
  • 2
  • 20
  • 30