c++中的半秒计时器

Half second timer in c++

本文关键字:半秒 计时器 c++      更新时间:2023-10-16

我想制作一个等待半秒的计时器,我已经搜索了很长时间,但只能找到等待一整秒的计时器这是整个程序:

//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 11nn";
    //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 << "nnnnnnnnnn                              You died on level " << Level << "nn                             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;

我想让这个星号在移动前等待半秒

从c++ 11开始,你可以做

#include <thread>
#include <chrono>
std::this_thread::sleep_for(std::chrono::milliseconds(500));

this_thread是引用当前运行线程的函数的命名空间。其中一个函数是sleep_for

对于情况