2013-07-28 4 views
1

내 ascii 게임 충돌에 도움이 필요합니다. 나는 이미 플레이어가 다른 물건을 때리면 무언가를하는 법을 이미 알고 있습니다. 하지만 플레이어가 오브젝트를 때릴 때 플레이어를 멈추게하려면 혼란 스럽습니다.C# ascii 게임 충돌

플레이어가 다른 물체와 충돌 할 때 사용합니다.

if(player.x == object.x && player.y == object.y) 
    { 
     //Does something 
    } 

그래서 당신은 I'v 잠시 동안 충돌 일에되어 있기 때문에 좋지 않을까 그런 나를 도울 수 있고 나는 그것이 가능한 게임 같은 미로가 될 수 있도록 내 게임으로 구현하려는 경우. 읽어 주셔서 감사합니다 : 여기

3 내 게임 "전체가 CODE"의 구조입니다 : 당신의 C#/객체 지향 프로그래밍에 익숙처럼

public static void Main(string[] args) 
    { 
     int x = 40; 
     int y = 12; 
     int ax = 23; 
     int ay = 7; 
     int apple = 0; 
     int starX = 64; 
     int starY = 5; 
     int score = 0; 
     int total = 0; 
     int time = 100; 
     int xt; 
     int yt; 
     bool quit = false; 
     Console.Title = "Catch and Run"; 

     while (quit == false) 
     { 
      Console.BackgroundColor = ConsoleColor.DarkGreen; 

      //Blocks 
      Console.SetCursorPosition(29, 18); 
      Console.BackgroundColor = ConsoleColor.DarkGreen; 

      //Clear Screen 
      Console.Clear(); 

      //Players 
      Console.SetCursorPosition(x, y); 
      Console.ForegroundColor = ConsoleColor.Yellow; 
      Console.Write("☻"); 

      Console.SetCursorPosition(ax, ay); 
      Console.ForegroundColor = ConsoleColor.Red; 
      Console.Write("o"); 

      Console.SetCursorPosition(starX, starY); 
      Console.ForegroundColor = ConsoleColor.Gray; 
      Console.Write("☻"); 
      Console.ResetColor(); 

      //score 
      Console.SetCursorPosition(0, 0); 
      Console.WriteLine("People: " + score); 
      //apples 
      Console.SetCursorPosition(20, 0); 
      Console.WriteLine("Apples: " + apple); 
      //Total 
      Console.SetCursorPosition(40, 0); 
      total = apple + score; 
      Console.WriteLine("Total: " + total); 
      //Timer 
      Console.SetCursorPosition(0, 23); 
      Console.WriteLine("Steps Left: "+time); 
      time--; 

      ConsoleKeyInfo keyInfo = Console.ReadKey(false); 

      //key controlls 
      switch (keyInfo.Key) 
      { 
       case ConsoleKey.Escape: 
        quit = true; 
        break; 
       case ConsoleKey.UpArrow: 
        if (y > 1) 
         y--; 

        break; 
       case ConsoleKey.DownArrow: 
        if (y < 22) 
         y++; 

        break; 
       case ConsoleKey.LeftArrow: 
        if (x > 0) 
         x--; 

        break; 
       case ConsoleKey.RightArrow: 
        if (x < 79) 
         x++; 

        break; 
      } 
      // Randomize Blacks 
      if (x == starX && y == starY) 
      { 

       Random random = new Random(); 
       starX = random.Next(0, 80); 
       starY = random.Next(1, 22); 
       score += 10; 
       time += 30; 
      } 
      //Randomize Apples 
      if (x == ax && y == ay) 
      { 

       Random random = new Random(); 
       ax = random.Next(0, 80); 
       ay = random.Next(1, 22); 
       apple += 5; 
       time += 20; 
      } 
      // Game Over timer set to 0 
      if (time == 0) 
      { 

       Console.Clear(); 
       Console.SetCursorPosition(40, 0); 
       Console.WriteLine("GAME OVER"); 

       Console.WriteLine("Score" + total); 
       Console.WriteLine("Press any key to continue"); 
       Console.ReadKey(); 
       quit = true; 
      } 
      if (x == 29 && y == 18) 
      { 
       x = 0; 
       y = 1; 

      } 
     } 
    } 
} 

}

