重绘后图形不会改变

时间:2016-10-25 10:53:27

标签: c# winforms visual-studio graphics pacman

我在Visual Studio中使用c#制作一个简单的Pacman游戏(使用Windows Forms)。

Pacman

Hovewer,我无法弄清楚为什么当pacman吃药时图像不会改变。

我的代码片段:

PacmanGame.cs

protected override void OnPaint(PaintEventArgs e)
        {
        Graphics dc = e.Graphics;
        _maze.DrawMaze(dc);
        // + some other code
        base.OnPaint(e);
        }
private void timeGameLoop_Tick(object sender, EventArgs e)
    {
    _character.Move(); //pacman's movement
    Invalidate();
     // + some other code
    }

Maze.cs

public List<string> lines { get; set; }
private bool[,] eaten; // true if pill was eaten

public void DrawMaze(Graphics gfx)
        {
            for (int i = 0; i < rows; i++)
            {
                for (int x = 0; x < columns; x++)
                {
                    // 2 indicates pill (white)
                    if (lines[i][x] == '2' && eaten[i, x] == false)
                    {
                        gfx.FillEllipse(Brushes.White, new Rectangle(x * 2 - 6, i * 2 - 6, 12, 12));
                    }

                    // 3 indicates power pill (red)
                    else if (lines[i][x] == '3' && eaten[i, x] == false)
                    {
                        gfx.FillEllipse(Brushes.Red, new Rectangle(x * 2 - 6, i * 2 - 6, 15, 15));
                    }
                }
            }

        }
public void Eat(int row, int column)
        {
            eaten[row, column] = true;
        }

Pacman.cs

public void Move()
{
      // Left - x of pacman's position, Top - y of pacman's position
      if (_maze.lines[Left][Top] == '2' || _maze.lines[Left][Top] == '3')
      {
            _maze.Eat(Left, Top);
      }

      //other code to continue moving...
 }

在pacman吃掉它们之后,地图不会更新(吃过的药片不会消失)。我该如何解决?

0 个答案:

没有答案