본문으로 바로가기

소코반 만들기 수정 작업(2)

category 스마트팩토리/C# 2020. 10. 28. 18:28
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlTypes;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _2020_10_211
{
    public partial class Form1 : Form
    {
        //그림이랑 문자를 호환 Mapping이라 한다.
        Bitmap Box;         //'B'
        Bitmap Point;       //'.'
        Bitmap Human;       //'@'
        Bitmap Wall;        //'#'
        Bitmap Road;        //''
        Bitmap Gound;       //'G'

        char[][] MapReal;
        string[,] Map;
        const int XTile = 16;
        const int YTile = 10;
        const int XPixel = 50;
        const int YPixel = 50;
        int HunmanX;
        int HunmanY;
        int HunmanXOld;         //휴먼의 움직이기전의 좌표를 저장하는 변수
        int HunmanYOld;         //휴먼의 움직이기전의 좌표를 저장하는 변수
        int iStage;             //스테이지의 판수를 나타내는 변수

        bool EndStage;          //스테이지가 끝나는지를 확인하는 변수



        public Form1()
        {
            InitializeComponent();
            this.ClientSize = new System.Drawing.Size(XPixel * XTile, YPixel * YTile);
            Box = new Bitmap(Properties.Resources.Box);
            Point = new Bitmap(Properties.Resources.Point);
            Human = new Bitmap(Properties.Resources.Human);
            Wall = new Bitmap(Properties.Resources.Wall);
            Road = new Bitmap(Properties.Resources.Road);
            Gound = new Bitmap(Properties.Resources.Gound);
            iStage = 0; //스테이지를 나타낸다.


            //          
            Map = new string[,] { {"GGGGGGGGGGGGGGGG",    //0
                                   "G##############G",    //1
                                   "G#            #G",    //2
                                   "G#           ##G",    //3
                                   "G#       BB..@#G",    //4 
                                   "G#            #G",    //5
                                   "G#            #G",    //6
                                   "G#            #G",    //7
                                   "G##############G",    //8
                                   "GGGGGGGGGGGGGGGG" },  //9
                                              
                                  {"GGGGGGGGGGGGGGGG",    //0
                                   "G##############G",    //1
                                   "G##############G",    //2
                                   "G#    @  ######G",    //3
                                   "G#  ..   ######G",    //4 
                                   "G#  BB        #G",    //5
                                   "G#    #####   #G",    //6
                                   "G#            #G",    //7
                                   "G##############G",    //8
                                   "GGGGGGGGGGGGGGGG"} }; //9  
            string Temp = "apple";      // stub코드는 영향을 미치지는 않지만 테스트하기위해서
            MapReal = new char[YTile][];

            for (int i = 0; i < YTile; i++)
            {
                MapReal[i] = Map[iStage, i].ToCharArray();      //iStage를 통해서 판수를 연결한다.
            }
            //MapReal[0][0] = '#';


        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Bitmap abitmap;
            EndStage = true;


            for (int iy = 0; iy < YTile; iy++)
            {

                for (int ix = 0; ix < XTile; ix++)
                {

                    switch (MapReal[iy][ix])
                    {
                        case '#':
                            abitmap = Wall;
                            break;
                        case 'G':
                            abitmap = Gound;
                            break;
                        case '@':
                            abitmap = Human;
                            HunmanX = ix;
                            HunmanY = iy;                            
                            //MessageBox.Show(string.Format("{0} {1}",ix,iy));
                            break;
                        case 'B':

                            abitmap = Box;
                            if(Map[iStage, iy][ix] != '.')              //Box가 .에 들어가지 않게 된다면 계속 돌리게 된다는 뜻이다.
                            {
                                EndStage = false;
                            }
                            break;
                        case '.':
                            abitmap = Point;
                            break;

                        case ' ':
                            abitmap = Road;
                            break;
                        default:
                            abitmap = Road;
                            break;
                    }
                        e.Graphics.DrawImage(abitmap, ix * 50, iy * 50);
                    }
                }
             
            }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void MoveHuman()
        {

        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            MapReal[HunmanY][HunmanX] = ' ';
            HunmanXOld = HunmanX;
            HunmanYOld = HunmanY;
            switch (e.KeyCode)
            {
                case Keys.Left:
                    if(MapReal[HunmanY][HunmanX - 1] == '#')        //벽이면 막히게 하는 코드
                    {
                        return;
                    }
                    if (MapReal[HunmanY][HunmanX - 1] == 'B')       
                    {
                        if (MapReal[HunmanY][HunmanX - 2] == '#')       //박스가 벽에 부딪히면 막히는 코드
                        {
                            return;
                        }
                        if (MapReal[HunmanY][HunmanX - 2] == 'B')       //박스가 박스가 겹치게 되면 막히는 코드
                        {
                            return;
                        }
                        MapReal[HunmanY][HunmanX - 2] = 'B';

                    }
                    --HunmanX;
                    break;
                case Keys.Right:
                    if (MapReal[HunmanY][HunmanX + 1] == '#')
                    {
                        return;
                    }
                    if (MapReal[HunmanY][HunmanX + 1] == 'B')
                    {
                        if (MapReal[HunmanY][HunmanX + 2] == '#')
                        {
                            return;
                        }
                        if (MapReal[HunmanY][HunmanX + 2] == 'B')
                        {
                            return;
                        }


                        MapReal[HunmanY][HunmanX + 2] = 'B';
                       
                    }
                    ++HunmanX;
                    break;
                case Keys.Down:
                    if (MapReal[HunmanY + 1][HunmanX] == '#')
                    {
                        return;
                    }
                    if (MapReal[HunmanY + 1][HunmanX] == 'B')
                    {
                        if (MapReal[HunmanY + 2][HunmanX] == '#')
                        {
                            return;
                        }

                        if (MapReal[HunmanY + 2][HunmanX] =='B')
                        {
                            return;
                        }
                        MapReal[HunmanY + 2][HunmanX] = 'B';
                        
                    }
                    ++HunmanY;
                    break;
                case Keys.Up:
                    if (MapReal[HunmanY - 1][HunmanX] == '#')
                    {
                        return;
                    }
                    if (MapReal[HunmanY - 1][HunmanX] == 'B')
                    {
                        if (MapReal[HunmanY - 2][HunmanX] == '#')
                        {
                            return;
                        }
                        if (MapReal[HunmanY - 2][HunmanX] == 'B')
                        {
                            return;
                        }

                        MapReal[HunmanY - 2][HunmanX] = 'B';
                       
                    }

                    --HunmanY;
                    break;
                default:
                    MapReal[HunmanY][HunmanX] = '@';
                    return;

            }
            if(Map[0,HunmanYOld][HunmanXOld] == '.')        
            {
                MapReal[HunmanYOld][HunmanXOld] = '.';
            }
            MapReal[HunmanY][HunmanX] = '@';

            Invalidate();
            Update();       
            if (EndStage)               //박스가 포인트에 다 들어가게 되면 메세지박스가 뜬다.
            {
                MessageBox.Show("종료");
            }

            //번쩍거림을 없애기 위해서 더블버퍼링을 켰다.
        }
    }

    }


 

'스마트팩토리 > C#' 카테고리의 다른 글

소코반 만들기 수정 작업(1)  (0) 2020.10.27
소코반 만들기 전 기초 작업(배열)  (0) 2020.10.23
2020/9/8 C#  (0) 2020.09.08
2020/8/18 C#  (0) 2020.08.18
2020/7/30 C#  (0) 2020.07.30