+0

어떻게 플레이어를 이동합니까? 당신의 선수는 단지 속도로 움직일 것입니다 그리고 당신은 단지 그것을 0으로 설정할 수 있습니다 – Sayse

답변

3

이 느낌. 내 대답은 조금 복잡하지만 더 많은 질문을 환영합니다.

핵심 질문에 답하면 "플레이어가 개체에 닿을 때 플레이어가 멈추길 원합니다"라고 대답하면 인간 플레이어가 영웅을 이동시키고 싶은 곳을 찾은 다음 충돌 감지를 수행해야합니다. 플레이어가 막히지 않으면, 그를 움직이십시오.

이것이 결국 미로와 같은 게임이 될 경우, 어떻게 든 경기장을 저장해야합니다. 간단한 방법은 다음과 같습니다.

char[,] maze = new char[25,80]; // [y,x] 

이 배열을 만들고 사과 몇 개를 배치 한 다음 보드를 표시해야합니다.

예를 들어, 우리가 수행하여 5,5에 사과를 배치 할 수 있습니다 :

for(int by=10; by<20; by++) 
{ 
    maze[by, 0] = '#'; 
    maze[by, 2] = '#'; 
} 
:
maze[5,5] = 'o'; 

일단 보드의 작품을 보여주는, 당신은 다음과 같은 벽을 만들 확장 할 수 있습니다

그러면 화면 왼쪽에 10 개의 회랑이 생깁니다.

보드를 인쇄하는 지우기 통화 후 발생한다 :

for (int by = 0; by < 25; by++) 
{ 
    for (int bx = 0; bx < 80; bx++) 
    { 
     Console.Write(maze[by, bx]); 
    } 
} 

이 충돌 감지를 충족하려면, 당신은 이런 식으로 뭔가를 할 것입니다 :

// !! Replace your switch with this, then continue experimenting 

int tempX, tempY; 

tempX = x; 
tempY = y; 

switch (keyInfo.Key) 
{ 
    case ConsoleKey.Escape: 
     quit = true; 
     break; 
    case ConsoleKey.UpArrow: 
     if (tempY > 1) 
      tempY--; 
     break; 
    case ConsoleKey.DownArrow: 
     if (tempY < 22) 
      tempY++; 
     break; 
    case ConsoleKey.LeftArrow: 
     if (tempX > 0) 
      tempX--; 
     break; 
    case ConsoleKey.RightArrow: 
     if (tempX < 79) 
      tempX++; 
     break; 
} 

// Bounds checking already done by switch 

if(maze[tempY, tempX] == '#') 
{ 
    // Collision with wall is detected - forbid player from moving 
} 
else if(maze[tempY, tempX] == 'o') 
{ 
    // Collision with apple is detected, handle consuming an apple 
    x = tempX; // Now we update the player's position 
    y = tempY; 
} else { 
    x = tempX; // No collision with anything, simply update player's position 
    y = tempY; 
} 

을 짧은 개정의 당신 귀하의 코드에 할 수 있습니다. 코드를 개발하고 유지 관리하기 쉽게하기 위해 다양한 코드 조각을 자신의 메서드 및 클래스로 옮기는 것에 대해 생각해보십시오. 각 메소드가 하나의 책임을 지도록 목표를 설정해야합니다 (기본 메소드가 너무 많이 수행되고, 단일 책임 메소드가 코드 작성 및 수정이 더 쉽습니다). 각 클래스는 하나의 책임을 져야합니다.

예를 들어, "bool CanMoveTo (int x, int y)"와 같은 메서드를 사용하면 경계 검사 및 충돌 감지를 Main 외부에 배치 할 수 있으며이 메서드를 호출하여 플레이어가 있는지 유효한 움직임을 만든다.

클래스의 경우 미로와 그 부분을 관리하는 Board 클래스, 미로에 배치 할 수있는 다양한 요소를 나타내는 BoardPiece 클래스가있을 수 있으며 메인 프로그램은 보드를 플레이어에게 보냅니다.

게임에 행운을 비네!