如何在C++上独立运行函数

How to run the functions independently on C++

本文关键字:独立 运行 函数 C++      更新时间:2023-10-16

我正在尝试使用C++在控制台上制作游戏。目前,我主要有三个功能。

int main() {
...
while (!level1 && !player.dead) {
    drowing(l1_map);
    PlayerMovements(empty, l1_map);
    Enemy1Movements(enemy1, l1_map, lastloc);
    cls();
  }
}

第一个函数绘制地图:

void drowing(char l1_map[26][50]) {

    for (unsigned int i = 0; i < 26; i++) {
        cout << endl;
        for (unsigned int x = 0; x < 50; x++) {
            if (x == 0) {
                cout << " ";
            }

            //Draws the map and change colours;
            //player
            if (l1_map[i][x] == player.character) {
                if (l1_map[player.locX][player.locY] == l1_map[enemy1.locX][enemy1.locY]) {
                    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 252);
                    cout << l1_map[i][x];
                }
                if (l1_map[player.locX][player.locY] != l1_map[enemy1.locX][enemy1.locY]) {
                    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 250);
                    cout << l1_map[i][x];
                }
            }//wall
            else if (l1_map[i][x] == wall) {
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 128);
                cout << l1_map[i][x];
            }//enemy
            else if (l1_map[i][x] == enemy1.character) {
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 252);
                cout << l1_map[i][x];
            }//empty space
            else if (l1_map[i][x] == 32) {
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 509);
                cout << l1_map[i][x];
            }//key1
            else if (l1_map[i][x] == key1.character && !key1.picked) {
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 509);
                cout << l1_map[i][x];
            }//key2
            else if (l1_map[i][x] == key2.character && !key1.picked) {
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 253);
                cout << l1_map[i][x];
            }//key3
            else if (l1_map[i][x] == key3.character && !key1.picked) {
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 253);
                cout << l1_map[i][x];
            }//doors1
            else if (l1_map[i][x] == doors1.character) {
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 253);
                cout << l1_map[i][x];
            }//doors2
            else if (l1_map[i][x] == doors2.character) {
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 240);
                cout << l1_map[i][x];
            }//doors3
            else if (l1_map[i][x] == doors3.character) {
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 240);
                cout << l1_map[i][x];
            }//doors4
            else if (l1_map[i][x] == doors3.character) {
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 240);
                cout << l1_map[i][x];
            }//exit
            else if (l1_map[i][x] == get_out1.character && !get_out1.open) {
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 139);
                cout << l1_map[i][x];
            }
            else
            {
                cout << l1_map[i][x];
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
            }

        }
    }
}

第二个功能允许玩家使用键盘箭头在地图上移动:

void PlayerMovements(char& empty, char l1_map[26][50]) {

    char awall = 219;
    if (_kbhit()) {

        switch ((_getch())) { //what button is pressed;

                              // move up
        case 72:
            if (l1_map[player.locX - 1][player.locY] != wall && l1_map[player.locX - 1][player.locY] != '[') {
                player.locX--;

                if (l1_map[player.locX + 1][player.locY] == player.character) {
                    l1_map[player.locX + 1][player.locY] = empty;
                }
                if (key3.locX == player.locX && key3.locY == player.locY) {
                    l1_map[key3.locX][key3.locY] = ' ';
                }
            }

            break;
            //move down
        case 80:

            if (l1_map[player.locX + 1][player.locY] != wall && l1_map[player.locX + 1][player.locY] != '[') {
                player.locX++;
                if (l1_map[player.locX - 1][player.locY] == player.character) {
                    l1_map[player.locX - 1][player.locY] = empty;
                }

            }
            break;
        case 75:
            //left
            if (l1_map[player.locX][player.locY - 1] != wall  && l1_map[player.locX][player.locY - 1] != '[') {
                player.locY--;

                if (l1_map[player.locX][player.locY + 1] == player.character) {

                    l1_map[player.locX][player.locY + 1] = empty;
                }
            }

            break;

        case 77: // player moves right
            if (l1_map[player.locX][player.locY + 1] != wall && l1_map[player.locX][player.locY + 1] != '[') {
                player.locY++;
                if (l1_map[player.locX][player.locY - 1] = player.character) {
                    l1_map[player.locX][player.locY - 1] = empty;
                }

            }
            break;
        }
    }
}

