Hi Everyone,

I am Actually trying to make a simple C++ game in which some random alphabets will fall down and when we press one of those random alphabets keys the aphabet should disappear.

The problem that i am facing in making this code is that when i am using some function to read the key input that causes my program to wait for the user's input and hence my program comes to a stop till i press some key.

I want some logic/function which will stop the execution of my program.

Can you please suggest any such function or some logic that i can use in this program.

Please note my compiler is Microsoft Visual C++ 2010 and i am not that familiar with the functions in windows.h header file.

Thanks in advance.

Any pointers would be greatly appreciated.


My program is somewhat like this

/*Including Header Files*/
#define _CRT_SECURE_NO_DEPRECATE
# include <iostream>
# include <conio.h>
# include <Windows.h>

int i=0,y=1;
char words[5];
int pos[5];
char c;
void gotoxy(int x,int y);
void Movement();
int readkey(void);

void random()
{
int n;
for (int t=0;t<5;++t)
{
n= rand() % 26;
pos[t] = n % 2;
words[t] = (char)(n+65);
}
}

void main()
{
random();
for(y=0;y<10;y++)
{
for(i=0;i<5;++i)
{

gotoxy(3*i,pos);
printf("%c",words);
pos=pos+1;
if(y>0)
{
gotoxy(3*i,pos-2);
printf("%c",' ');
}
}
Sleep(1000);
}
getch();
}


void gotoxy(int x,int y)
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

I'm a little confused.
Before any more code is written, you should solidify your design.

Is the alphabet supposed to be at the top of the screen?
...or is the program going to cascade-down any character that is pressed?

How does the user win the game?

...AND, you're intending on doing this on the console, right?

I'm a little confused.
Before any more code is written, you should solidify your design.

Is the alphabet supposed to be at the top of the screen?
...or is the program going to cascade-down any character that is pressed?

How does the user win the game?

...AND, you're intending on doing this on the console, right?

Hi thines01,

Yes i am doing this on win32 console.

Yes the random alphabets are at the top of the screen and then they start falling down.( right now i am making them fall just 10 times ie y loop)

the user wins if he type all the random alphabet before a certain boundary( i haven't included that functionality yet in my program)

Actually i was not able to figure out how to make the program run in the background and also find a way in which i can read user input.

Like if you will try with some functions like getch etc,then the program stops till you enter a key and if this function is in a loop then the program stops after every input read and is surely not the way to go forward

What i need is a logic or function that can be placed inside a loop
say while(1){}

and which get activated when we press a key and then by using some conditional statement like switch

say switch(key)
{
case word:

break;
.
.
}

we can remove the input character.

I hope you got what i have in my mind.

once i have done till here,then we can complicate the game.:)

Btw do you have any idea/suggestions please do tell.

if(kbhit())
  {
    char ch = getch();
    if(ch=='A')
      cout << "You pressed A" << endl;
  }

This game is an amusing diversion. Like Ali 2101 said, kbhit() is the easiest way to check for a key press in compilers that support it. You can also fake kbhit() with the Win32 API, but that's not necessary in this case.

Here's a sample game that does something similar:

#include <iostream>
#include <random>
#include <string>
#include <cctype>
#include <ctime>
#include <conio.h>
#include <windows.h>

using namespace std;

namespace 
{
    void SetPos(int x,int y)
    {
        COORD coord = {(SHORT)x, (SHORT)y};
        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
    }
    
    void ShowAt(string const& s, int x, int y)
    {
        SetPos(x, y);
        cout << s << flush;
    }
    
    class GameChar
    {
    public:
        GameChar(char value, int x, int y): _value(value), _pos(x, y) {}
        
        int Value() const { return _value; }
        void Clear() const { cout << "\b "; }
        int Next() const {return _pos.Y + 1; }
        void Advance() { _pos.Y = Next(); }
        
        void Show() const
        {
            Clear();
            SetPos(_pos.X, _pos.Y);
            cout.put(_value);
        }
    private:
        struct ScreenCoord
        {
            ScreenCoord(int x, int y): X(x), Y(y) {}
            int X, Y;
        };
    
        char _value;
        ScreenCoord _pos;
    };
}

int main()
{
    string const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    int const SPEED = 500;
    int const MAX_X = 40;
    int const MAX_Y = 10;
    
    mt19937 rgen((unsigned)time(nullptr));
    int answers[2] = {0};
    bool done = false;
    
    ShowAt("Match the letters before they hit the bottom:", 0, 0);
    ShowAt(string(MAX_X, '-'), 0, MAX_Y);
    
    while (!done)
    {
        GameChar ch(alphabet[rgen() % alphabet.size()], rgen() % MAX_X, 1);
        
        for (; ch.Show(), ch.Next() <= MAX_Y; ch.Advance(), Sleep(SPEED))
        {
            if (kbhit())
            {
                ++answers[toupper(getch()) == toupper(ch.Value())];
                break;
            }
        }
        
        if (ch.Next() > MAX_Y)
        {
            done = true;
        }
    }
    
    ShowAt("The letter hit the bottom!\n", 0, MAX_Y + 1);
    cout << "\nRight: " << answers[1]
         << "\nWrong: " << answers[0] << endl;
}

@ Ali @decepkticon
Thank you guys khbit has solved my issue.Cheers..:)

But i seem to happen one more doubt Can i ask the doubt regarding this program here only or should i start a new thread.

Actually i also want to include a timer in my program..to keep a track of how many correct random alphabets the user enters in say 60 secs..

I can use a variable and assign it value 60.and then decrease it by 1,but how to make sure that after exactly one sec it would get displayed in the screen.
i.e i need to display the counter exactly after 1 sec and this should not affect my program other functionality .Is this possible, there must be a solution.Guys please provide some pointers/suggestions regarding the same.

Thanks
Jatin

In the general case you're getting into multithreading territory. Otherwise, you can just check the time every now and again to see if 60 seconds have passed. Even in my sample program, such a check wouldn't be more than half of a second off because of the Sleep() call.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.