Thread: Advance a loop without waiting for a key press but have the option to accept it.

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    4

    Advance a loop without waiting for a key press but have the option to accept it.

    This is the source code for a game where you catch 'o's with a U.
    It only makes the 'o's fall and generate every key press.
    I want to make it so that the function "void advance(input)" is activated WITHOUT a key press every 200 milliseconds but will accept input if a key is pressed.

    I am also planning on adding more stuff but this is a problem I really want to fix.

    Code:
    #include "stdafx.h"#include <iostream>
    #include <stdlib.h>
    #include <time.h>
    #include <stdio.h>
    #include <conio.h>
    #include <windows.h>
    #include <WinSock.h>
    #define LEFT_ARROW 75
    #define RIGHT_ARROW 77
    
    
    using namespace std;
    
    
    char grid[2][8];
    void advance(char input);
    int o_spawn;
    int score = 0;
    void o_fall();
    void wait(int);
    int count;
    char input;
    
    
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    
    
        char input = '\0';
        grid[0][0] = ' ';
        grid[1][0] = ' ';
    
    
        unsigned short int menu_choice = 3;
        while (menu_choice !=2)
        {
            // asks if you want to play
            cout << "Would you like to play?\n";
            cout << "1: YES\n";
            cout << "2: NO\n";
            cin >> menu_choice;
            
    
    
            // checks your answer
            while (menu_choice == 1)
            {
                
                // advances the os and generates them
                cout << "\n\n\n\n\n\n\n\n\n\n\n\n";
                // starts loop of typing the arrow key
                    do
                    {
                        input = getch();
                        //moves 'u' to the left
                        if (input == LEFT_ARROW)
                        {
                            grid [0][7] = 'U';
                            grid [1][7] = ' ';
                            advance(input);
                            input = getch();
                        }
                        // moves u to the right
                        else if (input == RIGHT_ARROW)
                        {
                            grid [0][7] = ' ';
                            grid [1][7] = 'U';
                            advance(input);
                            input = getch();
                        }
                        else if (input == 'q' || input == 'Q')
                        {
                            cout << "It seems you wan't to quit... okay.\nPress any button to quit.\n";
                            char quit;
                            quit = getch();
                            return 0;
                        }
                        else
                        {
                            advance(input);
                        }
                }while (input != 'q' || 'Q');
            }
        }
        return 0;
    }
    
    
    void advance(char input)
    {
            //makes 'o's generate randomly
            int change_o_space = rand() % 10 + 1;
            if ((change_o_space == 3) && (grid[0][0] == ' '))
            {
                grid[0][0] = 'o';
            }
            else if ((change_o_space == 6) && (grid[1][0] == ' '))
            {
                grid[1][0] = 'o';
            }
            //advances o to the bottom of screen
            o_fall();
            //prints the board
            cout << grid[0][0];
            cout << grid[1][0] << endl;
            cout << grid[0][1];
            cout << grid[1][1] << endl;
            cout << grid[0][2];
            cout << grid[1][2] << endl;
            cout << grid[0][3];
            cout << grid[1][3] << endl;
            cout << grid[0][4];
            cout << grid[1][4] << endl;
            cout << grid[0][5];
            cout << grid[1][5] << endl;
            cout << grid[0][6];
            cout << grid[1][6] << endl;
            cout << grid[0][7];
            cout << grid[1][7] << endl;
            cout << "SCORE:" << score;
            cout << "\n\n\n\n\n\n\n\n\n\n";
    }
    
    
    // makes 'o's fall and increments the score
    void o_fall()
    {
        // generates 'o's
        if (grid[0][6] == 'o' && grid[0][7] == 'U')
        {
            grid[0][6] = ' ';
            // increases score
            score++;
        }
        // generates 'o's
        if (grid[1][6] == 'o' && grid[1][7] == 'U')
        {
            grid[1][6] = ' ';
            // increases score
            score++;
        }
    
    
        //advances 'o's 
        if (grid[0][7] == 'o')
        {
            grid[0][7] = ' ';
        }
        if (grid[1][7] == 'o')
        {
            grid[1][7] = ' ';
        }
        if (grid[0][6] == 'o')
        {
            grid[0][6] = ' ';
            grid[0][7] = 'o';
        }
        if (grid[1][6] == 'o')
        {
            grid[1][6] = ' ';
            grid[1][7] = 'o';
        }
        if (grid[0][5] == 'o')
        {
            grid[0][5] = ' ';
            grid[0][6] = 'o';
        }
        if (grid[1][5] == 'o')
        {
            grid[1][5] = ' ';
            grid[1][6] = 'o';
        }
        if (grid[0][4] == 'o')
        {
            grid[0][4] = ' ';
            grid[0][5] = 'o';
        }
        if (grid[1][4] == 'o')
        {
            grid[1][4] = ' ';
            grid[1][5] = 'o';
        }
        if (grid[0][3] == 'o')
        {
            grid[0][3] = ' ';
            grid[0][4] = 'o';
        }
        if (grid[1][3] == 'o')
        {
            grid[1][3] = ' ';
            grid[1][4] = 'o';
        }
        if (grid[0][2] == 'o')
        {
            grid[0][2] = ' ';
            grid[0][3] = 'o';
        }
        if (grid[1][2] == 'o')
        {
            grid[1][2] = ' ';
            grid[1][3] = 'o';
        }
        if (grid[0][1] == 'o')
        {
            grid[0][1] = ' ';
            grid[0][2] = 'o';
        }
        if (grid[1][1] == 'o')
        {
            grid[1][1] = ' ';
            grid[1][2] = 'o';
        }
        if (grid[0][0] == 'o')
        {
            grid[0][0] = ' ';
            grid[0][1] = 'o';
        }
        if (grid[1][0] == 'o')
        {
            grid[1][0] = ' ';
            grid[1][1] = 'o';
        }
    }
    Sorry this was so long.
    Yes I know there are improvements and simplifications that can easily be made.
    This code was rushed though and I'm not very experienced in c++ nor am I familiar with most of the libraries.
    This is a console application compiled with Visual Studio on Windows 7 Professional 64 bit.

    So this is my question here:
    I want to make it so that the function "void advance(input)" is activated WITHOUT a key press every 200 milliseconds but will accept input if a key is pressed.
    Last edited by wazzupyall; 07-13-2012 at 08:27 AM.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Try _kbhit (CRT)
    Code:
    while (1) {
        if (_kbhit()) {
            int key = _getch();
            // ...
        }
        // ...
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 07-29-2010, 01:57 PM
  2. Loop while waiting for input?
    By Terran in forum C++ Programming
    Replies: 6
    Last Post: 05-22-2008, 08:32 PM
  3. need some help thanks in advance
    By Mshock in forum C Programming
    Replies: 7
    Last Post: 05-07-2006, 06:44 AM
  4. Wait loop - not busy waiting?
    By Foldager in forum Windows Programming
    Replies: 1
    Last Post: 07-17-2003, 01:39 AM
  5. while loop weirdness (does not accept compound statements?)
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 03-01-2002, 12:21 AM