第三个功能是上下移动的敌人:

int Enemy1Movements(enemy& enemy1, char l1_map[26][50], char& lastloc) {
    //this_thread::sleep_for(chrono::milliseconds(500));
    char empty = ' ';
    bool ValidUp = false;
    bool ValidDown = false;
    bool ValidLeft = false;
    bool ValidRight = false;

    if (l1_map[enemy1.locX + 1][enemy1.locY] != wall && l1_map[enemy1.locX + 1][enemy1.locY] != '-') {
        ValidDown = true;
    }
    if (l1_map[enemy1.locX - 1][enemy1.locY] != wall && l1_map[enemy1.locX - 1][enemy1.locY] != '-') {
        ValidUp = true;
    }
    if (l1_map[enemy1.locX][enemy1.locY - 1] != wall && l1_map[enemy1.locX - 1][enemy1.locY] != '-') {
        ValidLeft = true;
    }
    if (l1_map[enemy1.locX][enemy1.locY + 1] != wall && l1_map[enemy1.locX + 1][enemy1.locY] != '-') {
        ValidRight = true;
    }

    ////enemy move up and down
    //////////////////////////

    if (lastloc != 'u' && ValidDown) {

        enemy1.locX++;
        lastloc = 'd';


        if (l1_map[enemy1.locX - 1][enemy1.locY] == enemy1.character) {
            l1_map[enemy1.locX - 1][enemy1.locY] = 32;
        }
    }
    else if (lastloc == 'd' && ValidUp) {
        enemy1.locX--;
        lastloc = 'u';

        if (l1_map[enemy1.locX + 1][enemy1.locY] == enemy1.character) {
            l1_map[enemy1.locX + 1][enemy1.locY] = 32;
        }

    }
    else {
        if (ValidUp) {
            enemy1.locX--;
            lastloc = 'u';

            if (l1_map[enemy1.locX + 1][enemy1.locY] == enemy1.character) {
                l1_map[enemy1.locX + 1][enemy1.locY] = 32;
            }
        }
        else {
            enemy1.locX++;
            lastloc = 'd';
            if (l1_map[enemy1.locX - 1][enemy1.locY] == enemy1.character) {
                l1_map[enemy1.locX - 1][enemy1.locY] = 32;
            }

        }
    }

    return NULL;

}

我正在努力实现的目标:

我需要减慢EnemyMovemens()功能,因为敌人会快速移动。或者使PlayerMovements()功能独立于其他功能。

问题:

我尝试使用线程。像this_thread::sleep_for()Sleep()这样的功能。将它们放入EnemyMovements()功能中。在这种情况下,它会减慢所有应用程序的速度,并且我的播放器移动速度非常慢。

我不需要代码,因为我喜欢自己学习。我只需要一个指导,现在该往哪个方向移动。

操作系统:赢10

基本问题是游戏循环中没有任何计时,因此一切都以相同的速度发生。 网上有一些很好的文章讨论了游戏计时循环,例如:http://www.koonsolo.com/news/dewitters-gameloop/引导您完成从您现在使用的方法到将游戏更新计时与显示计时分开的更好方法的步骤。

这篇文章没有告诉你如何减慢敌人相对于你的玩家的速度 - 一种方法是有一个 enemySpeed 变量并使用类似的东西:

// outside main loop
int enemySlowness = 20;  // adjust this as needed
int enemyMoveTimer = 0;
...
if (++enemyMoveTimer > enemySlowness)
{
    Enemy1Movements(enemy1, l1_map, lastloc);
    enemyMoveTime = 0;
}

这种方法存在问题,但再加上该文章中的一些想法应该可以让您走上正确的轨道。