-1

I want to make a timer that waits half a second I have searched for along time but can only find timers that wait a whole second this is the whole program:

//Draw map
char Map11[10][50] = {"#################################################",
                      "################H       @       H################",
                      "#         K###########     ###########K         #",
                      "# * #########                       ######### * #",
                      "#                                               #",
                      "#                                               #",
                      "#                                               #",
                      "#                                               #",
                      "############################################*  !#",
                      "#################################################" };
while(stopgame == false && Level == 11)
{
    //Removes everything on the screen
    system("cls");
    //Print
    cout << "Well done you made it to level 11\n\n";

    //Prints the map
    for (int y = 0; y < 10; y++)
    {
        cout << Map11[y] << endl;
    }

    //Tells you Hp
    cout << "Hp: "<< Hp << "/" << MaxHp << endl;

    for (int y = 0; y<10; y++)
    {
        for(int x = 0; x<50; x++)
        {

            switch(Map11[y][x])
            {
                //Sets the # sign to look like a line
                case '#':
                {
                   Map11[y][x] = 221;
                }
                break;
                //Star moving
                case '*':
                    {
                        if (y == 3 && x == 2 && Direction1 == 1)
                        {
                            Map11[y][x] = ' ';
                            x -= 1;
                            Map11[y][x] = '*';
                        }
                        else if (y == 3 && x == 1)
                        {
                            Map11[y][x] = ' ';
                            x += 1;
                            Direction1 = 0;
                            Map11[y][x] = '*';
                        }
                        else if (y == 3 && x == 2 && Direction1 == 0)
                        {
                            Map11[y][x] = ' ';
                            x += 1;
                            Map11[y][x] = '*';
                        }
                        else if (y == 3 && x == 3)
                        {
                            Map11[y][x] = ' ';
                            x -= 1;
                            Direction1 = 1;
                            Map11[y][x] = '*';
                        }

                    }
                    break;
                case '@':
                {


                    //When up arrow pressed @ sign move up
                    if (GetAsyncKeyState(VK_UP) != 0)
                    {
                        int y4 = (y-1);

                        switch(Map11[y4][x])
                        {
                            case ' ':
                            {
                                Map11[y][x] = ' ';
                                y -= 1;
                                Map11[y4][x] = '@';
                                yPos = y;
                                xPos = x;
                            }break;
                            //When touch ! sign set level to next level
                            case '!':
                                {
                                    Level = 11;
                                }break;
                            case 'H':
                                {
                                    if (Hp < 100)
                                    {
                                        Hp += 20;
                                    }
                                    Map11[y][x] = ' ';
                                    y -= 1;
                                    Map11[y][x] = '@';
                                }break;
                                //When touch star subtract 20 hp and remove star
                                case '*':
                                {
                                    Hp -= 20;
                                    Map11[y][x] = ' ';
                                    y -= 1;
                                    Map11[y4][x] = '@';
                                }break;
                        }

                    }

                    //When down arrow pressed @ sign move down
                    if (GetAsyncKeyState(VK_DOWN) != 0)
                    {
                        int y4 = (y + 1);

                        switch(Map11[y4][x])
                        {
                        case ' ':
                            {
                                Map11[y][x] = ' ';
                                y += 1;
                                Map11[y4][x] = '@';
                                yPos = y;
                                xPos = x;
                            }break;
                        //When touch ! sign set level to next level
                        case '!':
                            {
                                Level = 11;
                            }break;
                            case 'H':
                                {
                                    if (Hp < 100)
                                    {
                                        Hp += 20;
                                    }
                                    Map11[y][x] = ' ';
                                    y += 1;
                                    Map11[y][x] = '@';
                                }break;
                            //When touch star subtract 20 hp and remove star
                            case '*':
                                {
                                    Hp -= 20;
                                    Map11[y][x] = ' ';
                                    y += 1;
                                    Map11[y4][x] = '@';
                                }break;
                        }
                   }

                   //When right arrow pressed @ sign move right
                   if (GetAsyncKeyState(VK_RIGHT) != 0)
                   {
                       int x5 = (x + 1);

                       switch(Map11[y][x5])
                       {
                       case ' ':
                        {
                            Map11[y][x] = ' ';
                            x += 1;
                            Map11[y][x5] = '@';
                            yPos = y;
                            xPos = x;
                        }break;
                       //When touch ! sign set level to next level
                       case '!':
                        {
                            Level = 11;
                        }break;
                        case 'H':
                                {
                                    if (Hp < 100)
                                    {
                                        Hp += 20;
                                    }
                                    Map11[y][x] = ' ';
                                    x +=1;
                                    Map11[y][x] = '@';
                                }break;
                        //When touch star subtract 20 hp and remove star
                        case '*':
                                {
                                    Hp -= 20;
                                    Map11[y][x] = ' ';
                                    x += 1;
                                    Map11[y][x5] = '@';
                                }break;
                       }
                   }

                   //When left arrow pressed @ sign move left
                   if (GetAsyncKeyState(VK_LEFT) != 0)
                   {
                       int x5 = (x - 1);

                       switch(Map11[y][x5])
                       {
                       case ' ':
                        {
                            Map11[y][x] = ' ';
                            x -= 1;
                            Map11[y][x5] = '@';
                            yPos = y;
                            xPos = x;
                        }break;
                       //When touch ! sign set level to next level
                       case '!':
                        {
                            Level = 11;
                        }break;
                        case 'H':
                                {
                                    if (Hp < 100)
                                    {
                                        Hp += 20;
                                    }
                                    Map11[y][x] = ' ';
                                    x -= 1;
                                    Map11[y][x] = '@';
                                }break;
                        //When touch star subtract 20 hp and remove star
                        case '*':
                                {
                                    Hp -= 20;
                                    Map11[y][x] = ' ';
                                    x -= 1;
                                    Map11[y][x5] = '@';
                                }break;
                       }
                   }
                }
            }
        }
    }
    Sleep(Gamespeed);
    while(Hp == 0)
        {
            system("cls");
            cout << "\n\n\n\n\n\n\n\n\n\n                              You died on level " << Level << "\n\n                             Better luck next time.";
        }

}

