07
15

1) 게임제작

1.1) 캐릭터 이동 구현

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Resources;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _20200713_006
{
    public partial class Form1 : Form
    {
        const int WTileSize = 16;
        const int HTileSize = 9;
        const string Title = "푸시푸시";

        Image Human;
        Image HumanF;
        Image HumanL;
        Image HumanR;
        Image HumanB;

        Image Wall;
        Image Box;
        Image Dot;
        Image Road;

        int   WTile;
        int   HTile;

        int   XHuman;
        int   YHuman;
        int   XHumanOld;
        int   YHumanOld;


        char[][] MapReal;

        public Form1()
        {
            InitializeComponent();

            this.Text = Title;

            HumanF = new Bitmap(Properties.Resources.HumanF);
            HumanL = new Bitmap(Properties.Resources.HumanL);
            HumanR = new Bitmap(Properties.Resources.HumanR);
            HumanB = new Bitmap(Properties.Resources.HumanB);

            Wall = new Bitmap(Properties.Resources.Wall);
            Box = new Bitmap(Properties.Resources.Box);
            Dot = new Bitmap(Properties.Resources.Dot);
            Road = new Bitmap(Properties.Resources.Road);

            XHuman = 0;
            YHuman = 0;

            Human = HumanF;

            WTile = HumanF.Width;
            HTile = HumanF.Height;

            ClientSize = new Size(WTile * WTileSize, HTile * HTileSize);

            string[] TempMap = {"################", // 0
                                "#@     ##      #", // 1
                                "#    #    ###  #", // 2
                                "##  ###  #    ##", // 3
                                "##  ######  ####", // 4
                                "##  #          #", // 5
                                "## #  ##  B .  #", // 6
                                "##    ### B .  #", // 7
                                "################" };//8 

            MapReal = new char[HTileSize][];
            for (int i = 0; i < HTileSize; i++)
            {
                MapReal[i] = TempMap[i].ToCharArray();
            }
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Image Temp = Wall;
            for (int j = 0; j < HTileSize; j++)
            {
                for (int i = 0; i < WTileSize; i++)
                {
                    switch (MapReal[j][i])
                    {
                        case '#':
                            Temp = Wall;
                            break;
                        case 'B':
                            Temp = Box;
                            break;
                        case '.':
                            Temp = Dot;
                            break;
                        case ' ':
                            Temp = Road;
                            break;
                        case '@':
                            Temp = Human;
                            XHuman = i;
                            YHuman = j;
                            Text = Title + "[" + XHuman + "," + YHuman + "]";
                            break;
                        default:
                            break;
                    }
                    e.Graphics.DrawImage(Temp, i * WTile, j * HTile);
                }
            }
        }

        private void Move()
        {
            if (MapReal[YHuman][XHuman] == '#')
            {
                return;

            }
            else if (MapReal[YHuman][XHuman] == 'B')
            {
                if(MapReal[2 * YHuman- YHumanOld][2 * XHuman - XHumanOld] == ' ')
                {
                    MapReal[2 * YHuman - YHumanOld][2 * XHuman - XHumanOld] = 'B';
                }
                else if(MapReal[2 * YHuman - YHumanOld][2 * XHuman - XHumanOld] == '.')
                {
                    MapReal[2 * YHuman - YHumanOld][2 * XHuman - XHumanOld] = 'B';
                }
                else
                {
                    return;
                }
            }
            MapReal[YHumanOld][XHumanOld] = ' ';
            MapReal[YHuman][XHuman] = '@';
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            XHumanOld = XHuman;
            YHumanOld = YHuman;
            switch (e.KeyCode)
            {
                case Keys.Left:
                    if(XHuman > 0)
                    {
                        XHuman = XHuman - 1;
                    }
                    Human = HumanL;
                    break;
                case Keys.Right:
                    if(XHuman < WTileSize)
                    {
                        XHuman = XHuman + 1;
                    }
                    Human = HumanR;
                    break;
                case Keys.Up:
                    if(YHuman > 0)
                    {
                        YHuman = YHuman - 1;
                    }
                    Human = HumanB;
                    break;
                case Keys.Down:
                    if(YHuman < HTileSize)
                    {
                        YHuman = YHuman + 1;
                    }
                    Human = HumanF;
                    break;
                default:
                    return;
            }
            Move();
            Refresh();
        }
    }
}

 

