성장일기
7/15 게임을 만들어보자 3 본문
using System.Windows.Forms;
using System.Drawing;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
const int WTileSize = 16;
const int HTileSize = 9;
string[] Map;
Image Human;
Image HumanF;
Image HumanL;
Image HumanR;
Image HumanB;
Image Wall;
Image Road;
Image Box;
Image Dot;
int WTile;
int HTile;
int XHuman;
int YHuman;
public Form1()
{
InitializeComponent();
HumanF = new Bitmap(BrickGame.Properties.Resources.HumanF);
WTile = HumanF.Width;
HTile = HumanF.Height;
ClientSize = new Size(WTileSize * WTile, HTileSize * HTile);
HumanL = new Bitmap(BrickGame.Properties.Resources.HumanL);
HumanR = new Bitmap(BrickGame.Properties.Resources.HumanR);
HumanB = new Bitmap(BrickGame.Properties.Resources.HumanB);
Human = HumanF;
Wall = new Bitmap(BrickGame.Properties.Resources.Wall);
Road = new Bitmap(BrickGame.Properties.Resources.Road);
Box = new Bitmap(BrickGame.Properties.Resources.Box);
Dot = new Bitmap(BrickGame.Properties.Resources.Dot);
XHuman = 0;
YHuman = 0;
string[] TempMap = { "################",
" # #",
"###### B . ## ",
"#BBB ###########",
"#### ##### ##",
"# @ #",
"## B### #",
"#B ### ",
"####### "};
Map = TempMap;
}
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(Map[j][i])
{
case '#': Temp = Wall;
break;
case ' ':
Temp = Road;
break;
case '.':
Temp = Dot;
break;
case 'B':
Temp = Box;
break;
case '@':
Temp = Human;
break;
}
e.Graphics.DrawImage(Temp, WTile * i, HTile * j);
//if ('#' == Map[j][i])
//{
// Temp = Wall;
//}
//else if ('B' == Map[j][i])
//{
// Temp = Box;
//}
//else if ('.' == Map[j][i])
//{
// Temp = Dot;
//}
//else //if (' ' == Map[j][i])
//{
// Temp = Road;
//}
}
}
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Left:
XHuman = XHuman - 10;
Human = HumanL;
break;
case Keys.Right:
XHuman = XHuman + 10;
Human = HumanR;
break;
case Keys.Up:
YHuman = YHuman - 10;
Human = HumanB;
break;
case Keys.Down:
YHuman = YHuman + 10;
Human = HumanF;
break;
default:
return;
}
Refresh();
}
}
}
현재 움직이지않음
스트링은 변경할 수 없다. 따라서 스트링을 char형으로 바꿔주는것이 필요(그래야 로봇이나 박스가 움직임)
맵을 수정가능하게 바꾸는 과정, 맵은 키를 누를때 달라진다(@의 위치)
이제 움직일 수 있으나 복사된다(원래있던 @가 지워지지 않기 때문)
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
MapReal[YHuman][XHuman] = ' ';
switch (e.KeyCode)
{
case Keys.Left:
--XHuman;
Human = HumanL;
break;
case Keys.Right:
++XHuman;
Human = HumanR;
break;
case Keys.Up:
--YHuman;
Human = HumanB;
break;
case Keys.Down:
++YHuman;
Human = HumanF;
break;
default:
return;
}
MapReal[YHuman][XHuman] = '@';
Refresh();
}
}
}
switch문 위에 ' '를 넣어주면서 더이상 복사되지않음
-> 다지워짐 ㅎㅎ
박스가 밀린다.
using System.Windows.Forms;
using System.Drawing;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
const int WTileSize = 16;
const int HTileSize = 9;
const string Title = "푸시푸시 - 안드로이드의 모험";
char[][] MapReal;
string[] Map;
Image Human;
Image HumanF;
Image HumanL;
Image HumanR;
Image HumanB;
Image Wall;
Image Road;
Image Box;
Image Dot;
int WTile;
int HTile;
int XHuman;
int YHuman;
int XHumanOld;
int YHumanOld;
public Form1()
{
InitializeComponent();
Text = Title;
HumanF = new Bitmap(BrickGame.Properties.Resources.HumanF);
WTile = HumanF.Width;
HTile = HumanF.Height;
ClientSize = new Size(WTileSize * WTile, HTileSize * HTile);
HumanL = new Bitmap(BrickGame.Properties.Resources.HumanL);
HumanR = new Bitmap(BrickGame.Properties.Resources.HumanR);
HumanB = new Bitmap(BrickGame.Properties.Resources.HumanB);
Human = HumanF;
Wall = new Bitmap(BrickGame.Properties.Resources.Wall);
Road = new Bitmap(BrickGame.Properties.Resources.Road);
Box = new Bitmap(BrickGame.Properties.Resources.Box);
Dot = new Bitmap(BrickGame.Properties.Resources.Dot);
XHuman = 0;
YHuman = 0;
string[] TempMap = { "################",
"# # #",
"###### B . ###",
"#BBB #### ###",
"#### #### ##",
"# @ #",
"## B### #",
"#B ### #",
"################"
};
MapReal = new char[HTileSize][];
for(int i = 0; i<HTileSize;++i) //9줄 반복
{
MapReal[i] = TempMap[i].ToCharArray();
}
Map = TempMap;
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
string t = "test";
char[] ct = t.ToCharArray();
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 ' ':
Temp = Road;
break;
case '.':
Temp = Dot;
break;
case 'B':
Temp = Box;
break;
case '@':
Temp = Human;
XHuman=i;
YHuman=j;
Text = Title + "["+XHuman+","+YHuman+"]";
break;
}
e.Graphics.DrawImage(Temp, WTile * i, HTile * j);
//if ('#' == Map[j][i])
//{
// Temp = Wall;
//}
//else if ('B' == Map[j][i])
//{
// Temp = Box;
//}
//else if ('.' == Map[j][i])
//{
// Temp = Dot;
//}
//else //if (' ' == Map[j][i])
//{
// Temp = Road;
//}
}
}
}
private void Move()
{
if('#' == MapReal[YHuman][XHuman])
{
return;
}
if ('B' == MapReal[YHuman][XHuman])
{
MapReal[YHuman*2-YHumanOld][XHuman*2-XHumanOld] = 'B';
}
MapReal[YHumanOld][XHumanOld] = ' ';
MapReal[YHuman][XHuman] = '@';
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
YHumanOld = YHuman;
XHumanOld = XHuman;
switch (e.KeyCode)
{
case Keys.Left:
--XHuman;
Human = HumanL;
break;
case Keys.Right:
++XHuman;
Human = HumanR;
break;
case Keys.Up:
--YHuman;
Human = HumanB;
break;
case Keys.Down:
++YHuman;
Human = HumanF;
break;
default:
return;
}
Move();
Refresh();
}
}
}
'WinForm > 게임을 만들어보자' 카테고리의 다른 글
게임을 완성해보자! 끝! (0) | 2020.07.20 |
---|---|
7/14 게임을 만들어보자 2 <소코반> (0) | 2020.07.14 |
07/13 게임을 만들어보자 (0) | 2020.07.13 |