首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在c++游戏中添加分屏多人游戏

在c++游戏中添加分屏多人游戏
EN

Stack Overflow用户
提问于 2013-08-10 22:54:24
回答 1查看 1K关注 0票数 0

我正在用libnds为c++中的NDS编写代码,但这个问题不是针对NDS的。我现在有一个基于文本的游戏,在这个游戏中,顶部的屏幕只显示一个徽标,而你则在底部的屏幕上播放。

因此,我想添加一种类型的单DS多人游戏,其中一个播放在顶部的屏幕,另一个在底部。我在设置两个屏幕的文本引擎方面没有问题,我只需要找到一种在多人游戏中高效编码的方法。下面我写了一个摘要或简化的版本。

注意: consoleClear()清除屏幕,游戏停止的唯一地点是att暂停函数。

代码语言:javascript
复制
//Headers

void display(int x,int y,const char* output))
{
    printf("\x1b[%d;%dH%s", y, x,output);
}

void pause(KEYPAD_BITS key) //KEYPAD_BITS is an ENUM for a key on the NDS
{
    scanKeys();
    while (keysHeld() & key)
    {
        scanKeys();
        swiWaitForVBlank();
    }
    while (!(keysHeld() & key))
    {
        scanKeys();
        swiWaitForVBlank();
    }
    return;
}

void pause() //Only used to simplify coding
{
    pause(KEY_A);
    return;
}

int main(void)
{
    //Initializations/Setup
    while (1)
    {
        if (rand()%2==1) //Say Hello
        {
            if (rand()%3!=1) //To Friend (greater chance of friend than enemy)
            {
                display(6,7,"Hello Friend!");
                display(6,8,"Good greetings to you.");
                pause();
                consoleClear(); //Clears text
                display(6,7,"Would you like to come in?");
                pause();
                //Normally more complex complex code (such as interactions with inventories) would go here
            }
            else //To enemy
            {
                display(6,7,"Hello enemy!");
                display(6,8,"I hate you!");
                pause();
                consoleClear();
                display(6,7,"Leave my house right now!!!");
                pause();
            }
        }
        else //Say goodbye
        {
            if (rand()%4==1) //To Friend (lesser chance of friend than enemy)
            {
                display(6,7,"Goodbye Friend!");
                display(6,8,"Good wishes to you.");
                pause();
                consoleClear();
                display(6,7,"I'll see you tomorrow.");
                pause();
                consoleClear();
                display(6,7,"Wait, I forgot to give you this present.");
                pause();
            }
            else //To enemy
            {
                display(6,7,"Goodbye enemy!");
                display(6,8,"I hate you!");
                pause();
                consoleClear();
                display(6,7,"Never come back!!");
                pause();
                consoleClear();
                display(6,7,"Good riddance!"); //I think I spelt that wrong...
                pause();
            }
        }
    }
}

我知道gotos令人困惑,可以被认为是一个坏习惯,但我想不出更好的方法了。我的集成多人游戏的版本:

代码语言:javascript
复制
//Headers and same functions

