2012-03-02 3 views
0

"Arxl"オブジェクトの配列を通してすべてのループで描画されるはずのブロックを取得する方法を私の人生に与えることはできませんグリッド全体をアニメーション化します。格子がグリッドを越えてアニメーション化しない理由を解決できない

私のコードを完成させるために誰かを探すのではなく、本当に感謝しています。新鮮な目のセット。あなたのタイマーで

public partial class Game : Form 
{ 
    //attributes 
    private Bitmap _grid; 
    private Arxl[,] _cartesianGrid; 
    private int _arxlAmount; 
    const int ARXL = 4; 

    public Game() 
    { 
     InitializeComponent(); 
     _arxlAmount = (gridPictureBox.Height/ARXL);//in case height/arxl is not an even number? 

     _cartesianGrid = new Arxl[_arxlAmount, _arxlAmount]; 
     _grid = new Bitmap(gridPictureBox.Width, gridPictureBox.Height); 

     int x; 
     int y; 

     for (x = 0; x < _arxlAmount; x++) 
     { 
      for (y = 0; y < _arxlAmount; y++) 
      { 
       _cartesianGrid[x, y] = new Arxl(); 
      } 
     } 

     SetSeed(_cartesianGrid); 
    } 
    private void SetSeed(Arxl[,] cartesianGrid) 
    { 
     _cartesianGrid[1, 1].Active = true; 
    } 

    private void DrawArxl(Bitmap _grid, Arxl[,] cartesianGrid,int arxlAmount) 
    { 
     int x, y; 
     x=0; 
     y=0; 
     Graphics graphics = Graphics.FromImage(_grid); 

     graphics.Clear(Color.White); 
     for (x = 1; x < arxlAmount;x++) 
     { 
      for (y = 1; y < arxlAmount; y++) 
      { 
       if (cartesianGrid[x, y].Active==true) 
       { 
        cartesianGrid[x, y].Area = new Rectangle(x * ARXL, y * ARXL, ARXL, ARXL); 
        graphics.FillRectangle(Brushes.Black, cartesianGrid[x, y].Area); 
       } 
       else if(cartesianGrid[x,y].Active==false) 
       { 
        Pen newPen=new Pen(Color.Black); 
        cartesianGrid[x, y].Area = new Rectangle(x * ARXL, y * ARXL, ARXL, ARXL); 
        graphics.DrawRectangle(newPen,cartesianGrid[x, y].Area); 
        newPen.Dispose(); 
       } 
      } 
     } 
    } 

