0

I'm new to C++. Have decided to do my own game. And i want to make a starting screen for it. And the problem is that i havent found the way to make an "Press any key to continue" function while dots continue. I made the loop for the programm to wait till any would be pressed but dots dont want to display in.

#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <stdlib.h>
#include <windows.h>

using namespace std;


int pressCheck(){
    char c = 0;
    c = getchar();
    if (c == 0)
        return 1;
    return 0;
}

int main()
{
    cout << "\t\t\t\t  Hello" << endl;
    Sleep(300);
    cout << "\t\t     Welcome to my new game BITHCES!" << endl << endl;
    Sleep(700);
    cout << "\t\t\tPress any key to proceed";
    while(!pressCheck()){
        Sleep(300);
        cout << ".";
        Sleep(300);
        cout << ".";
        Sleep(300);
        cout << ".";
    }
    getchar();
    system("cls");
    Sleep(100);


        return 0;
}
Kaspar Siricenko
  • 237
  • 2
  • 18
  • 1
    There are many ways to do this. For example: 1) use a [signal handler](https://msdn.microsoft.com/en-us/library/xdkz3x12.aspx), 2) spawn a thread and a global variable (exit "while" loop when global becomes "true") – paulsm4 Aug 23 '15 at 23:43
  • 1
    Unfortunately, [this gets tricky pretty fast](http://stackoverflow.com/q/448944/335858). – Sergey Kalinichenko Aug 23 '15 at 23:43
  • I ve started doing game and got stcuk from the first function. Pretty fast :D – Kaspar Siricenko Aug 23 '15 at 23:49
  • 1
    You should consider using ncurses for more elaborate uses of the console. – Arthur Laks Aug 24 '15 at 01:25
  • 2
    If you want to do extensive screen formatting and keyboard control - all in text mode - then [ncurses](http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/) is absolutely your best bet. Otherwise, if you also want graphics, then you might want to look at [SDL](https://www.libsdl.org/). If you only want to "detect a keystroke in a while loop", I'd recommend "signal()" (as above). – paulsm4 Aug 24 '15 at 02:52

2 Answers2

1

If you are creating a text based game I would recommend using ncurses (or pdcurses for windows):

[...] a toolkit for developing "GUI-like" application software that runs under a terminal emulator.

Implementing what you have above would be something like

#include <string>
#include <ncurses.h>        // This header might be different on windows
#include <unistd.h>         // for usleep, replace with Windows.h (?)

void DisplayCentre(int yy, const std::string& str)
{
  // Get the screen size
  int y, x;
  getmaxyx(stdscr, y, x);

  // Compute starting location for string (centre)
  x = (x - str.size())/2;

  // Write the string to the window
  mvwprintw(stdscr, yy, x, str.c_str());

  // Make sure the screen is updated
  refresh();
}

void PromptForKey(void)
{
  // Get the screen size
  int y, x;
  getmaxyx(stdscr, y, x);

  // Write a message at the bottom left of the screen
  mvwprintw(stdscr, y-1, 0, "Press any key to continue");

  // Set a time-out for wgetch
  wtimeout(stdscr, 300);

  // While the user hasn't entered a character
  while (wgetch(stdscr) == ERR)
  {
    // Add another dot to the screen
    waddch(stdscr, '.');
    refresh();
  }

  // Clear time-out
  notimeout(stdscr, true);
}

int main(int argc, char** argv)
{
  initscr();           // Initialize curses
  cbreak();            // Make typed characters immediately available
  noecho();            // Don't automatically print typed characters
  curs_set(0);         // Make the cursor invisible (where supported)

  // Display `Hello' (at line 10)
  DisplayCentre(10, "Hello");

  // Delay (you might want to use Sleep())
  sleep(1);

  // Display `Welcome to my new game' (at line 15)
  DisplayCentre(15, "Welcome to my new game");
  sleep(1);

  // Prompt user for key
  PromptForKey();

  // Close down curses
  endwin();

  return 0;
}

To compile this program on Linux I use g++ test.cpp -lncurses. On windows you will probaly need to replace sleep with the windows Sleep function and use the appropriate header. You may also need to use an alternative to ncurses.

However, if you are just learning to program I would suggest you try using ncurses in Python. Python has the benefit of being an interpreted language so you don't need to worry too much about compiling or linking executables. Python is also mostly cross platform. The above implemented in Python:

#!/usr/bin/python

from curses import *
from time import sleep

def promptForKey(win):
  """ Ask the user to press any key to continue. """
  # Get screen size
  y,x = win.getmaxyx()

  # Display prompt
  win.addstr(y-1, 0, "Press any key to continue")
  win.refresh()

  # Set time-out
  win.timeout(300)

  while (win.getch() == ERR):
    win.addch('.')

  # Disable time-out
  win.notimeout(True)


def dispCentre(win, yy, string, delay):
  """ Display string at line yy and wait for delay milliseconds. """
  # Get screen size
  y,x = win.getmaxyx()

  # Display string in centre
  x = (x - len(string))/2
  win.addstr(yy, x, string)
  win.refresh()

  # Delay
  sleep(delay)

if __name__ == '__main__':

  # Initialize curses
  win = initscr()
  cbreak()
  noecho()
  curs_set(0)

  # Display some stuff
  dispCentre(win, 10, "Hello", 0.3)
  dispCentre(win, 15, "Welcome to my new game", 0.7)
  promptForKey(win)

  # Close down curses
  endwin()
ilent2
  • 5,171
  • 3
  • 21
  • 30
0

i know i am late but i think maybe you wanted to do this? (Run the attached code)

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <stdlib.h>
#include <windows.h>
#include<conio.h>

using namespace std;


bool pressCheck(){
    if (getch())
        return 1;
    return 0;
}

int main()
{
    cout << "\t\t\t\t  Hello" << endl;
    Sleep(300);
    cout << "\t\t     Welcome to my new game BITHCES!" << endl << endl;
    Sleep(700);
    cout << "\t\t\tPress any key to proceed";
    while(!pressCheck()){
        Sleep(300);
        cout << ".";
        Sleep(300);
        cout << ".";
        Sleep(300);
        cout << ".";
    }
    system("cls");
    Sleep(100);


    return 0;

}

Daniyal Shaikh
  • 419
  • 3
  • 12