07
14

1) 게임 제작

1.1) 캐릭터 이미지 준비

게임 캐릭터로 사용할 이미지를 준비

 

1.2) 맵 이미지 준비

게임에 사용할 맵 이미지를 준비

 

1.3) 캐릭터와 맵 그리기, 캐릭터 이동

public partial class Form1 : Form
{
	const int WTileSize = 16;
	const int HTileSize = 9;

	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;

	string[] Map;

	public Form1()
	{
		InitializeComponent();

		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 

		Map = TempMap;
	}

	private void Form1_Paint(object sender, PaintEventArgs e)
	{
		for (int j = 0; j < HTileSize; j++)
		{
			for (int i = 0; i < WTileSize; i++)
			{
				if (Map[j][i].Equals('#'))
				{
					e.Graphics.DrawImage(Wall, i * WTile, j * HTile);
				}
				else if (Map[j][i].Equals('B'))
				{
					e.Graphics.DrawImage(Box, i * WTile, j * HTile);
				}
				else if (Map[j][i].Equals('.'))
				{
					e.Graphics.DrawImage(Dot, i * WTile, j * HTile);
				}
				else if (Map[j][i].Equals(' '))
				{
					e.Graphics.DrawImage(Road, i * WTile, j * HTile);
				}
			}
		}


		e.Graphics.DrawImage(Human,XHuman,YHuman);
	}

	private void Form1_KeyDown(object sender, KeyEventArgs e)
	{
		switch(e.KeyCode)
		{
			case Keys.Left:
				XHuman = XHuman - WTile;
				Human = HumanL;
				break;
			case Keys.Right:
				XHuman = XHuman + WTile;
				Human = HumanR;
				break;
			case Keys.Up:
				YHuman = YHuman - HTile;
				Human = HumanB;
				break;
			case Keys.Down:
				YHuman = YHuman + HTile;
				Human = HumanF;
				break;
			default:
				return;
		}
		Refresh();
	}
}

 

예제 실행 화면

 

2) 복습

public class Car
{
    // 속성(attribute) : 변수
    public int Speed; // 속도 : 숫자 => int
    const int SpeedValue = 10;

    // 행위(behavior) : 메서드
    public void Run()
    {
        Console.WriteLine("달립니다.");
    }

    public void Accel()
    {
        if(Speed < 200)
        {
            Speed = Speed + SpeedValue;
        }
        Console.WriteLine($"현재 속도는 {Speed}입니다.");
    }

    public void Break()
    {
        if(Speed > 0)
        {
            Speed = Speed - SpeedValue;
        }
        Console.WriteLine($"현재 속도는 {Speed}입니다.");
    }
}

class Program
{
    static void Main(string[] args)
    {
        Car aCar = new Car();
        aCar.Accel();
        aCar.Accel();
        aCar.Accel();
        aCar.Accel();
        aCar.Break();
        aCar.Break();
        aCar.Break();
        aCar.Break();
    }

    static void Main1(string[] args)
    {
        Car aCar;
        aCar = new Car();
        aCar.Run();
        aCar.Speed = 0;
        Console.WriteLine($"aCar의 속도는 {aCar.Speed}입니다.");
    }
}

 

예제 실행 화면

 

class Program
{
    class Car
    {
        string color;
        string vender;
        string name;

        // 생성자
        // 1. 메서드다.
        // 2. 클래스와 이름이 동일
        // 3. 반환 타입이 존재하지 않는다.
        
        public Car() // 디폴트 생성자
        {
            Console.WriteLine("Car 디폴트 생성자 호출");
        }

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

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

    static void Main(string[] args)
    {
        Console.WriteLine("1================================");
        Car aCar = new Car("Test");
        new Car();
        Console.WriteLine("2================================");
    }
}

 

예제 실행 화면

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

21. 게임제작, 복습  (0) 2020.07.17
20. 게임제작, 복습  (0) 2020.07.15
18. 복습  (0) 2020.07.13
17. 복습  (0) 2020.07.01
16. 복습  (0) 2020.06.30
COMMENT