예제 실행 화면

 

2) 복습

class Program
{
    class Car
    {
        const int iNum1 = 10; // 컴파일 시점에 값이 들어감, 수정 불가
        readonly int iNum2; // 실행 시점에 값이 들어감, 생성자에서 수정 가능

        string _color;
        string _vender;
        string _name;

        // 생성자
        // 1. 메서드다.
        // 2. 클래스와 이름이 동일
        // 3. 반환 타입이 존재하지 않는다.
        
        public string name // 프로퍼티
        {
            set
            {
                value = "Test : " + value;
                this._name = value;
            }
            get { return _name; }
        }

        //public string GetName()
        //{
        //    return name;
        //}

        //public void SetName(string name)
        //{
        //    name = "Test : " + name;
        //    this.name = name;
        //}

        public void Print()
        {
            Console.WriteLine($"[{_color}],[{_vender}],[{_name}]");
        }

        public Car() : this("", "", "") // 디폴트 생성자
        { 
            //Console.WriteLine("Car 디폴트 생성자 호출");
        }

        public Car(string name) : this(name, "", "") // 메서드 오버로딩
        {
            //this.name = name;
            //Console.WriteLine("Car 생성자 호출");
        }

        public Car(string name, string vender) : this(name, vender, "") // 메서드 오버로딩
        {
            //this.name = name;
            //this.vender = vender;
            //Console.WriteLine("Car 생성자 호출");
        }

        public Car(string name, string vender, string color) // 메서드 오버로딩
        {
            iNum2 = 100;
            this._name = name;
            this._vender = vender;
            this._color = color;
            Console.WriteLine("Car 생성자 호출");
        }

        ~Car()
        {
            Console.WriteLine("Car 소멸자 호출");
        }
    }

    static void Main(string[] args)
    {
        Console.WriteLine("1================================");
        Car aCar = new Car("Test");
        new Car();
        aCar.Print();
        //aCar.SetName("Name");
        aCar.Print();
        //Console.WriteLine(aCar.GetName());
        Console.WriteLine("2================================");
        Console.WriteLine("3================================");
        aCar.name = "Test2";
        aCar.Print();
        Console.WriteLine(aCar.name);
        Console.WriteLine("4================================");
    }
}

 

예제 실행 화면

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


class Parent
{
    public int iNum = 100;

    public Parent()
    {
        //Console.WriteLine("부모 클래스 생성자");
    }

    public Parent(int iNum)
    {
        this.iNum = iNum;
        //Console.WriteLine($"부모 클래스 생성자 {iNum}");
    }

    ~Parent()
    {
        //Console.WriteLine("부모 클래스 소멸자");
    }

    public virtual void Name()
    {
        Console.WriteLine("Parent");
    }
}

class Child : Parent
{
    public int iNum = 200;

    public Child() : base (101)
    {
        //Console.WriteLine("자식 클래스 생성자");
    }

    ~Child()
    {
        //Console.WriteLine("자식 클래스 소멸자");
    }

    public void Print()
    {
        Console.WriteLine($"부모 클래스 {base.iNum}");
        Console.WriteLine($"자식 클래스 {this.iNum}");
    }

    public override void Name()
    {
        Console.WriteLine("Child");
    }
}

class GrandChild : Child
{
    public override void Name()
    {
        Console.WriteLine("GrandChild");
    }
}

class Program
{
    static void Main(string[] args)
    {
        //Child child = new Child();
        //child.Print(); 

        //Parent obj1 = new Parent();
        ////Child obj2 = new Child();
        //Parent obj2 = new Child();
        ////GrandChild obj3 = new GrandChild();
        //Parent obj3 = new GrandChild();

        //obj1.Name();
        //obj2.Name();
        //obj3.Name();

        Parent[] Array = new Parent[] { new Parent(), new Child(), new GrandChild() };

        foreach (Parent item in Array)
        {
            item.Name();
        }
    }
}

 

예제 실행 화면

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

22. 복습  (0) 2020.07.20
21. 게임제작, 복습  (0) 2020.07.17
19. 게임 제작, 복습  (0) 2020.07.14
18. 복습  (0) 2020.07.13
17. 복습  (0) 2020.07.01
COMMENT