c ++中的半秒计时器

时间:2015-10-21 08:58:56

标签: c++

我想制作一个等待半秒钟的计时器我一直在搜索但是只能找到等待一秒钟的计时器 这是整个计划:

//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.";
        }

}

这是明星移动的部分

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;

我想让这位明星在移动之前等待半秒

1 个答案:

答案 0 :(得分:3)

从C ++ 11开始,你可以做到

#include <thread>
#include <chrono>

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

this_thread是引用当前正在运行的线程的函数的命名空间。其中一项功能是sleep_for

对于案件&lt; C ++ 11,您可能希望查看here