I don't really know how to type the Title so I came up with this. So basically I am trying to make some sort of a snake in C. But I don't know how to make the controls. Lets say you have pressed left arrow and the snake has to move left till another key is pressed but I can't come up with the code for that its always stopping and waiting for me to press the key.
I was thinking for something like this:
for ( i = 0; i < 10 ; i++ ) {
if( next_move = getch() ) break;
else Sleep(10);
}
if(!next_move)
next_move = prev_move;
if( move ( next_move, bite_cord ) ) {
prev_move = next_move;
next_move = 0;
Sleep(300);
system("CLS");
printf("You lost. ");
}
My idea was to wait one second and if theres no key pressed to continue with the previous one but if its pressed to save the pressed now.
Do you have any ideas of how to do it?
You can use kbhit()
The code would be something like
if (kbhit())
input = getch(); //Or something like that
Related
I have a simple piece of code that detects keystrokes using ncurses.
As I understand, 1 keypress pushes 3 values into the buffer. Where the 3rd value differentiates between arrow keys for example.
So when pressing any of the 4 arrows on the keyboard, the first 2 values pushed into the buffer should be similar \033, and [. But the 3rd value is unique to the arrow (A for up, B for down, C for right or D for left). Therefore, when mapping actions to keystrokes, we're depending on the 3rd value.
When trying to assess which arrow was pressed I tried both an if else ladder and a switch. The if else ladder works perfectly. But the switch seems to fire multiple cases on each keypress.
Here's the working code (if else) -
char first = getch(); //returns \033
char second = getch(); //returns [
char third = getch(); // returns A or B or C or D
if(third == 'A'){
printf("Up Arrow Pressed");
}
else if(third == 'B'){
printf("Down Arrow Pressed");
}
else if(third == 'C'){
printf("Right Arrow Pressed");
}
else if(third == 'D'){
printf("Left Arrow Pressed");
}
Here's the code that isn't working (Case) -
char first = getch(); //returns \033
char second = getch(); //returns [
char third = getch(); // returns A or B or C or D
switch(third){
case('B'):
printf("\nDOWN");
case('C'):
printf("\nRIGHT");
case('A'):
printf("\nUP");
case('D'):
printf("\nLEFT");
default:
printf("default");
}
This is the output when cases are used and I press the down key:
if you don't break; at the end of a case the next case is executed.
If You Don't Use break at the end of each case then the cases that you write after the true case will also be executed.
Use "break; " (same as in the code provided as image) as last statement in each case so that it prevents execution of followed cases.!
(https://i.stack.imgur.com/up2pG.png)
I'm simulating a tv game in a C prompt program. The player has 60 seconds to guess a word, and when he finds it, he has to press enter to get a new one: the word changes, and the number of words and the time left upgrade.
Because the "enter listener" part is a getchar, I was guessing if it could be possible to do a real-time upgrading of the time left second by second, while waiting for the enter pressing with getchar().
while(1) {
system("clear"); //RAND WORD
parola = parole[rand() % n]; //PRINT WORDS, NEW WORD, SECONDS LEFT
printf("\n\n[%d]\t\t%s\t\t%d", indovinate, parola, secLeft);
gettimeofday(&initTime, NULL);
int initSec = initTime.tv_sec; //WAIT FOR PAYLER TO PRESS ENTER
getchar();
gettimeofday(&tookTime, NULL);
int tookSec = tookTime.tv_sec - initSec; //UPGRADE TIME TOOK
secLeft -= tookSec;
try kbhit(),
It's part of the conio.h library.
It basically checks if a key is pressed. If it isn't you can update the time. if it is you can input the answer.
if(kbhit()){
a = getch();
}
else{
//update time
}
you might want to use getch(), it doesn't need the player to press enter.
There might be better ways to do this than c. (Don't ask me, I don't do stuff like this)
But if It's just a cool project, go ahead.
you can use delay of dos.h with the kbhit() of conio.h
the delay(time) take integer as time to pause the program from execution for that 'time' amount
int i=0;
while (i<60||!kbhit()) // wait till i reach value 60 or key is pressed
{//doing the stuff
i++;}
if (i==60)
printf("\n sorry but the time out you may try next time");```
I'm reading a book. The author explains the function clearly. The problem is that my program doesn't detect any key that is pressed, so I can't terminate the window or stop a loop. This is the part of the problem.
while(1)
{
frame = cvQueryFrame( capture );
if ( !frame )
break;
cvShowImage("Example2", frame);
char c = cvWaitKey(33);
if ( c == 27 )
{
printf("Yes");
break; // 27 == escape button
}
}
I'm using Mac (I'm running the program from the terminal).
There might be 2 possibilities ,
The focus is not on the cv window when you press the key.
Try using just, cvWaitKey().
I'm coding a snake game in C and my compiler is turbo C.
I've got a problem with moving the snake. I need a function or a way of coding so that I can move my snake without the keyboard waiting for a key to be pressed. but if a key is pressed some specific actions are to be done. I used the structure below but it doesn't work as I explained above:
while (gameStatus == CONTINUE)
{
if(kbhit)
{
char c = getch();
.....
}
.....
}
can anyone help?
kbhit is a function. When you write if(kbhit) you are testing if the function exists. Instead call the function by writing if(kbhit()).
while (gameStatus == CONTINUE)
{
if (kbhit())
{
char c = '\0';
/*a keystroke return 0*/
if ((c=getch())!=0)
{
c=getch(); /*then you capture the actual directional key*/
......conditionals
}
//else if not was a directional, you Can Pause or Something
.....
}
.....
}
Hey all I've asked this question a few times in the past few days but I just don't get it...I basically want to have the while loop for the Beep command executed in the background while the user can interact with the available case statements (only one shown..there are others)....i keep getting blocked and everytime i want the beep to make a sound constantly i block the rest of my program...I have to use Beep so please don't suggest any other functionality..
here's a sample code...
while( keypress != 'q' || keypress != 'Q')
{
x = Beep(x);
while (x == 1)
Beep(350,300);
alarm_t current;
keypress = _getch();
switch(keypress){
case 'h':
sprintf_s(current.message,"high alarm");
current.timeOfEvent = time(NULL);
recordEvent(current);
break;
Now...my issue is with the while loop and the Beep command....here is what i call to Beep(x)
int Beep(int y)
{
return y;
}
So basically i am trying to call a function outside of my current cpp file to just compare x and y, and return y as being equivalent to x...i thought this might avoid blocking but it doesn't...
Your while loop around beep just won't work and _getch is blocking. So it will just block until there's a character.
Depending what platform you are on, you need something like kbhit (and if you google that you will find alternatives for other platforms). ie it's not standard C functionality and platform specific.
kbhit will return true or false depending if there is a character or not.
So you can do:
while(!key_is_quit(ch))
{
Beep();
if(kbhit())
{
ch = getch();
// switch....
}
}