This is the part where the star moves

case '*':
                    {
                        if (y == 3 && x == 2 && Direction1 == 1)
                        {
                            Map11[y][x] = ' ';
                            x -= 1;
                            Map11[y][x] = '*';
                        }
                        else if (y == 3 && x == 1)
                        {
                            Map11[y][x] = ' ';
                            x += 1;
                            Direction1 = 0;
                            Map11[y][x] = '*';
                        }
                        else if (y == 3 && x == 2 && Direction1 == 0)
                        {
                            Map11[y][x] = ' ';
                            x += 1;
                            Map11[y][x] = '*';
                        }
                        else if (y == 3 && x == 3)
                        {
                            Map11[y][x] = ' ';
                            x -= 1;
                            Direction1 = 1;
                            Map11[y][x] = '*';
                        }

                    }
                    break;

I want to make this star wait half a second before it moves

Matthew
  • 1
  • 5
  • Did you try `sleep()`? – Thomas Ayoub Oct 21 '15 at 09:02
  • I did try sleep but that stops the whole program I still need the whole program running while the star isnt – Matthew Oct 21 '15 at 09:16
  • directly use `sleep(500);` in the place where you want to stop the program for 30 seconds. – The Apache Oct 21 '15 at 09:46
  • What was wrong with answer you received when you asked the same thing almost a year ago? http://stackoverflow.com/questions/26943282/sleep-is-sleeping-the-whole-programme . You were even given a way to create a proper gameloop instead of using a blocking timer – Taus Oct 21 '15 at 10:43
  • @Taus I do use that in my game but I can't make that wait for half a second it is only for a full second – Matthew Oct 22 '15 at 06:04
  • @Taus I have experimented around with the other timer more and have found a way to make it wait half a second thanks for all your help – Matthew Oct 22 '15 at 06:41

1 Answers1

3

Since C++11, you can do

#include <thread>
#include <chrono>

std::this_thread::sleep_for(std::chrono::milliseconds(500));

this_thread is a namespace for functions referring to the currently running thread. One of those functions is sleep_for.

For the case < C++11, you might want to look here.

Community
  • 1
  • 1
cadaniluk
  • 15,027
  • 2
  • 39
  • 67
  • 2
    @Matthew No. That's your part. I think I even partially crossed the line when I provided this code to you instead of telling you to research properly. – cadaniluk Oct 21 '15 at 09:21
  • @Matthew I added something to my answer. – cadaniluk Oct 21 '15 at 09:28
  • @Matthew If you have chosen the best answer, accept it. I don't say this because I'm a rep-who*e but because it marks this problem as solved and helps keep the state of this site clean. – cadaniluk Oct 22 '15 at 14:55
  • I have tried this in a new blank application and it is still saying 'std::this_thread' has not been declared and from what I have seen by looking around more what I have should work and I have -std=c++11 enabled in my compiler – Matthew Oct 23 '15 at 07:59
  • I have looked around more and I have found that MingGW does not support std::thread so if you could recommend a compiler that does support this that would be great – Matthew Oct 23 '15 at 08:04
  • @Matthew Did you specify the `-std=c++0x` or `-std=c++11` option when compiling with MinGW? Anyway, GCC 5 supports it but that's for GNU systems like Linux. The Visual Studio 2015 IDE supports C++11, former versions do only partially. – cadaniluk Oct 23 '15 at 08:11
  • I specified -std=c++11 – Matthew Oct 23 '15 at 08:12
  • I am currently using code blocks should I use visual studio 2015 instead – Matthew Oct 23 '15 at 08:13
  • @Matthew Yes, you could, but try `-std=c++0x` first. I'm pretty sure you *can* use C++11 with MinGW, though. – cadaniluk Oct 23 '15 at 08:14
  • When I enable -std=c++0x instead it still says the same message – Matthew Oct 23 '15 at 11:48
  • I am using version 4.7.1 MinGW as my compiler and am using -std=c++11 standards. I still get and error saying 'std::this_thread' has not been declared and I have included . When I upgrade to MinGW 5.14 I get an error saying unrecognized command line option "-std=c++11". – Matthew Oct 24 '15 at 03:49
  • I have managed to get this working in visual studio 2015 but `std::this_thread::sleep_for(std::chrono::milliseconds(100));` is functioning just like `Sleep(100)` and stopping the whole programme. – Matthew Oct 24 '15 at 22:24
  • @Matthew Open a new question or search on your own, please. Otherwise the comment box'll explode soon. – cadaniluk Oct 25 '15 at 06:13