본문 바로가기
프로그래밍/콘솔프로젝트

공의 이동

by SUGI_ 2023. 7. 11.
namespace BrickGame2
{
    public class BALLDATA
    {
        public int m_nReady;  //키를 누르면 공이 움직이게 하기위한 상태
        public int m_nX;      //좌표 X
        public int m_nY;      //좌표 Y
        public int m_nDirect; //방향
        public int m_nSpeed;  //스피드
    }
}
namespace BrickGame2
{
    public class Ball
    {
        private BALLDATA tInfo = new BALLDATA(); //공 데이터 객체

        public void Initialize()
        {
            tInfo.m_nReady = 1; // 1이 준비상태 0 움직이는상태
            tInfo.m_nDirect = 0; // 0위 1 오른쪽위...
            tInfo.m_nX = 30;
            tInfo.m_nY = 10;
            tInfo.m_nSpeed = 1;
        }

        public void Progress()
        {
            //키입력함수
            KeyInput();
            //움직임
            if(tInfo.m_nReady == 0) //움직이는상태
            {
                switch(tInfo.m_nDirect)
                {
                    case 0://위
                        tInfo.m_nY -= tInfo.m_nSpeed;
                        break;
                    case 1: //오른쪽 위
                        tInfo.m_nX += tInfo.m_nSpeed;
                        tInfo.m_nY -= tInfo.m_nSpeed;
                        break;
                    case 2: //오른쪽 아래
                        tInfo.m_nX += tInfo.m_nSpeed;
                        tInfo.m_nY += tInfo.m_nSpeed;
                        break;
                    case 3: //아래
                        tInfo.m_nY += tInfo.m_nSpeed;
                        break;
                    case 4: //왼쪽 아래
                        tInfo.m_nX -= tInfo.m_nSpeed;
                        tInfo.m_nY += tInfo.m_nSpeed;
                        break;
                    case 5: //왼쪽 위로
                        tInfo.m_nX -= tInfo.m_nSpeed;
                        tInfo.m_nY -= tInfo.m_nSpeed;
                        break;
                }
                //4군데 콜솔벽에 나갈려고한다면
                if(tInfo.m_nY < 1 || tInfo.m_nX>79 || tInfo.m_nY >23 || tInfo.m_nX <1)
                {
                    tInfo.m_nReady = 1;
                    tInfo.m_nX = 30;
                    tInfo.m_nY = 10;
                    tInfo.m_nDirect = 0;
                }
            }
        }

        public void Render() 
        {
            Console.Clear();
            gotoxy(tInfo.m_nX, tInfo.m_nY);
            Console.Write("●");
        }
        public void Release() 
        { 
        }
        public void gotoxy(int x, int y) 
        {
            Console.SetCursorPosition(x, y);
        } //좌표함수

        public void KeyInput()//키입력 함수
        {
            int nKey;

            if (Console.KeyAvailable)
            {
                nKey = Program._getch(); //키가눌리면 값을 가져온다. 아스키코드값

                switch (nKey)
                {
                    case '0':
                        tInfo.m_nDirect = 0;
                        tInfo.m_nX = 30;
                        tInfo.m_nY = 10;
                        break;
                    case '1':
                        tInfo.m_nDirect = 1;
                        tInfo.m_nX = 30;
                        tInfo.m_nY = 10;
                        break;
                    case '2':
                        tInfo.m_nDirect = 2;
                        tInfo.m_nX = 30;
                        tInfo.m_nY = 10;
                        break;
                    case '3':
                        tInfo.m_nDirect = 3;
                        tInfo.m_nX = 30;
                        tInfo.m_nY = 10;
                        break;
                    case '4':
                        tInfo.m_nDirect = 4;
                        tInfo.m_nX = 30;
                        tInfo.m_nY = 10;
                        break;
                    case '5':
                        tInfo.m_nDirect = 5;
                        tInfo.m_nX = 30;
                        tInfo.m_nY = 10;
                        break;
                    case 'f':
                        tInfo.m_nReady = 0; //공움직이게 하기
                        break;
                }
            }
        }
    }
}
namespace BrickGame2
{
    public class Program
    {
        [DllImport("msvcrt.dll")]
        public static extern int _getch();  //c언어 함수 가져옴
        static void Main(string[] args)
        {
            Ball ball = new Ball(); //객체생성

            int Current = Environment.TickCount;

            ball.Initialize();

            while(true)
            {
                if(Current+100 <Environment.TickCount) //0.1초정도 지나갔다면 
                {
                    Current = Environment.TickCount;  //현재시간세팅
                    ball.Progress();
                    ball.Render();
                }
            }

            ball.Release();

        }
    }
}
728x90

'프로그래밍 > 콘솔프로젝트' 카테고리의 다른 글

벽돌 중복없이 만들기  (0) 2023.07.12
Ball Bar로 잡기  (0) 2023.07.11
공 벽 튕기기  (0) 2023.07.11
TextRpgGame <최종>  (0) 2023.07.11
ShootingGame <최종>  (0) 2023.07.04