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

时间:2016-02-07 00:11:14

标签: c++

我正在尝试使用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

1 个答案:

答案 0 :(得分:3)

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

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

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    //Toast.makeText(getActivity(),""+resultCode+"", Toast.LENGTH_SHORT).show();
    if (resultCode == RESULT_OK) {
        if (requestCode == PICK_FROM_CAMERA) {
            Bundle extras = data.getExtras();
            if (extras != null) {
                Bitmap imageBitmap = (Bitmap) extras.get("data");

               /*Picasso.with(this)
                        .load()
                        .transform(new CircleTransform())
                        .into(imageProfile);*/

                if(statusOnUpload == 1){
                    ivImage1.setImageBitmap(imageBitmap);
                } else if(statusOnUpload == 2){
                    ivImage2.setImageBitmap(imageBitmap);
                }else if(statusOnUpload == 3){
                    ivImage3.setImageBitmap(imageBitmap);
                }else{
                    ivImage4.setImageBitmap(imageBitmap);
                }

                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                imageBitmap.compress(Bitmap.CompressFormat.JPEG, 80, baos);
                byte[] b = baos.toByteArray();

                if (statusOnUpload == 1) {
                    encodedImageString1 = Base64.encodeToString(b, Base64.NO_WRAP);
                    Log.d(TAG, encodedImageString1.toString());
                } else if (statusOnUpload == 2) {
                    encodedImageString2 = Base64.encodeToString(b, Base64.NO_WRAP);
                } else if (statusOnUpload == 3) {
                    encodedImageString3 = Base64.encodeToString(b, Base64.NO_WRAP);
                } else if (statusOnUpload == 4) {
                    encodedImageString4 = Base64.encodeToString(b, Base64.NO_WRAP);
                }

                //Log.i("")

                //Toast.makeText(getApplicationContext(),""+encodedImageString+"",Toast.LENGTH_SHORT).show();
                //DialogDeal dialogDeal=new DialogDeal(EditProfileActivity.this,"imageBase64",encodedImageString,"Cancel");
                //dialogDeal.show();

            } else {
                //LogManager.logI("extras == null");
            }
        } else if (requestCode == PICK_FROM_GALLERY) {
            mImageCaptureUri = data.getData();
            doCrop();
        }
    }
}

这种方法存在问题,但加上该文章的一些想法可以让你走上正轨。