반응형
소스 코드
GameRule.cs
namespace 도형_이동
{
class GameRule
{
internal const int B_WIDTH = 30;
internal const int B_HEIGHT = 30;
internal const int BX = 12;
internal const int BY = 20;
internal const int SX = 4;
internal const int SY = 0;
}
}
BlockValue.cs
namespace 도형_이동
{
static class BlockValue
{
static public readonly int[,,,] bvals = new int[7, 4, 4, 4]
{
{
{
{ 0, 0, 1, 0 },
{ 0, 0, 1, 0 },
{ 0, 0, 1, 0 },
{ 0, 0, 1, 0 }
},
{
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
{ 1, 1, 1, 1 },
{ 0, 0, 0, 0 }
},
{
{ 0, 0, 1, 0 },
{ 0, 0, 1, 0 },
{ 0, 0, 1, 0 },
{ 0, 0, 1, 0 }
},
{
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
{ 1, 1, 1, 1 },
{ 0, 0, 0, 0 }
}
},
{
{
{ 0, 0, 0, 0 },
{ 0, 1, 1, 0 },
{ 0, 1, 1, 0 },
{ 0, 0, 0, 0 }
},
{
{ 0, 0, 0, 0 },
{ 0, 1, 1, 0 },
{ 0, 1, 1, 0 },
{ 0, 0, 0, 0 }
},
{
{ 0, 0, 0, 0 },
{ 0, 1, 1, 0 },
{ 0, 1, 1, 0 },
{ 0, 0, 0, 0 }
},
{
{ 0, 0, 0, 0 },
{ 0, 1, 1, 0 },
{ 0, 1, 1, 0 },
{ 0, 0, 0, 0 }
}
},
{
{
{ 0, 0, 0, 0 },
{ 0, 1, 1, 0 },
{ 0, 0, 1, 0 },
{ 0, 0, 1, 0 }
},
{
{ 0, 0, 0, 0 },
{ 0, 0, 0, 1 },
{ 0, 1, 1, 1 },
{ 0, 0, 0, 0 }
},
{
{ 0, 0, 0, 0 },
{ 0, 0, 1, 0 },
{ 0, 0, 1, 0 },
{ 0, 0, 1, 1 }
},
{
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
{ 0, 1, 1, 1 },
{ 0, 1, 0, 0 }
}
},
{
{
{ 0, 0, 0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 0, 0, 0, 0 },
{ 0, 0, 1, 0 },
{ 0, 0, 1, 1 },
{ 0, 0, 0, 1 }
}
}
};
}
}
Diagram.cs
using System;
namespace 도형_이동
{
internal class Diagram
{
internal int X
{
get;
private set;
}
internal int Y
{
get;
private set;
}
internal int Turn
{
get;
private set;
}
internal int BlockNum
{
get;
private set;
}
internal Diagram()
{
ReSet();
}
internal void ReSet()
{
Random rand = new Random();
X = GameRule.SX;
Y = GameRule.SY;
Turn = rand.Next()%4;
BlockNum = rand.Next() % 7;
}
internal void MoveLeft()
{
X--;
}
internal void MoveRight()
{
X++;
}
internal void MoveDown()
{
Y++;
}
internal void MoveTurn()
{
Turn = (Turn + 1) % 4;
}
}
}
Game.cs
using System.Drawing;
namespace 도형_이동
{
class Game
{
Diagram now;
internal Point NowPosition
{
get
{
return new Point(now.X, now.Y);
}
}
internal int BlockNum
{
get
{
return now.BlockNum;
}
}
internal int Turn
{
get
{
return now.Turn;
}
}
internal static Game Singleton
{
get;
private set;
}
static Game()
{
Singleton = new Game();
}
Game()
{
now = new Diagram();
}
internal bool MoveLeft()
{
for(int xx=0;xx<4;xx++)
{
for(int yy=0;yy<4;yy++)
{
if(BlockValue.bvals[now.BlockNum,Turn, xx,yy]!=0)
{
if (now.X + xx <= 0)
{
return false;
}
}
}
}
now.MoveLeft();
return true;
}
internal bool MoveRight()
{
for (int xx = 0; xx < 4; xx++)
{
for (int yy = 0; yy < 4; yy++)
{
if (BlockValue.bvals[now.BlockNum, Turn, xx, yy] != 0)
{
if ((now.X + xx+1) >= GameRule.BX)
{
return false;
}
}
}
}
now.MoveRight();
return true;
}
internal bool MoveDown()
{
for (int xx = 0; xx < 4; xx++)
{
for (int yy = 0; yy < 4; yy++)
{
if (BlockValue.bvals[now.BlockNum, Turn, xx, yy] != 0)
{
if ((now.Y + yy + 1) >=GameRule.BY)
{
return false;
}
}
}
}
now.MoveDown();
return true;
}
internal bool MoveTurn()
{
for (int xx = 0; xx < 4; xx++)
{
for (int yy = 0; yy < 4; yy++)
{
if (BlockValue.bvals[now.BlockNum, (Turn+1)%4, xx, yy] != 0)
{
if (((now.X + xx) < 0)|| ((now.X + xx) >= GameRule.BX)||((now.Y+yy)>=GameRule.BY))
{
return false;
}
}
}
}
now.MoveTurn();
return true;
}
internal void Next()
{
now.ReSet();
}
}
}
Form1.cs
using System;
using System.Drawing;
using System.Windows.Forms;
namespace 도형_이동
{
public partial class Form1 : Form
{
Game game;
int bx;
int by;
int bwidth;
int bheight;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
game = Game.Singleton;
bx = GameRule.BX;
by = GameRule.BY;
bwidth = GameRule.B_WIDTH;
bheight = GameRule.B_HEIGHT;
this.SetClientSizeCore(GameRule.BX * GameRule.B_WIDTH, GameRule.BY * GameRule.B_HEIGHT);
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
DoubleBuffered = true;
DrawGraduation(e.Graphics);
DrawDiagram(e.Graphics);
}
private void DrawDiagram(Graphics graphics)
{
Pen dpen = new Pen(Color.Red, 4);
Point now = game.NowPosition;
int bn = game.BlockNum;
int tn = game.Turn;
for(int xx=0;xx<4;xx++)
{
for(int yy=0;yy<4;yy++)
{
if(BlockValue.bvals[bn,tn,xx,yy]!=0)
{
Rectangle now_rt = new Rectangle((now.X+xx) * bwidth + 2, (now.Y+yy) * bheight + 2, bwidth - 4, bheight - 4);
graphics.DrawRectangle(dpen, now_rt);
//graphics.FillRectangle(Brushes.Green, now_rt);
}
}
}
}
private void DrawGraduation(Graphics graphics)
{
DrawHorizons(graphics);
DrawVerticals(graphics);
}
private void DrawVerticals(Graphics graphics)
{
Point st = new Point();
Point et = new Point();
for (int cx = 0; cx < bx; cx++)
{
st.X = cx * bwidth;
st.Y = 0;
et.X = st.X;
et.Y = by * bheight;
graphics.DrawLine(Pens.Purple, st, et);
}
}
private void DrawHorizons(Graphics graphics)
{
Point st = new Point();
Point et = new Point();
for (int cy = 0; cy < by; cy++)
{
st.X = 0;
st.Y = cy * bheight;
et.X = bx * bwidth;
et.Y = cy * bheight;
graphics.DrawLine(Pens.Green, st, et);
}
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
switch(e.KeyCode)
{
case Keys.Right: MoveRight(); return;
case Keys.Left: MoveLeft(); return;
case Keys.Space: MoveDown();return;
case Keys.Up: MoveTurn();return;
}
}
private void MoveTurn()
{
if(game.MoveTurn())
{
Region rg = MakeRegion();
Invalidate(rg);
}
}
private void MoveDown()
{
if(game.MoveDown())
{
Region rg = MakeRegion(0, -1);
Invalidate(rg);
}
else
{
game.Next();
Invalidate();
}
}
private void MoveLeft()
{
if (game.MoveLeft())
{
Region rg = MakeRegion(1, 0);
Invalidate(rg);
}
}
private Region MakeRegion(int cx, int cy)
{
Point now = game.NowPosition;
int bn = game.BlockNum;
int tn = game.Turn;
Region region = new Region();
for (int xx = 0; xx < 4; xx++)
{
for (int yy = 0; yy < 4; yy++)
{
if (BlockValue.bvals[bn, tn, xx, yy] != 0)
{
Rectangle rect1 = new Rectangle((now.X + xx) * bwidth + 2, (now.Y + yy) * bheight + 2, bwidth - 4, bheight - 4);
Rectangle rect2 = new Rectangle((now.X + cx+xx) * bwidth, (now.Y + cy+yy) * bheight, bwidth, bheight);
Region rg1 = new Region(rect1);
Region rg2 = new Region(rect2);
region.Union(rg1);
region.Union(rg2);
}
}
}
return region;
}
private Region MakeRegion()
{
Point now = game.NowPosition;
int bn = game.BlockNum;
int tn = game.Turn;
int oldtn = (tn + 3) % 4;
Region region = new Region();
for (int xx = 0; xx < 4; xx++)
{
for (int yy = 0; yy < 4; yy++)
{
if (BlockValue.bvals[bn, tn, xx, yy] != 0)
{
Rectangle rect1 = new Rectangle((now.X + xx) * bwidth + 2, (now.Y + yy) * bheight + 2, bwidth - 4, bheight - 4);
Region rg1 = new Region(rect1);
region.Union(rg1);
}
if (BlockValue.bvals[bn, oldtn, xx, yy] != 0)
{
Rectangle rect1 = new Rectangle((now.X + xx) * bwidth + 2, (now.Y + yy) * bheight + 2, bwidth - 4, bheight - 4);
Region rg1 = new Region(rect1);
region.Union(rg1);
}
}
}
return region;
}
private void MoveRight()
{
if (game.MoveRight())
{
Region rg = MakeRegion(-1, 0);
Invalidate(rg);
}
}
private void timer_down_Tick(object sender, EventArgs e)
{
MoveDown();
}
}
}
반응형
'프로젝트 > 테트리스 만들기' 카테고리의 다른 글
[C# 프로젝트] 테트리스 만들기 (0) | 2022.06.23 |
---|---|
[.NET C# 프로젝트] 테트리스 만들기 – Part 4(Final). 꽉 찬 라인 지우기, 종료 체크 (0) | 2020.04.17 |
[.NET C# 프로젝트] 테트리스 만들기 Part3. 도형 쌓기 (0) | 2020.04.17 |
[.NET C# 프로젝트] 테트리스 만들기 - Part1. 도형 이동시키기 (0) | 2020.04.17 |