int game(int location)
{
    switch (location)
    {
    case 1: goto one; break;
    case 2: goto two; break;
    case 3: goto three; break;
    case 4: goto four; break;
    case 5: goto five; break;
    case 6: goto six; break;
    case 7: goto seven; break;
    case 8: goto eight; break;
    case 9: goto nine; break;
    case 10: goto ten; break;
    default: break;
    }

    if (rand()%2==1) //Say Hello
    {
        if (rand()%3!=1) //To Friend (greater chance of friend than enemy)
        {
            display(6,7,"Hello Friend!");
            display(6,8,"Good greetings to you.");
            return 1;
one:;
            consoleClear(); //Clears text
            display(6,7,"Would you like to come in?");
            return 2;
two:;
            //Normally more complex complex code (such as interactions with inventories) would go here
        }
        else //To enemy
        {
            display(6,7,"Hello enemy!");
            display(6,8,"I hate you!");
            return 3;
three:;
            consoleClear();
            display(6,7,"Leave my house right now!!!");
            return 4;
four:;
        }
    }
    else //Say goodbye
    {
        if (rand()%4==1) //To Friend (lesser chance of friend than enemy)
        {
            display(6,7,"Goodbye Friend!");
            display(6,8,"Good wishes to you.");
            return 5;
five:;
            consoleClear();
            display(6,7,"I'll see you tomorrow.");
            return 6;
six:;
            consoleClear();
            display(6,7,"Wait, I forgot to give you this present.");
            return 7;
seven:;
        }
        else //To enemy
        {
            display(6,7,"Goodbye enemy!");
            display(6,8,"I hate you!");
            return 8;
eight:;
            consoleClear();
            display(6,7,"Never come back!!");
            return 9;
nine:;
            consoleClear();
            display(6,7,"Good riddance!"); //I think I spelt that wrong...
            return 10;
ten:;
        }
        return -1;
    }
}
int main(void)
{
    //Initializations/Setup
    int location1 = -1, location2 = -1;
    location1 = game(location1);
    location2 = game(location2);
    while (1)
    {
        scanKeys(); //Whenever checking key state this must be called
        if (keysDown() & KEY_A) //A key is used to continue for player1
            location1 = game(location1);
        if (keysDown() & KEY_DOWN) //Down key is used to continue for player2
            location2 = game(location2);
    }
}

除了这种方法是一种糟糕的实践之外,在实际的源代码中,我需要添加数百个gotos,这太费时了。

任何帮助都是非常感谢的。如果任何人有任何问题或回答,请问/回答。

编辑:虽然它不喜欢这样做,我愿意重写游戏从头开始,如果有人有这样做的方法。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-08-12 00:57:50

为每一种情况使用if- each条件语句是首先想到的一个简单解决方案。

例如:

代码语言:javascript
复制
int game(int i){
  if(i == 1){
    //first case code here.
  }
  else if(i == 2){
    //second case code here.
  }
  //....
  return 0;
}

在每种情况下,代码甚至可以放在将根据每个条件调用的其他函数中。这可能对你的案子来说就足够了。

一个更灵活的解决方案(但要复杂得多)是一个调度表。这样做的想法是有单独的函数和每个所需的功能,并将它们的指针放在一个数组中。然后,您可以通过使用这些函数指针对表进行索引来调用它们。如果您需要执行一系列的执行(函数调用),并且希望轻松地设置它,或者您希望根据您的输入获得不同的结果,而不需要更改您的程序,这将非常有帮助。

下面是一个例子。如果用printf替换std::cout,用stdio库替换iostream,则C中也可以使用此代码。

代码语言:javascript
复制
#include <iostream>

using namespace std;

// Arrays start from 0.
// This is used for code
// readability reasons.
#define CASE(X) X-1 

typedef void (*chooseCase)();

// Functions to execute each case.
// Here, I am just printing
// different strings.
void case1(){
    cout<< "case1" << endl;
}

void case2(){
    cout<< "case2" << endl;
}

void case3(){
    cout<< "case3" << endl;
}

void case4(){
    cout<< "case4" << endl;
}

//Put all the cases in an array.
chooseCase cases[] = {
    case1, case2, case3, case4
};

int main()
{
    //You can call each scenario
    //by hand easily this way:
    cases[CASE(1)]();
    cout << endl;

    //Idea: You can even set in another
    // array a sequence of function executions desired.
    int casesSequence[] = {
        CASE(1), CASE(2), CASE(3), CASE(4),CASE(3),CASE(2),CASE(1)
    };
    //Execute the functions in the sequence set.
    for(int i = 0; i < (sizeof(casesSequence)/sizeof(int)); ++i){
        cases[casesSequence[i]]();
    }

    return 0;
}

这将在输出处打印:

代码语言:javascript
复制
case1

case1
case2
case3
case4
case3
case2
case1
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18167107

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档