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

Ball Bar로 잡기

by SUGI_ 2023. 7. 11.

 

namespace BrickGame4
{
    internal class BARDATA
    {
        public int[] nX = new int[3]; //x좌표 배열3개 선언
        public int nY;
    }
}
namespace BrickGame4
{
    internal class Bar
    {
        BARDATA m_tBar = new BARDATA();
        int m_nCatch; //공을 잡았는지 체크

        const int LEFTKEY = 75; //상수로 만들어준다. 변수에 값 대입 x
        const int RIGHTKEY = 77; //상수로 만들어준다. 변수에 값 대입 x

        public void Initialize()
        {
            m_nCatch = 0;

            m_tBar.nY = 18;
            m_tBar.nX[0] = 12;
            m_tBar.nX[1] = 14;
            m_tBar.nX[2] = 16;
        }

        //공의 객체를 가지고와서 잡았는지 판단 및 움직임도 줘야함
        //ref로 인자값 전달 참조 
        public void Progress(ref Ball pBall) 
        {
            int nKey = 0;

            if(Console.KeyAvailable)
            {
                nKey = Program._getch(); // 키 눌림 값

                switch(nKey)
                {
                    case LEFTKEY: //왼쪽
                        m_tBar.nX[0]--;
                        m_tBar.nX[1]--;
                        m_tBar.nX[2]--;

                        if(pBall.GetBall().nReady == 1 && m_nCatch ==1)
                        {
                            //공이 잡힌상태
                            pBall.SetX(-1); //공왼쪽으로 움직이게 값주기
                        }
                        break;
                    case RIGHTKEY: //오른쪽
                        m_tBar.nX[0]++;
                        m_tBar.nX[1]++;
                        m_tBar.nX[2]++;

                        if(pBall.GetBall().nReady == 1 && m_nCatch ==1)
                        {
                            //공이 잡힌상태
                            pBall.SetX(1); //공 오른쪽으로 움직이게 값주기
                        }
                        break;

                    case 'a':
                        BALLDATA tBall = new BALLDATA();
                        tBall.nReady = 0;
                        tBall.nX = 20;
                        tBall.nY = 1;
                        pBall.SetBall(tBall);
                        m_nCatch = 0;
                        break;
                    case 's':
                        if(pBall.GetBall().nX >= m_tBar.nX[0] &&pBall.GetBall().nX<= m_tBar.nX[2]+1 && pBall.GetBall().nY ==(m_tBar.nY-1))
                        {
                            pBall.SetReady(1);
                            m_nCatch = 1;
                        }
                        break;
                }
            }
        }

        public void Render()
        {
            for(int i = 0; i<3; i++)
            {
                Program.gotoxy(m_tBar.nX[i], m_tBar.nY);
                Console.Write("▥");
            }
        }

        public void Release()
        {

        }
    }
}
namespace BrickGame4
{
    public class BALLDATA
    {
        public int nReady; //공의 움직일지 말지 결정
        public int nDirect; //공의 방향값
        public int nX, nY; //x좌표, y좌표
    }
}
namespace BrickGame4
{
    public class Ball
    {
        BALLDATA m_tBall = new BALLDATA();

        public BALLDATA GetBall() { return m_tBall; } //볼데이터를 가져오는 함수
        public void SetX(int x) { m_tBall.nX += x; } //공의 x값을 더해주는 함수
        public void SetY(int y) { m_tBall.nY += y; }  //공의 y값을 더해주는 함수
        public void SetBall(BALLDATA tBall) { m_tBall = tBall; } //볼데이터를 전달해주는 함수
        public void SetReady(int Ready) { m_tBall.nReady = Ready; } //공의 움직임을 전달해주는 함수

        public void Initialize()
        {
            m_tBall.nReady = 1; //공안움직임 1
            m_tBall.nDirect = 0;
            m_tBall.nX = 20;
            m_tBall.nY = 1;
        }

        public void Progress()
        {
            if(m_tBall.nReady == 0) //공이 움직임
            {
                m_tBall.nY++;

                if(m_tBall.nY >24) //밑에 벽을 만나면 초기화
                {
                    m_tBall.nReady = 1;
                    m_tBall.nX = 20;
                    m_tBall.nY = 1;
                }
            }
        }

        public void Render() //공 그려주는 함수
        {
            Console.Clear();
            Program.gotoxy(m_tBall.nX, m_tBall.nY);
            Console.Write("●");
        }

        public void Release()
        {

        }
    }
}
namespace BrickGame4
{
    internal class GameManager
    {
        Ball m_pBall = null;
        Bar m_pBar = null;

        public void Initialize()
        {
            m_pBall = new Ball();
            m_pBar = new Bar();

            m_pBall.Initialize();//초기화
            m_pBar.Initialize();
        }

        public void Progress()
        {
            m_pBall.Progress();
            m_pBar.Progress(ref m_pBall);
        }

        public void Rander()
        {
            m_pBall.Render();
            m_pBar.Render();
        }

        public void Release() //c#은 굳이 하지 않도 괜춘,! 가비지콜렉터
        {
            m_pBall = null;
            m_pBar = null;
        }
    }
}
namespace BrickGame4
{
    internal class Program
    {
        [DllImport("msvcrt.dll")]
        public static extern int _getch();  //c언어 함수 가져옴

        //좌표 함수 , 각 클래스에 따로 붙일 필요가 없다고 생각 
        public static void gotoxy(int x, int y)
        {
            Console.SetCursorPosition(x, y);
        }

        static void Main(string[] args)
        {
            GameManager gm = new GameManager();
            gm.Initialize();

            int Curr = Environment.TickCount;

            while (true)
            {
                if (Curr + 50 < Environment.TickCount)
                {
                    Curr = Environment.TickCount;
                    gm.Progress();
                    gm.Rander();
                }
            }
            gm.Release();
        }
    }
}
728x90

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

공 튕기기  (0) 2023.07.12
벽돌 중복없이 만들기  (0) 2023.07.12
공의 이동  (0) 2023.07.11
공 벽 튕기기  (0) 2023.07.11
TextRpgGame <최종>  (0) 2023.07.11