📄 block.cs
字号:
using System;
using System.Drawing;
namespace AnotherBlock
{
/// <summary>
/// Represents a Tetris block.
/// </summary>
public class Block
{
/// <summary>
/// Private attribute that holds the current width of the block (in block units).
/// </summary>
private short width;
/// <summary>
/// Private attribute that holds the current height of the block (in block units).
/// </summary>
private short height;
/// <summary>
/// Private attribute that holds the shape of the block.
/// </summary>
private Brick[,] shape;
/// <summary>
/// Private attribute that holds the top position of the block in the play field (in block units).
/// </summary>
private short top;
/// <summary>
/// Private attribute that holds the left position of the block in the play field (in block units).
/// </summary>
private short left;
/// <summary>
/// Private attribute that holds the block type of the block.
/// </summary>
private BlockType blockType = BlockType.Undefined;
/// <summary>
/// Private attribute that holds the image #1 of the block unit.
/// </summary>
private Image blockImage01 = Image.FromFile("images/block01.gif");
/// <summary>
/// Private attribute that holds the image #2 of the block unit.
/// </summary>
private Image blockImage02 = Image.FromFile("images/block02.gif");
/// <summary>
/// Private attribute that holds the image #3 of the block unit.
/// </summary>
private Image blockImage03 = Image.FromFile("images/block03.gif");
/// <summary>
/// Private attribute that holds the image #4 of the block unit.
/// </summary>
private Image blockImage04 = Image.FromFile("images/block04.gif");
/// <summary>
/// Private attribute that holds the image #5 of the block unit.
/// </summary>
private Image blockImage05 = Image.FromFile("images/block05.gif");
/// <summary>
/// Private attribute that holds the image #6 of the block unit.
/// </summary>
private Image blockImage06 = Image.FromFile("images/block06.gif");
/// <summary>
/// Private attribute that holds the image #7 of the block unit.
/// </summary>
private Image blockImage07 = Image.FromFile("images/block07.gif");
/// <summary>
/// Private attribute that holds the image #8 of the block unit.
/// </summary>
private Image blockImage08 = Image.FromFile("images/block08.gif");
/// <summary>
/// Private attribute that holds the image #9 of the block unit.
/// </summary>
private Image blockImage09 = Image.FromFile("images/block09.gif");
/// <summary>
/// Private attribute that holds the image #10 of the block unit.
/// </summary>
private Image blockImage10 = Image.FromFile("images/block10.gif");
/// <summary>
/// Private attribute that holds the image #11 of the block unit.
/// </summary>
private Image blockImage11 = Image.FromFile("images/block11.gif");
/// <summary>
/// Private attribute that holds the image #12 of the block unit.
/// </summary>
private Image blockImage12 = Image.FromFile("images/block12.gif");
/// <summary>
/// Private attribute that holds the image #13 of the block unit.
/// </summary>
private Image blockImage13 = Image.FromFile("images/block13.gif");
/// <summary>
/// Private attribute that holds the image #14 of the block unit.
/// </summary>
private Image blockImage14 = Image.FromFile("images/block14.gif");
/// <summary>
/// Class constructor that creates a block with a random block type.
/// </summary>
public Block()
{
// Creates a random number generator.
Random randomGenerator = new Random();
// Randomizes the block type.
BlockType randomBlockType = (BlockType)(randomGenerator.Next(1, 8));
// Sets the block type.
this.BlockType = randomBlockType;
}
/// <summary>
/// Class constructor that creates a block with a given block type.
/// </summary>
/// <param name="blockType">Block type of the new block being created.</param>
public Block(BlockType blockType)
{
// Sets the block type.
this.BlockType = blockType;
}
/// <summary>
/// Property that holds and sets the top position of the block in the play field (in block units).
/// </summary>
/// <remarks>
/// Value must be between 0 and the playing field height.
/// </remarks>
public short Top
{
get
{
// Returns the value in the top attribute.
return top;
}
set
{
// Checks if the value is within the allowed range.
if ((value > -1) && (value < (Game.PlayingFieldHeight + 1)))
{
// The value is within the allowed range.
// Sets the top attribute to the value.
top = value;
}
else
{
// The value is not within the allowed range.
// Throws an argument exception with the error.
throw new ArgumentException("Property Top must be between 0 and " + Game.PlayingFieldHeight.ToString() + ".");
}
}
}
/// <summary>
/// Property that holds and sets the left position of the block in the play field (in block units).
/// </summary>
/// <remarks>
/// Value must be between 0 and the playing field width, taking into consideration the block width.
/// </remarks>
public short Left
{
get
{
// Returns the value in the left attribute.
return left;
}
set
{
// Checks if the value is within the allowed range.
if ((value > -1) && ((value + this.Width) <= Game.PlayingFieldWidth))
{
// The value is within the allowed range.
// Sets the left attribute to the value.
left = value;
}
else
{
// The value is not within the allowed range.
// Throws an argument exception with the error.
throw new ArgumentException("Property Left must be between 0 and " + ((int)(Game.PlayingFieldWidth - this.Width)).ToString() + ".");
}
}
}
/// <summary>
/// Property that holds and sets the current width of the block (in block units).
/// </summary>
/// <remarks>
/// Value must be between 1 and 4.
/// </remarks>
public short Width
{
get
{
// Returns the value in the width attribute.
return width;
}
set
{
// Checks if the value is within the allowed range.
if ((value > 0) && (value < 5))
{
// The value is within the allowed range.
// Sets the width attribute to the value.
width = value;
}
else
{
// The value is not within the allowed range.
// Throws an argument exception with the error.
throw new ArgumentException("Property Width must be between 1 and 4.");
}
}
}
/// <summary>
/// Property that holds and sets the current height of the block (in block units).
/// </summary>
/// <remarks>
/// Value must be between 1 and 4.
/// </remarks>
public short Height
{
get
{
// Returns the value in the height attribute.
return height;
}
set
{
// Checks if the value is within the allowed range.
if ((value > 0) && (value < 5))
{
// The value is within the allowed range.
// Sets the width attribute to the value.
height = value;
}
else
{
// The value is not within the allowed range.
// Throws an argument exception with the error.
throw new ArgumentException("Property Height must be between 1 and 4.");
}
}
}
/// <summary>
/// Property that holds and sets the shape of the block.
/// </summary>
public Brick[,] Shape
{
get
{
// Returns the value in the shape attribute.
return shape;
}
set
{
// Checks if the length of the value to be set is right, according the the width and height of the block.
if (value.Length == width * height)
{
// The value is right.
// Sets the shape attribute to the value.
shape = value;
}
else
{
// The value is not right.
// Throws an argument exception with the error.
throw new ArgumentException("Specified index length does not match block dimensions.");
}
}
}
/// <summary>
/// Property that holds and sets the block type.
/// </summary>
/// <remarks>
/// The block type can be set only one time.
/// </remarks>
public BlockType BlockType
{
get
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -