반응형
소스 코드
GameRule.cs
namespace 테트리스_만들기
{
static class GameRule
{
internal const int B_WIDTH = 30;//게임 X좌표 1의 Pixel수
internal const int B_HEIGHT = 30;//게임 Y좌표 1의 Pixel수
internal const int BX = 12;//게임 보드의 폭(B_WIDTH*BX Pixels)
internal const int BY = 20;//게임 보드의 높이(B_HEIGHT*BX Pixels)
internal const int SX = 4;//시작 S 좌표
internal const int SY = 0; //시작 Y 좌표
}
}
Diagram.cs
namespace 테트리스_만들기
{
class Diagram
{
internal int X
{
get;
private set;
}
internal int Y
{
get;
private set;
}
internal Diagram()
{
Reset();
}
internal void Reset()
{
X = GameRule.SX;
Y = GameRule.SY;
}
internal void MoveLeft()
{
X--;
}
internal void MoveRight()
{
X++;
}
internal void MoveDown()
{
Y++;
}
}
}
Game.cs
using System.Drawing;
namespace 테트리스_만들기
{
class Game
{
Diagram now;
internal Point NowPosition
{
get
{
if(now == null)
{
return new Point(0, 0);
}
return new Point(now.X, now.Y);
}
}
#region 단일체
internal static Game Singleton
{
get;
private set;
}
static Game()
{
Singleton = new Game();
}
Game()
{
now = new Diagram();
}
#endregion
internal bool MoveLeft()
{
if(now.X>0)
{
now.MoveLeft();
return true;
}
return false;
}
internal bool MoveRight()
{
if ((now.X+1) <GameRule.BX)
{
now.MoveRight();
return true;
}
return false;
}
internal bool MoveDown()
{
if ((now.Y+1) < GameRule.BY)
{
now.MoveDown();
return true;
}
return false;
}
}
}
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;
SetClientSizeCore(bx * bwidth, by * bheight);
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
DrawGraduation(e.Graphics);
DrawDiagram(e.Graphics);
}
private void DrawDiagram(Graphics graphics)
{
Pen dpen = new Pen(Color.Red, 4);
Point now = game.NowPosition;
Rectangle now_rt = new Rectangle(now.X * bwidth+2, now.Y * 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 = st.Y;
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()
{
}
private void MoveDown()
{
if(game.MoveDown())
{
Region rg = MakeRegion(0, -1);
Invalidate(rg);
}
}
private void MoveLeft()
{
if (game.MoveLeft())
{
Region rg = MakeRegion(1, 0);
Invalidate(rg);
}
}
private void MoveRight()
{
if (game.MoveRight())
{
Region rg = MakeRegion(-1, 0);
Invalidate(rg);
}
}
private Region MakeRegion(int cx, int cy)
{
Point now = game.NowPosition;
Rectangle rect1 = new Rectangle(now.X * bwidth, now.Y * bheight, bwidth, bheight);
Rectangle rect2 = new Rectangle((now.X+cx) * bwidth, (now.Y+cy) * bheight, bwidth, bheight);
Region rg1 = new Region(rect1);
Region rg2 = new Region(rect2);
rg1.Union(rg2);
return rg1;
}
private void timer1_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# 프로젝트] 테트리스 만들기 - Part 2. 벽돌 모양 정의하기, 회전 (0) | 2020.04.17 |