按照面向对象方法,对游戏进行细分,可以分为小区域和大块,大块由小区域组成.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;
namespace TT
{
class Square
{
//属性
public Point Location; //方块出现的位置
public Size size; //方块大小
public Color ForeColor;
public Color BackColor;
//构造方法,由于大小和颜色是固定的,所以可以在开始时就初始化
public Square(Size InitialSize, Color InitialBackColor, Color InitialForeColor)
{
size = InitialSize;
BackColor = InitialBackColor;
ForeColor = InitialForeColor;
}
//用前景色画方块
public void Show(System.IntPtr WinHandle)
{
Graphics GameGraphics;
GraphicsPath graphPath;
PathGradientBrush brushSqure;
Color[] surroundColor;
Rectangle rectSquare;
GameGraphics = Graphics.FromHwnd(WinHandle);
//梯度颜色
graphPath = new GraphicsPath();
rectSquare = new Rectangle(Location.X, Location.Y, size.Width, size.Height);
graphPath.AddRectangle(rectSquare);
brushSqure = new PathGradientBrush(graphPath);
brushSqure.CenterColor = ForeColor;
surroundColor = new Color[] { BackColor };
brushSqure.SurroundColors = surroundColor;
GameGraphics.FillPath(brushSqure, graphPath);
}
//用背景色画方块
public void Hide(System.IntPtr WinHandle)
{
Graphics GameGraphics;
Rectangle rectSquare;
GameGraphics = Graphics.FromHwnd(WinHandle);
rectSquare = new Rectangle(Location.X, Location.Y, size.Width, size.Height);
GameGraphics.FillRectangle(new SolidBrush(GameField.BackColor), rectSquare);
}
}
}
注:这个为最新类,添加了梯度路径画笔,便方块更加美观.