public Form1() { InitializeComponent(); mover.Interval = 500; mover.Tick += Mover_Tick; for (int x = 0; x <= squares.GetUpperBound(0); x++) { for (int y = 0; y <= squares.GetUpperBound(1); y++) { squares[x, y] = new Square { BackColor = SystemColors.ActiveCaption, BorderStyle = BorderStyle.FixedSingle, Location = new Point(x * 40, y * 40), Name = "square", Size = new Size(40, 40), TabIndex = 2, TabStop = false, X = x, Y = y, Pathed = false }; squares[x, y].MouseClick += Form1_MouseClick; this.Controls.Add(squares[x, y]); } } this.Size = new Size(56 + (40 * squares.GetUpperBound(0)), 79 + (40 * squares.GetUpperBound(1))); }
// used to draw the game public void OnDrawing(Tetris.GameBase.TetrisGame game, Tetris.GameBase.TetrisGame.DrawEventArgs e) { Square[,] image = game.Image; if (imageCache == null) { imageCache = new int[image.GetUpperBound(0) + 1, image.GetUpperBound(1) + 1]; Array.Clear(imageCache, 0, (image.GetUpperBound(0) + 1) * (image.GetUpperBound(1) + 1)); } int i, j; for (i = 0; i < game.Height; i++) { for (j = 0; j < game.Width; j++) { // only draw the changed part if ((image[i, j] == null && imageCache[i, j] != 0) || (image[i, j] != null && image[i, j].Color + image[i, j].SubId != imageCache[i, j])) { imageCache[i, j] = image[i, j] == null ? 0 : image[i, j].Color + image[i, j].SubId; try { squaresMatrix[i, j].Dispatcher.Invoke( new Action( delegate { /* * squaresMatrix[i, j].Fill = * new SolidColorBrush( * colorMap[ * image[i, j] == null * ? 0 * : (image[i, j].Color < colorMap.Length * ? image[i, j].Color * : colorMap.Length - 1)]); */ squaresMatrix[i, j].Fill = SquareGenerator.brushClone( brushesMap[ image[i, j] == null ? 0 : (image[i, j].Color + image[i, j].SubId < brushesMap.Length ? image[i, j].Color + +image[i, j].SubId : brushesMap.Length - 1)]); } )); } catch { } } } } }
public SquareArray(Square[,] _squares) // 生成以传入数组为Storage的SquareArray { Storage = _squares; _m = Storage.GetUpperBound(0); _n = Storage.GetUpperBound(1); }