2013-08-10 2 views
0

libnds를 사용하여 C++에서 NDS를 코딩하고 있지만이 질문은 NDS 관련 사항이 아닙니다. 나는 현재 최상위 화면에 로고 만 표시하고 아래쪽 화면에서 게임하는 텍스트 기반 게임을 사용합니다.분할 화면 멀티 플레이어를 C++ 게임에 추가

그래서 한 명의 플레이어가 상단 화면에서 재생하고 다른 하나는 하단에서 재생하는 단일 DS 멀티 플레이어 유형을 추가하고 싶습니다. 두 화면 모두에서 텍스트 엔진을 설정하는 데 문제가 없습니다. 멀티 플레이어에서 효율적으로 코딩하는 방법을 찾아야합니다. 아래 나는 그것의 개요 또는 단순화 된 버전을 썼다.

참고 : consoleClear()는 화면을 지우고 게임이 멈추는 유일한 지점은 일시 중지 기능입니다.

//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(); 
      } 
     } 
    } 
} 

나는 잡동사니가 혼란스럽고 나쁜 습관으로 여겨 질 수 있다는 것을 알고 있지만 더 좋은 방법은 없다고 생각합니다. 통합 멀티 플레이어의 내 버전 : 나쁜 관행 인이 방법에서 제외

//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의 수백이있다.

도움을 주시면 감사하겠습니다. 누군가 질문이나 대답이 조금이라도 있으면 질문/답장을하십시오.

편집 : 이렇게하는 것이 바람직하지 않지만 다른 사람에게 그렇게 할 방법이있는 경우 게임을 처음부터 다시 작성하려고합니다.

+0

gotos에 대해서, 왜 기능에 공통 기능을두고, 필요할 때 전화하지? 경우처럼? * 및 *는 기본 경우입니다. –

+0

멀티 플레이어 및 분할 화면의 문제에 관해서는 처음부터 게임을 디자인 한 경우 이러한 것들이 * 많은 * 효과가 있습니다. 예, 현재 솔루션을 손질하고 새로운 디자인으로 다시 시작하는 것이 좋습니다.이미 작동중인 솔루션에 그런 것들을 추가하는 것은 작동한다고해도 항상 나 빠질 것입니다. –

+0

@Joachim Pileborg : 물론, 전체 게임을 처음부터 재 설계 할 의향이 있습니다. 그렇게 할 수있는 방법을 생각조차 모릅니다. 그렇게 할 수있는 방법을 찾으면 내 질문에 답할 것입니다. –

답변

1

각 사례에 대해 if-else 조건문을 사용하는 것이 가장 먼저 떠오르는 간단한 해결책입니다. 예를 들어

:

int game(int i){ 
    if(i == 1){ 
    //first case code here. 
    } 
    else if(i == 2){ 
    //second case code here. 
    } 
    //.... 
    return 0; 
} 

각 경우의 코드라도, 각 상태에 따라 호출되는 다른 기능에 배치 될 수있다. 아마도 이것으로 충분할 것입니다.

더욱 유연한 솔루션 (그러나 훨씬 더 복잡한)은 디스패치 테이블입니다. 아이디어는 각각의 원하는 기능을 가진 별도의 기능을 가지고 배열에 포인터를 넣는 것입니다. 그런 다음 함수 포인터를 사용하여 테이블을 인덱싱하여 호출 할 수 있습니다. 이는 수행 할 일련의 실행 (함수 호출)이 있고 쉽게 완료되도록 설정하거나 프로그램을 변경하지 않고 입력에 따라 다른 결과를 원할 경우 매우 유용 할 수 있습니다.

아래 예가 있습니다. std :: cout을 printf로 바꾸고 iostreamstdio 라이브러리로 바꾸면이 코드를 C에서 사용할 수도 있습니다.

#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; 
} 

이 출력에서 ​​출력합니다 :

case1 

case1 
case2 
case3 
case4 
case3 
case2 
case1 
+0

나는 왜 내가 첫번째 것을 생각하지 않았는지 모르겠다. :). 그리고이 게시물을보고 다른 사람을 위해 간단하게 if ... else if 문을 switch 문으로 변경하면 위와 같이 할 수 있습니다. –

관련 문제