刚才写了好多好多,特别有感觉的时候,结果没有保存,全没了,哇,难受的要命。
好了言归正传,今天是第三天了,今天来来实现,方块类:GameBlock,说以下这个类的功能吧:
1.这个类描述了俄罗斯方块中的下落方块的属性,首先对于下落的方块有X坐标,Y坐标,方块的宽度Width,方块的高度Height,下落的速度speed,形状BlockDeformation,颜色Color,最最重要的还需要一个数组用来存储已经下落完成的方块,至于为什么属性的修饰符这么定:
2.首先,对于方块的大小和数组是确定的,不能改变的,在整个游戏中都要访问,有什么办法可以快速访问呢,一个是可以声明为静态的,这样可以通过类名调用它比如:GameBlock.Width这种形式,还有一个是声明为接口,这样可以通过实现接口来调用它,非常方便,其次对于这个数组,首先他的大小是通过你设计的游戏窗口除以方块大小得到的。
3.对于颜色和形状为啥用枚举,由于颜色和形状是早就确定的,使用枚举可以避免掉无意义的编号,提高了程序的可读性,见名知意,推荐大家使用这个。
4.其次要将此方块对象绘制在游戏控件上,如何绘制呢,我们知道在窗口加载时需要调用Paint方法绘制窗体,那么,可以知道,在控件加载时也会有一个方法来绘制控件,那么我们可以重写控件的Paint方法,private void pictureBoxGame_Paint(object sender, PaintEventArgs e)将我们的方块绘制上去,由于,要绘图,必须得到Graphiceh画布,如何得到呢,可以查文档得到,PaintEventArgs e中的e可以获得,aphics g = e.Graphics;//获取绘图对象,这样我们可以在GameBlock中创建一个方法接受这个对象,在这个画布上绘制,代码如下,
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 俄罗斯方块
{
enum BlockColor
{
Red = 1,//红
Blue,//蓝
Yellow,//黑
Green,//绿
}
enum BlockDeformation
{
AVerticalBar,//竖线
HorizontalLine,//横线
UpTriangle,//开口向上0的三角形
DownTriangle,//开口向下的三角形
LeftTriangle,//开口向左的三角形
RightTriangle,//开口向右的三角形
FourRectangular//四个小正方形组成的大正方形
}
/// <summary>
/// 游戏方块
/// </summary>
class GameBlock
{
public static readonly int width = 20;//游戏方块宽度
public static readonly int height = 20;//游戏方块高度
public static BlockColor[,] array = new BlockColor[13,23];//存贮游戏方块的数组
private int X;
private int Y;
private int speed = 1;
private BlockColor color = BlockColor.Yellow ;//方块的颜色,枚举
private BlockDeformation defor = BlockDeformation.UpTriangle;//方块的形状,枚举
//空参构造
public GameBlock() { }
//有参构造
public GameBlock(int x, int y)
{
X1 = x;
Y1 = y;
}
//封装字段
public int X1 { get => X; set => X = value; }
public int Y1 { get => Y; set => Y = value; }
public int Speed { get => speed; set => speed = value; }
internal BlockColor Color { get => color; set => color = value; }
internal BlockDeformation Defor { get => defor; set => defor = value; }
//将方块绘制在窗口
public RectangleF[] DrawBlockToWindow(Graphics g)
{
//制定画布样式
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
//需要绘制的矩形数组
RectangleF[] rectangleFs = null;
//绘制方块,十以内的随机数确定变化的外形,四以内的随机数确定变化的颜色,
switch (this.Defor)
{
case BlockDeformation.AVerticalBar:
rectangleFs = new RectangleF[] {
new RectangleF(this.X1, this.Y1, width, height),
new RectangleF(this.X1, this.Y1+1*height, width, height),
new RectangleF(this.X1, this.Y1+2*height,width, height),
new RectangleF(this.X1, this.Y1+3*height, width, height)};
g.FillRectangles(GetRandomColor(this.Color), rectangleFs);
return rectangleFs;
case BlockDeformation.HorizontalLine:
rectangleFs = new RectangleF[] {
new RectangleF(this.X1, this.Y1, width, height),
new RectangleF(this.X1+height, this.Y1, width, height),
new RectangleF(this.X1+2*height, this.Y1,width, height),
new RectangleF(this.X1+3*height, this.Y1, width, height)};
g.FillRectangles(GetRandomColor(this.Color), rectangleFs);
return rectangleFs;
case BlockDeformation.UpTriangle:
rectangleFs = new RectangleF[] {
new RectangleF(this.X1, this.Y1, width, height),
new RectangleF(this.X1, this.Y1+height, width, height),
new RectangleF(this.X1-width, this.Y1+height,width, height),
new RectangleF(this.X1+width, this.Y1+height, width, height)};
g.FillRectangles(GetRandomColor(this.Color), rectangleFs);
return rectangleFs;
case BlockDeformation.DownTriangle:
rectangleFs = new RectangleF[] {
new RectangleF(this.X1, this.Y1, width, height),
new RectangleF(this.X1+width, this.Y1, width, height),
new RectangleF(this.X1+2*width,this.Y1,width, height),
new RectangleF(this.X1+width, this.Y1+height, width, height)};
g.FillRectangles(GetRandomColor(this.Color), rectangleFs);
return rectangleFs;
case BlockDeformation.LeftTriangle:
rectangleFs = new RectangleF[] {
new RectangleF(this.X1, this.Y1, width, height),
new RectangleF(this.X1, this.Y1+1*height, width, height),
new RectangleF(this.X1, this.Y1+2*height,width, height),
new RectangleF(this.X1-width, this.Y1+height, width, height)};
g.FillRectangles(GetRandomColor(this.Color), rectangleFs);
return rectangleFs;
case BlockDeformation.RightTriangle:
rectangleFs = new RectangleF[] {
new RectangleF(this.X1, this.Y1, width, height),
new RectangleF(this.X1, this.Y1+1*height, width, height),
new RectangleF(this.X1, this.Y1+2*height,width, height),
new RectangleF(this.X1+width, this.Y1+height, width, height)};
g.FillRectangles(GetRandomColor(this.Color), rectangleFs);
return rectangleFs;
case BlockDeformation.FourRectangular:
rectangleFs = new RectangleF[] {
new RectangleF(this.X1, this.Y1, width, height),
new RectangleF(this.X1, this.Y1+1*height, width, height),
new RectangleF(this.X1+width, this.Y1+2*height,width, height),
new RectangleF(this.X1+width, this.Y1+height, width, height)};
g.FillRectangles(GetRandomColor(this.Color), rectangleFs);
return rectangleFs;
default:
rectangleFs = new RectangleF[] {
new RectangleF(this.X1, this.Y1, width, height),
new RectangleF(this.X1, this.Y1+1*height, width, height),
new RectangleF(this.X1, this.Y1+2*height,width, height),
new RectangleF(this.X1, this.Y1+3*height, width, height)};
g.FillRectangles(GetRandomColor(this.Color), rectangleFs);
return rectangleFs;
}
}
private Brush GetRandomColor(BlockColor xcolor)
{
Brush brush;
//获取随机颜色值
switch (xcolor)
{
case BlockColor.Red:
brush = Brushes.Red;
break;
case BlockColor.Blue:
brush = Brushes.Blue;
break;
case BlockColor.Green:
brush = Brushes.Green;
break;
case BlockColor.Yellow:
brush = Brushes.Yellow;
break;
default:
brush = null;
break;
}
return brush;
}
}
}
/* */
Graphics g = e.Graphics;//获取绘图对象
Brush brush;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
RectangleF[] rectangles = gameBlock.DrawBlockToWindow(g);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;这句的意思是说明在画布上做图的质量,
System.Drawing.Drawing2D.SmoothingMode.AntiAlias这是一个系统自定义常量,意思是抗锯齿的 , g.FillRectangles(GetRandomColor(this.Color), rectangleFs);意思是画一个填充矩形,为什么不用drawRectangles,这个是画矩形框不填充,填充色可以通过 brush = Brushes.Blue;对象指定。