일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
- 스마트폰
- 쉐어포인트코리아
- 윈모데브
- MIX10
- windows mobile 6.5
- 윈도우폰7
- 실버라이트 코리아
- 지승욱
- 데브피아
- 윈도우모바일
- 소년포비소프트
- 윈도데브
- 루나네스
- 신동혁
- 김춘배
- 훈스닷넷
- UX베이커리
- 거제도
- 소년포비
- 윈도우 모바일
- 안드로이드
- 옴니아2
- winmodev
- 서진호
- 주신영
- 헤이맨
- 신석현
- 윈도우폰
- 황광진
- 마이크로소프트
- Today
- Total
소년포비의 세계정복!!
Windows Moblie 퍼즐 게임 4 본문
마지막으로 퍼즐 게임이 진행되는 폼이다.
1. frmgame 폼 디자인
- 원본 보기을 위한 picturebox
- 이동 횟수를 출력하기 위한 lable
- 메뉴
2. frmgame.designer.cs
public int PuzzleCnt; // 퍼즐 갯수를 담기 위한
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.PictureBox[,] PuzzlePiece;// 퍼즐 조각을 담기 위한
private System.Windows.Forms.PictureBox whiteimg; // 퍼즐 이동할때 조각 이미지 잔상이 남아서 그것을 대처 하기 위한 이미지 ... 다른 방법을 못 찾아서 꽁수를 썼다
private System.Windows.Forms.PictureBox whiteimgt;// 세로 가로 구분했다
public int PuzzleImageWidth ; // 퍼즐 조각 width
public int PuzzleImageHeight; // 퍼즐 조각 height
private int PuzzlePieceSize;// 퍼즐 조각 size
private System.Drawing.Point SpacePuzzle; // 퍼즐 빈 공간의 위치
private System.Drawing.Point[,] PuzzleInGrid; // 퍼즐 조각 위치
private System.Drawing.Point[,] GridOfPuzzle;
private int MoveCnt = 0;// 이동 횟수
public frmsltgame frmimg;
private System.Drawing.Graphics dc; // 퍼즐 조각을 만들기 위한 graphics
public string PuzzleFile; // 퍼즐 이미지 파일 이름
public int PuzzleSpeed; // 퍼즐 이동 속도
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
public frmht fht;
private System.Windows.Forms.PictureBox pictureBox1;
3. frmgame 폼 소스
- using System.IO; 추가
public void game_setting(int pnt)// 퍼즐 게임을 설정 하기 위한
{
PuzzleCnt = pnt;
PuzzlePiece = new System.Windows.Forms.PictureBox[PuzzleCnt, PuzzleCnt]; // 퍼즐 조각 갯수 만큼 배열을 잡는다.
PuzzlePieceSize = PuzzleImageWidth / PuzzleCnt;
PuzzleInGrid = new Point[PuzzleCnt, PuzzleCnt]; ;
GridOfPuzzle = new Point[PuzzleCnt, PuzzleCnt]; ;
}
private void frmgame_Load(object sender, EventArgs e)
{
// 원본 보기 처리.
fht = new frmht();
fht.pictureBox1.Image = new Bitmap(PuzzleFile);
fht.Show();
fht.Visible = false;
// 퍼즐 조각 출발점
int Setx = 0;
int Sety = 0;
// 퍼즐 이동 속도를 위해
switch (PuzzleSpeed)
{
case 0:
PuzzleSpeed = PuzzlePieceSize;
break;
case 1:
PuzzleSpeed = 4;
break;
case 2:
PuzzleSpeed = 2;
break;
}
string path = GetApplication();
// 원본 이미지를 조각을 내야함으로 bitmap에 담는다
Bitmap DefaultImage = new Bitmap(PuzzleFile);
// 퍼즐 조각 사이즈 만큼의 크기의 bitmap를 생성한다.
Bitmap SpeceImage = new Bitmap(PuzzlePieceSize, PuzzlePieceSize);
// 이동 할때 잔상을 없게 하기 위한 이미지 생성
whiteimg = new System.Windows.Forms.PictureBox();
whiteimg.Location = new System.Drawing.Point(0, 0);
whiteimg.Size = new System.Drawing.Size(4, PuzzlePieceSize);
whiteimg.Name = "whiteimg";
whiteimg.Image = new Bitmap(Path.Combine(path,"whitebmp.jpg"));
this.Controls.Add(whiteimg);
whiteimg.Visible = false;
whiteimgt = new System.Windows.Forms.PictureBox();
whiteimgt.Location = new System.Drawing.Point(0, 0);
whiteimgt.Size = new System.Drawing.Size(PuzzlePieceSize,4);
whiteimgt.Name = "whiteimg";
whiteimgt.Image = new Bitmap(Path.Combine(path,"whitebmpt.jpg"));
this.Controls.Add(whiteimgt);
whiteimgt.Visible = false;
// 퍼즐 조각 내기
for (int Px = 0; Px < PuzzleCnt; Px++)
{
for (int Py = 0; Py < PuzzleCnt; Py++)
{
if ((Px == PuzzleCnt - 1) && (Py == PuzzleCnt - 1))
{
PuzzleInGrid[Px, Py].X = Px;
PuzzleInGrid[Px, Py].Y = Py;
GridOfPuzzle[Px, Py].X = Px;
GridOfPuzzle[Px, Py].Y = Py;
break;
}
// 퍼즐 조각 이미지를 생성하는 부분이다. 처음 작성한 코드는 속도가 너무 느려서 아래와 같은 방법으로 했다.)
SpeceImage = PicImageCreate(DefaultImage, PuzzlePieceSize, Px * PuzzlePieceSize, Py * PuzzlePieceSize);
// 조각 이미지 생성
PuzzlePiece[Px, Py] = new System.Windows.Forms.PictureBox();
PuzzlePiece[Px, Py].Location = new System.Drawing.Point(Setx + Px * PuzzlePieceSize, Sety + Py * PuzzlePieceSize);
PuzzlePiece[Px, Py].Size = new System.Drawing.Size(PuzzlePieceSize, PuzzlePieceSize);
PuzzlePiece[Px, Py].Name = "PuzzlePiece" + Px.ToString() + Py.ToString();
PuzzlePiece[Px, Py].Tag = Px + PuzzleCnt * Py;
PuzzlePiece[Px, Py].Image = new Bitmap(SpeceImage);
PuzzlePiece[Px, Py].MouseDown += new System.Windows.Forms.MouseEventHandler(this.PuzzlePiececlick);
this.Controls.Add(PuzzlePiece[Px, Py]);
PuzzleInGrid[Px, Py].X = Px;
PuzzleInGrid[Px, Py].Y = Py;
GridOfPuzzle[Px, Py].X = Px;
GridOfPuzzle[Px, Py].Y = Py;
}
}
SpacePuzzle.X = PuzzleCnt - 1;
SpacePuzzle.Y = PuzzleCnt - 1;
Mixing(); // 퍼즐 조각 섞는 함수.
label1.Text = "이동 횟수 :";
}
// 퍼즐 조각 섞는 함수.
public void Mixing()
{
int DelayStartTime = 0;
int Mvalue = PuzzleCnt - 1;
for (int p = 0; p < Mvalue; p++)
{
for (int SqX = 0; SqX < Mvalue - p % Mvalue; SqX++)
MovePiece(0, DelayStartTime);
for (int SqY = 0; SqY < Mvalue - p % Mvalue; SqY++)
MovePiece(2, DelayStartTime);
for (int SqX = 0; SqX < Mvalue - p % Mvalue; SqX++)
MovePiece(1, DelayStartTime);
for (int SqY = 0; SqY < Mvalue - p % Mvalue; SqY++)
MovePiece(3, DelayStartTime);
}
Random r = new Random((int)System.DateTime.Now.Ticks);
int LL = 0;
LL = (PuzzleCnt * 10) + (int)(r.NextDouble() * PuzzleCnt);
int Count = 0;
int CrossStep = 0;
while (Count < LL)
{
int Direction = (int)(r.NextDouble() * 2);
if (CrossStep == 1) Direction += 2;
CrossStep ^= 1;
if (MovePiece(Direction, DelayStartTime)) Count++;
}
MoveCnt = 0;
this.label2.Text = MoveCnt.ToString();
}
// 퍼즐 조각 클릭 이벤트 처리 함수
private void PuzzlePiececlick(object sender, System.Windows.Forms.MouseEventArgs e)
{
System.Windows.Forms.PictureBox t = (System.Windows.Forms.PictureBox)sender;
int x, y;
int v = (int)t.Tag;
x = v % PuzzleCnt;
y = v / PuzzleCnt;
if ((SpacePuzzle.X == GridOfPuzzle[x, y].X + 1) && (SpacePuzzle.Y == GridOfPuzzle[x, y].Y))
MovePiece(0, 0);
else
if ((SpacePuzzle.X == GridOfPuzzle[x, y].X - 1) && (SpacePuzzle.Y == GridOfPuzzle[x, y].Y))
MovePiece(1, 0);
else
if ((SpacePuzzle.X == GridOfPuzzle[x, y].X) && (SpacePuzzle.Y == GridOfPuzzle[x, y].Y + 1))
MovePiece(2, 0);
else
if ((SpacePuzzle.X == GridOfPuzzle[x, y].X) && (SpacePuzzle.Y == GridOfPuzzle[x, y].Y - 1))
MovePiece(3, 0);
if (CheckCompletion()) this.label1.Text = "성공!!!";
else
{
MoveCnt++;
this.label2.Text = MoveCnt.ToString();
}
}
// 퍼즐 조각 이동 처리 .
private bool MovePiece(int Direction, int DelayStartTime)
{
bool Moved = false;
switch (Direction)
{
case 0:
if (SpacePuzzle.X > 0)
{
Point PiecePos
= PuzzleInGrid[SpacePuzzle.X, SpacePuzzle.Y]
= PuzzleInGrid[SpacePuzzle.X - 1, SpacePuzzle.Y];
whiteimg.Visible = true ;
whiteimg.Left = PuzzlePiece[PiecePos.X, PiecePos.Y].Left ;
whiteimg.Top = PuzzlePiece[PiecePos.X, PiecePos.Y].Top ;
whiteimg.Refresh();
for (int p = 0; p < PuzzlePieceSize / PuzzleSpeed; p++)
{
PuzzlePiece[PiecePos.X, PiecePos.Y].Left = PuzzlePiece[PiecePos.X, PiecePos.Y].Left + PuzzleSpeed;
whiteimg.Left = whiteimg.Left + PuzzleSpeed;
PuzzlePiece[PiecePos.X, PiecePos.Y].Refresh();
whiteimg.Refresh();
}
GridOfPuzzle[PiecePos.X, PiecePos.Y] = SpacePuzzle;
SpacePuzzle.X--;
System.Threading.Thread.Sleep(DelayStartTime);
Moved = true;
whiteimg.Visible = false;
}
break;
case 1:
if (SpacePuzzle.X < PuzzleCnt - 1)
{
Point PiecePos
= PuzzleInGrid[SpacePuzzle.X, SpacePuzzle.Y]
= PuzzleInGrid[SpacePuzzle.X + 1, SpacePuzzle.Y];
whiteimg.Visible = true;
whiteimg.Left = PuzzlePiece[PiecePos.X, PiecePos.Y].Right-4;
whiteimg.Top = PuzzlePiece[PiecePos.X, PiecePos.Y].Top;
whiteimg.Refresh();
for (int p = 0; p < PuzzlePieceSize / PuzzleSpeed; p++)
{
PuzzlePiece[PiecePos.X, PiecePos.Y].Left = PuzzlePiece[PiecePos.X, PiecePos.Y].Left - PuzzleSpeed;
whiteimg.Left = whiteimg.Left - PuzzleSpeed;
PuzzlePiece[PiecePos.X, PiecePos.Y].Refresh();
whiteimg.Refresh();
}
GridOfPuzzle[PiecePos.X, PiecePos.Y] = SpacePuzzle;
SpacePuzzle.X++;
System.Threading.Thread.Sleep(DelayStartTime);
Moved = true;
whiteimg.Visible = false;
}
break;
case 2:
if (SpacePuzzle.Y > 0)
{
Point PiecePos
= PuzzleInGrid[SpacePuzzle.X, SpacePuzzle.Y]
= PuzzleInGrid[SpacePuzzle.X, SpacePuzzle.Y - 1];
whiteimgt.Visible = true;
whiteimgt.Left = PuzzlePiece[PiecePos.X, PiecePos.Y].Left;
whiteimgt.Top = PuzzlePiece[PiecePos.X, PiecePos.Y].Top ;
whiteimgt.Refresh();
for (int p = 0; p < PuzzlePieceSize / PuzzleSpeed; p++)
{
PuzzlePiece[PiecePos.X, PiecePos.Y].Top = PuzzlePiece[PiecePos.X, PiecePos.Y].Top + PuzzleSpeed;
whiteimgt.Top = whiteimgt.Top + PuzzleSpeed;
PuzzlePiece[PiecePos.X, PiecePos.Y].Refresh();
whiteimgt.Refresh();
}
GridOfPuzzle[PiecePos.X, PiecePos.Y] = SpacePuzzle;
SpacePuzzle.Y--;
System.Threading.Thread.Sleep(DelayStartTime);
Moved = true;
whiteimgt.Visible = false;
}
break;
case 3:
if (SpacePuzzle.Y < PuzzleCnt - 1)
{
Point PiecePos
= PuzzleInGrid[SpacePuzzle.X, SpacePuzzle.Y]
= PuzzleInGrid[SpacePuzzle.X, SpacePuzzle.Y + 1];
whiteimgt.Visible = true;
whiteimgt.Left = PuzzlePiece[PiecePos.X, PiecePos.Y].Left;
whiteimgt.Top = PuzzlePiece[PiecePos.X, PiecePos.Y].Bottom-4 ;
whiteimgt.Refresh();
for (int p = 0; p < PuzzlePieceSize / PuzzleSpeed; p++)
{
PuzzlePiece[PiecePos.X, PiecePos.Y].Top = PuzzlePiece[PiecePos.X, PiecePos.Y].Top - PuzzleSpeed;
whiteimgt.Top = whiteimgt.Top - PuzzleSpeed;
PuzzlePiece[PiecePos.X, PiecePos.Y].Refresh();
whiteimgt.Refresh();
}
GridOfPuzzle[PiecePos.X, PiecePos.Y] = SpacePuzzle;
SpacePuzzle.Y++;
System.Threading.Thread.Sleep(DelayStartTime);
Moved = true;
whiteimgt.Visible = false;
}
break;
}
return Moved;
}
// 퍼즐 조각 완성 체크.
private bool CheckCompletion()
{
for (int Px = 0; Px < PuzzleCnt; Px++)
for (int Py = 0; Py < PuzzleCnt; Py++)
{
if ((Px == PuzzleCnt - 1) && (Py == PuzzleCnt - 1)) continue;
if ((Px == GridOfPuzzle[Px, Py].X) && (Py == GridOfPuzzle[Px, Py].Y))
{
continue;
}
else
{
return false;
}
}
PictureBox pic1 = new System.Windows.Forms.PictureBox();
pic1.Location = new System.Drawing.Point(0, 0);
pic1.Size = new System.Drawing.Size(480, 480);
pic1.Name = "spic";
pic1.Tag = PuzzleCnt * PuzzleCnt * PuzzleCnt;
pic1.Image = new Bitmap(PuzzleFile);
this.Controls.Add(pic1);
return true;
}
private void menuItem1_Click(object sender, EventArgs e)
{
this.Close();
}
private void menuItem2_Click(object sender, EventArgs e)
{
Application.Exit();
}
// 퍼즐 조각 이미지 만들기 함수.
private Bitmap PicImageCreate(Image dimg, int size ,int sx , int sy)
{
Rectangle rct; // 퍼즐 조각 만들 Rectangle.
Rectangle srct;// 퍼즐 원본에서 자를 Rectangle
Bitmap bm; // 퍼즐 조각 Bitmap
rct = new Rectangle(0, 0, size, size); // 퍼즐 조각 이미지 생성
srct = new Rectangle(sx, sy, size, size);
bm = new Bitmap(size, size);
dc = Graphics.FromImage(bm);
dc.DrawImage(dimg, rct,srct,GraphicsUnit.Pixel); // 퍼즐 조각 이미지를 그리는 함수다 (원본이미지,생성 Rectangle,원본에서 추출할 Rectangle,Pixel).
return bm;
}
public string GetApplication()
{
return System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly()
.GetModules()[0].FullyQualifiedName);
}
private void pictureBox1_Click(object sender, EventArgs e)
{
fht.Visible = true;
}
이상으로 허접한 Windows Moblie 퍼즐 게임 소스을 올려 봤다...
나중에 또 다른 것을 만든다면 그때도 소스를 올릴까 한다.
그리고 새로 습득한 내용에 대해서도 시간 될때마다 작성해 볼 생각이다.
'윈도우폰 세상 > Windows Phone' 카테고리의 다른 글
TreeControl 구현 Class (0) | 2009.09.27 |
---|---|
Windows CE Form 고정 시키기 (0) | 2009.09.27 |
Windows Moblie 퍼즐 게임 3 (0) | 2009.09.27 |
Windows Moblie 퍼즐 게임 2 (0) | 2009.09.27 |
Windows Mobile 퍼즐 게임 (0) | 2009.09.27 |