    private void timer_Tick(object sender, EventArgs e) 
    { 
     //GameOfLife(_cartesianGrid, _arxlAmount); 
     ScrollBlock(_cartesianGrid, _arxlAmount); 
     DrawArxl(_grid, _cartesianGrid, _arxlAmount); 
     gridPictureBox.Image = _grid; 
    } 
    private void ScrollBlock(Arxl[,] cartesianGrid, int arxlAmount) 
    { 
     int x = 0; 
     int y = 0; 
     for (x = 0; x < arxlAmount; x++) 
     { 
      for (y = 0; y < arxlAmount; y++) 
      { 
       if (_cartesianGrid[x, y].Active == true) 
       { 
        if (x>=0) 
        { 
         if (x == (arxlAmount-1)) 
         { 
          _cartesianGrid[x, y].Active = false; 
          _cartesianGrid[1, y].Active = true; 
         } 
         else if(x<(arxlAmount-1)) 
         { 
          _cartesianGrid[x, y].Active = false; 
          _cartesianGrid[x+1, y].Active = true; 
         } 
        } 
       } 
      } 
     } 
    } 
+0

を描きますstackoverflowを使用していくつかのポインタ;私はそれを使用して、怠惰にしようとしたり、誰かを怒らせようとするのではなく、かなり新しいです。 –

+0

これでデバッグしようとしましたか?あなたは何を見つけましたか? – 0x434D53

+1

@ user40062:人々が投票アップに投票するのを心配しないで、私は投票のためだけに投票しましたが、投票するのはルール違反です。投票ダウンは、あなたの質問が間違っていることを意味するのではなく、誰かがそれを気に入らなかった(または理由を書いていない)ことを意味します。 WPFでアニメーションをやった後であれば、Winformsははるかに簡単です。また、あなたの質問があまりにも一般的かもしれない、より具体的にしようとする。 – Arjang

答えて

0

私はあなたの問題を発見しました。

問題は、セルの位置を更新しています(この特定の初期状態では右に移動しています)。しかし、forループの次の反復では、以前の反復から更新された状態が見つかるため、サイクルが停止すると、セルは最初のセルの位置にスクロールされ、その間に再ペイントは行われませんでした。

同じ「アクティブなドット」を複数回更新するのを避けるために、グリッドスキャンが完了した後にオンにする必要のあるセルを有効にするUpdateListを追加するようにコードを修正しています。これは、左から右へ動くドットを示すはずです。

private void ScrollBlock(Arxl[,] cartesianGrid, int arxlAmount) { 
     int x = 0; 
     int y = 0; 
     List<Point> updateList = new List<Point>(); 
     for(x = 0; x < arxlAmount; x++) { 
      for(y = 0; y < arxlAmount; y++) { 
       if(_cartesianGrid[x, y].Active == true) { 
        if(x >= 0) { 
        if(x == (arxlAmount - 1)) { 
         _cartesianGrid[x, y].Active = false; 
         //_cartesianGrid[1, y].Active = true; 
         updateList.Add(new Point(1, y)); 
        } else if(x < (arxlAmount - 1)) { 
         _cartesianGrid[x, y].Active = false; 
         //_cartesianGrid[x + 1, y].Active = true; 
         updateList.Add(new Point(x + 1, y)); 
        } 
        } 
       } 

      } 
     } 
     foreach(var pt in updateList) { 
      _cartesianGrid[pt.X, pt.Y].Active = true; 
     } 
     } 
+0

関連のない問題では、cartesianGridをパラメータとして渡している代わりに_cartesianGridフィールドを更新していることが奇妙です。パラメータが不要であるか、フィールドを直接更新する代わりにパラメータを更新する必要があります。がんばろう! –

+0

属性がすべて同じクラスに属していることが分かりました。私はその機能がクラス外にあると思った。パラメータを指摘してくれてありがとう。 –

+0

私が理解したように、私は結合しようとしていたスキャン機能からアップデート機能を分離しました。あなたのソリューションは完璧に動作します。あなたは本当に感謝しています。これは理想的な結果です。 –

0

あなたがのPictureBoxに画像を割り当てた後gridPictureBox.Invalidate()を呼び出してみてください。これにより、ピクチャボックスが強制的に再描画されます。

1

あなたのコードのコメントによれば、ライフゲームをプログラムしたいと思っています。変更されていない古い状態から新しい状態を計算する必要があるため、セルを変更すると機能しません。したがって、現在の状態と新しい状態の2つのゲームボードを持つ必要があります。新しいボードを作成するのではなく、2枚のボードを交換して交換する方が良いでしょう。さらに、ボードにRectanglesを格納することに意味はありません。したがって、私はブール行列としてボードを宣言します。

const int CellSize = 4; 

private int _boardSize; 
private bool[,] _activeBoard, _inactiveBoard; 
Bitmap _grid; 

フォームコンストラクタは、私たちは、タイマーティックがこの

ScrollBlock(); 
DrawGrid(); 
を行い

private void SetSeed() 
{ 
    _activeBoard[0, 0] = true; 
    _activeBoard[7, 4] = true; 
    DrawGrid(); 
} 

(例として)このようなゲームを初期化し、この

public Game() 
{ 
    InitializeComponent(); 
    _boardSize = Math.Min(gridPictureBox.Width, gridPictureBox.Height)/CellSize; 
    _grid = new Bitmap(gridPictureBox.Width, gridPictureBox.Height); 
    _activeBoard = new bool[_boardSize, _boardSize]; 
    _inactiveBoard = new bool[_boardSize, _boardSize]; 
    SetSeed(); 
} 

のように変更されました

のロジックは全く新しいものです。我々は_activeBoardの状態を見て、_inactiveBoardの状態を設定します。その後、2枚のボードを交換します。

private void ScrollBlock() 
{ 
    for (int x = 0; x < _boardSize; x++) { 
     for (int y = 0; y < _boardSize; y++) { 
      if (_activeBoard[x, y]) { 
       _activeBoard[x, y] = false; 
       int newX = x + 1; 
       int newY = y; 
       if (newX == _boardSize) { 
        newX = 0; 
        newY = (newY + 1) % _boardSize; 
       } 
       _inactiveBoard[newX, newY] = true; 
      } 
     } 
    } 
    SwapBoards(); 
} 

ボードは、単にこの

private void SwapBoards() 
{ 
    bool[,] tmp = _activeBoard; 
    _activeBoard = _inactiveBoard; 
    _inactiveBoard = tmp; 
} 

そして最後にDrawGridのように交換される代わりにダウンこの質問を投票あなたが私を与えることができれば、それは本当にいただければ幸いです_activeBoard

private void DrawGrid() 
{ 
    Graphics graphics = Graphics.FromImage(_grid); 
    graphics.Clear(Color.White); 
    for (int x = 0; x < _boardSize; x++) { 
     for (int y = 0; y < _boardSize; y++) { 
      var rect = new Rectangle(x * CellSize, y * CellSize, CellSize, CellSize); 
      if (_activeBoard[x, y]) { 
       graphics.FillRectangle(Brushes.Black, rect); 
      } else { 
       graphics.DrawRectangle(Pens.Black, rect); 
      } 
     } 
    } 
    gridPictureBox.Image = _grid; 
} 
+0

情報をありがとう、私は現時点であなたのコードを使って試作品を作っています。 –

+0

プロトタイプを完成し、それは素晴らしい作品です。セルの3次元配列とブール値を含む別のクラスを作成しています。 ご協力いただきありがとうございます。 –

+0

クラス内の矩形についての点は特に真です。 –

関連する問題