C语言俄罗斯方块程序设计 第3页

C语言俄罗斯方块程序设计 第3页
using System;
using System.Drawing;

namespace AnotherBlock
{
 /// <summary>
 /// Summary description for Blocks.
 /// </summary>
 public class Blocks
 {
  private short width;
  private short height;
  private bool[,] shape;
  private short top;
  private short left;
  private BlockTypes blockType = BlockTypes.undefined;

  Image basicBlockImage = Image.FromFile("images/block.gif");

  private const int blockImageWidth = 10;
  private const int blockImageHeight = 10;

  public Blocks(BlockTypes blockType)
  {
   this.BlockType = blockType;
  }

  public Blocks()
  {
   Random randomGenerator = new Random();
   BlockTypes randomBlockType = (BlockTypes)(randomGenerator.Next(1, 8));

   this.BlockType = randomBlockType;
  }

  public short Top
  {
   get
   {
    return top;
   }

   set
   {
    if ((value > -1) && (value < 21))
    {
     top = value;
     if (top == 0)
     {
      top = 1;
     }
    }
    else
    {
     throw new ArgumentException("Property Top must be between 1 and 20.");
    }
   }
  }

  public short Left
  {
   get
   {
    return left;
   }

   set
   {
    if ((value > -1) && (value < 10))
    {
     left = value;
    }
    else
    {
     throw new ArgumentException("Property Left must be between 0 and 9.");
    }
   }
  }

  public short Width
  {
   get
   {
    return width;
   }

   set
   {
    if ((value > 0) && (value < 5))
    {
     width = value;
    }
    else
    {
     throw new ArgumentException("Propert Width must be between 1 and 4.");
    }
   }
  }

  public short Height
  {
   get
   {
    return height;
   }

   set
   {
    if ((value > 0) && (value < 5))
    {
     height = value;
    }
    else
    {
     throw new ArgumentException("Property Height must be between 1 and 4.");
    }
   }
  }

  public bool[,] Shape
  {
   get
   {
    return shape;
   }

   set
   {
    if (value.Length == width * height)
    {
     shape = value;
    }
    else
    {
     throw new InvalidOperationException("Specified index length does not match block dimensions.");
    }
   }
  }

  public BlockTypes BlockType
  {
   get
   {
    return blockType;
   }

   set
   {
    bool[,] shape;

    if (blockType == BlockTypes.undefined)
    {
     blockType = value;

     switch(blockType)
     {
      case BlockTypes.straight:
       this.Width = 1;
       this.Height = 4;
       this.Top = 0;
       this.Left = 5;
       shape = new bool[this.Width, this.Height];
       shape[0, 0] = true;
       shape[0, 1] = true;
       shape[0, 2] = true;
       shape[0, 3] = true;
       this.Shape = shape;
       break;

      case BlockTypes.rectangle:
       this.Width = 2;
       this.Height = 2;
       this.Top = 0;
       this.Left = 4;
       shape = new bool[this.Width, this.Height];
       shape[0, 0] = true;
       shape[0, 1] = true;
       shape[1, 0] = true;
       shape[1, 1] = true;
       this.Shape = shape;
       break;

      case BlockTypes.cornerleft:
       this.Width = 3;
       this.Height = 2;
       this.Top = 0;
       this.Left = 4;
       shape = new bool[this.Width, this.Height];
       shape[0, 0] = true;
       shape[1, 0] = true;
       shape[2, 0] = true;
       shape[0, 1] = true;
       shape[1, 1] = false;
       shape[2, 1] = false;
       this.Shape = shape;
       break;

      case BlockTypes.cornerright:
       this.Width = 3;
       this.Height = 2;
       this.Top = 0;
       this.Left = 4;
       shape = new bool[this.Width, this.Height];
       shape[0, 0] = true;
       shape[1, 0] = true;
       shape[2, 0] = true;
       shape[0, 1] = false;
       shape[1, 1] = false;
       shape[2, 1] = true;
       this.Shape = shape;
       break;

      case BlockTypes.cross:
       this.Width = 3;
       this.Height = 2;
       this.Top = 0;
       this.Left = 4;
       shape = new bool[this.Width, this.Height];
       shape[0, 0] = true;
       shape[1, 0] = true;
       shape[2, 0] = true;
       shape[0, 1] = false;
       shape[1, 1] = true;
       shape[2, 1] = false;
       this.Shape = shape;
       break;

      case BlockTypes.zigzag1:
       this.Width = 2;
       this.Height = 3;
       this.Top = 0;
       this.Left = 4;
       shape = new bool[this.Width, this.Height];
       shape[0, 0] = false;
       shape[1, 0] = true;
       shape[0, 1] = true;
       shape[1, 1] = true;
       shape[0, 2] = true;
       shape[1, 2] = false;
       this.Shape = shape;
       break;

      case BlockTypes.zigzag2:
       this.Width = 2;
       this.Height = 3;
       this.Top = 0;
       this.Left = 4;
       shape = new bool[this.Width, this.Height];
       shape[0, 0] = true;
       shape[1, 0] = false;
       shape[0, 1] = true;
       shape[1, 1] = true;
       shape[0, 2] = false;
       shape[1, 2] = true;
       this.Shape = shape;
       break;
     }
    }
    else
    {
     throw new InvalidOperationException("The property BlockType can't be changed once defined the first time.");
    }
   }
  }

  public void Draw(Graphics drawingSurface)
  {
   for(int i = 0; i < this.Width; i++)
   {
    for(int j = 0; j < this.Height; j++)
    {
     if (this.Shape[i, j] == true)
     {
      Rectangle rect = new Rectangle((this.Left + i) * blockImageWidth, (this.Top + j) * blockImageHeight, blockImageWidth, blockImageHeight);
      drawingSurface.DrawImage(basicBlockImage, rect);

上一页  [1] [2] [3] 

Copyright © 2007-2012 www.chuibin.com 六维论文网 版权所有