Reputation: 31
EDIT 1:
I would like to have a program that checks for enter key pressed and then skip some code after it (kind of like some games that have the skippable screens when you launch them). But I don't want it to wait for the input.
if(getchar()=='\n')
{
goto skip;
}
ClearScreen();
printf("%s Welcome to Guy's game!\n\n");
Sleep(500);
ClearScreen();
printf("%s Welcome to Guy's game!\n\n");
Sleep(500);
ClearScreen();
printf("%s Welcome to Guy's game!\n\n");
Sleep(500);
ClearScreen();
printf("%s Welcome to Guy's game!\n\n");
Sleep(500);
ClearScreen();
//this is the where it should skip
skip:
printf("%s Welcome to Guy's game!\n\n");
printf("Please enter your name: ");
gets(name);
Sleep(250);
I want it to check for enter while it prints "Welcome to Guy's game!" so that no matter when I press enter (as long as it's printing "Welcome to Guy's game!") I can skip to the last part of the code. I can't figure out how to get this to work.
UPDATE:
I have one more question that I forgot to ask.. How can I fix "warning: implicit declaration of function 'Sleep', when I use:
ClearScreen();
printf("%s Welcome to Guy's game!\n\n");
Sleep(500);
inside a for loop.
Upvotes: 0
Views: 1851
Reputation: 31
So.. I finally solved it! I created a new program to try to find how to fix the one I'm currently working on.
(Code goes in [...] of
#include <stdio.h>
#include <conio.h>
#include <windows.h>
void ClearScreen(void)
{
system("cmd /c cls");
}
int main(void)
{
[...] //Everything is in here for next examples
}
for next examples)
For Any Letter:
int i=0;
char name[20];
while(1)
{
for(i=1; i>0&&i<5; i++)
{
//ClearScreen();
printf("%i", i);
printf(" Welcome to Guy's game!\n\n");
Sleep(500);
while (kbhit())
{
getch();
goto INPUT;
i=0;
}
}
goto INPUT;
}
INPUT:
ClearScreen();
//this is the where it should skip
printf(" Welcome to Guy's game!\n\n");
printf("Please enter your name: ");
gets(name);
Sleep(250);
printf("%s", name);
return 0;
For Only One Letter:
int i=0;
char name[20];
while(1)
{
for(i=1; i>0&&i<5; i++)
{
//ClearScreen();
printf("%i", i);
printf(" Welcome to Guy's game!\n\n");
Sleep(500);
while (kbhit()&&getch()== 'p')
{
goto INPUT;
i=0;
}
}
goto INPUT;
}
INPUT:
ClearScreen();
//this is the where it should skip
printf(" Welcome to Guy's game!\n\n");
printf("Please enter your name: ");
gets(name);
Sleep(250);
printf("%s", name);
return 0;
You may use this in your code if you like! Good luck, and thank you for all of your answers!
*BTW I made it show numbers in the loop so that it would prove that the program works, but you can take that part out because it is not needed.
Upvotes: 0
Reputation: 70909
You need to alter your terminal to not do canonical key processing.
All terminals hold information for just a little while to allow for things like the user hitting backspace (and the program not getting the erased character). This buffering of keystrokes is not sent to the program until a special "buffer flushing" key is pressed, like Enter or Return.
In a game this is undesirable, you want the keys as soon as you can get them. This means your program will requires the termios data structure, and then turn off (communicating to the terminal) the Canonical key processing. Note that this means you will have to handle backspace erasing in your program (should you desire it in other portions of your program, like entering in names to go with the high score).
// define a terminal configuration data structure
struct termios term;
// copy the stdin terminal configuration into term
tcgetattr( fileno(stdin), &term );
// turn off Canonical processing in term
term.c_lflag &= ~ICANON;
// set the terminal configuration for stdin according to term, now
tcsetattr( fileno(stdin), TCSANOW, &term);
Note that this only improves the response; but, really you still have small delays as the terminal communicates to the kernel, and the kernel then communicates to your program.
For example, the kernel actually captures the key down / key up events, and evaluates them within a short timer period to determine if a key needs to "autorepeat".
My recommendation is that you don't venture into writing this code yourself if you are interested in getting your game out quickly. Instead use a game programming library that interacts and configures your environment appropriately, like allegro.
However, if you are interested in how this works under the covers, by all means write your code to handle it (as it is quite a fascinating topic and you really will understand the terminal / program / kernel communications much better!).
Upvotes: 2
Reputation: 371
Start a thread that shows your message. Meanwhile, read the keyboard waiting for a hit. When the user hits enter, terminate the thread that shows the text and resume the normal flow jumping to the new screen.
Upvotes: 0