문제

나는 내 첫 번째 게임인 콘솔 테트리스를 만들려고 노력하고 있습니다.x 및 y 정수를 포함하는 Block 클래스가 있습니다.그럼 난 수업이 있어 Piece : List<Block>, 그리고 수업 Pieces : List<Piece>.

이미 무작위로 조각을 생성하여 초당 한 줄씩 떨어지게 할 수 있습니다.아직 충돌 감지에 도달하지 못했지만 나중에 해결하는 방법을 이미 알고 있다고 생각합니다.문제는 조각을 제어하는 ​​방법을 모른다는 것입니다.키보드 후킹에 대해 조금 읽고 일부 테트리스 튜토리얼을 확인했지만 대부분은 이벤트 처리 등을 단순화하는 Windows 양식용입니다.

그래서...콘솔의 부분을 제어하는 ​​경로의 시작 부분을 알려주시겠습니까?감사해요!

public class Program
    {
        static void Main(string[] args)
        {
            const int limite = 60;
            Piezas listaDePiezas = new Piezas();    //list of pieces
            bool gameOver = false;
            Pieza pieza;    //piece
            Console.CursorVisible = false;
            while (gameOver != true)
            {
                pieza = CrearPieza();    //Cretes a piece
                if (HayColision(listaDePiezas, pieza) == true)   //if there's a collition
                {
                    gameOver = true;
                    break;
                }
                else
                    listaDePiezas.Add(pieza);    //The piece is added to the list of pieces
                while (true)    //This is where the piece falls. I know that I shouldn't use a sleep. I'll take care of that later
                {
                    Thread.Sleep(1000);
                    pieza.Bajar();    //Drop the piece one row.
                    Dibujar(listaDePiezas);    //Redraws the gameplay enviroment.
                }
            }
        }
도움이 되었습니까?

해결책

당신이 찾고 있는 것은 비차단 콘솔 입력입니다.

예는 다음과 같습니다.

http://www.dutton.me.uk/2009/02/24/non-blocking-keyboard-input-in-c/

기본적으로 확인해보면 Console.Key사용 가능 while 루프에서 누른 키에 따라 조각을 이동하십시오.


            if (Console.KeyAvailable)
            {
                ConsoleKeyInfo cki = Console.ReadKey();
                switch (cki.Key)
                {
                    case ConsoleKey.UpArrow:
                        // not used in tetris game?
                        break;
                    case ConsoleKey.DownArrow:
                        // drop piece
                        break;
                    case ConsoleKey.LeftArrow:
                        // move piece left
                        break;
                    case ConsoleKey.RightArrow:
                        // move piece right
                        break;
                }
            }

다른 팁

그림과 같이 저수준 키보드 후크를 사용할 수 있습니다. 여기

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top