永久更改C#WPF中的画布位置

时间:2018-04-14 17:45:46

标签: c# wpf

我正在尝试使用Visual Studio上的WPF C#制作垄断游戏。 我有一个问题我已经制作了一个骰子类,它给了我2个骰子的随机值。

Dice.Cs

    namespace WpfApp1
 {
    /// <summary>
    /// class that throws two dices in the game
    /// </summary>
  public class Dice
     {
    public int FirstDice { get; set; }
    public int SecondDice { get; set; }
    public bool HasBeenThrown { get; set; }
    public Random Random { get; set; }


    public Dice()
    {
        HasBeenThrown = false;
        Random = new Random();
    }

    public int ThrowDice(Image m1,Image m2)
    {
        FirstDice = Random.Next(1, 6);
        SecondDice = Random.Next(1, 6);
        string s1 = FirstDice.ToString() + ".png";
        string s2 = SecondDice.ToString() + ".png";
        BitmapImage bi3 = new BitmapImage();
        BitmapImage bi2 = new BitmapImage();
        bi2.BeginInit();
        bi2.UriSource = new Uri(s2, UriKind.Relative);
        bi2.EndInit();
        //---------------------------------------------
        bi3.BeginInit();
        bi3.UriSource = new Uri(s1, UriKind.Relative);
        bi3.EndInit();
        //--------------------------------------------
        m1.Stretch = Stretch.Fill;
        m1.Source = bi3;
        m2.Stretch = Stretch.Fill;
        m2.Source = bi2;

        return (FirstDice + SecondDice);
    }

    public bool IsDouble()
    {
        return (FirstDice == SecondDice);
    }
}
}

还有一个静态类板,可以将每个单元的位置存储在板上,也可以获得玩家当前的位置

Board.cs

namespace WpfApp1
 {

public class Pair<T, U>
{
    public Pair()
    {
    }

    public Pair(T first, U second)
    {
        this.First = first;
        this.Second = second;
    }

    public T First { get; set; }
    public U Second { get; set; }
};
public static class Board
{
    public static Tuple<int, int>[] Postions =
        {
         Tuple.Create(533,590),
         Tuple.Create(533,540),
         Tuple.Create(533,478),
         Tuple.Create(533,423),
         Tuple.Create(533,368),
         Tuple.Create(533,313),
         Tuple.Create(533,258),
         Tuple.Create(533,203),
         Tuple.Create(533,148),
         Tuple.Create(533,93),
         Tuple.Create(533,40),
         Tuple.Create(430,40),
         Tuple.Create(380,40),
         Tuple.Create(330,40),
         Tuple.Create(280,40),
         Tuple.Create(227,40),
         Tuple.Create(180,40),
         Tuple.Create(127,40),
         Tuple.Create(80,40),
         Tuple.Create(40,40),
         Tuple.Create(40,88),
         Tuple.Create(40,148),
         Tuple.Create(40,203),
         Tuple.Create(40,258),
         Tuple.Create(40,313),
         Tuple.Create(40,373),
         Tuple.Create(40,428),
         Tuple.Create(40,478),
         Tuple.Create(40,530),
         Tuple.Create(40,590),
         Tuple.Create(80,590),
         Tuple.Create(130,590),
         Tuple.Create(180,590),
         Tuple.Create(230,590),
         Tuple.Create(283,590),
         Tuple.Create(333,590),
         Tuple.Create(383,590),
         Tuple.Create(433,590),
         Tuple.Create(483,590)

        };


    public static int FindPos(double top, double left, ref Canvas c)
    {

        int i;
        for (i = 0; i < 40; i++)
        {
            if (Canvas.GetTop(c) == top && Canvas.GetLeft(c) == left)
            {
                return i;
            }
        }
        return i;
    }
}
}

这是负责移动玩家当前位置的功能

public static void Move(int num,ref Canvas c)
    {
        double left = Canvas.GetLeft(c);
        double top = Canvas.GetTop(c);
        int current=Board.FindPos(top, left,ref c);

        Canvas.SetLeft(c,Board.Postions[current+num].Item2);
        Canvas.SetTop(c,Board.Postions[current+num].Item1);
    }

问题是画布总是从起始位置移动而不是 继续推进董事会

链接图像以进一步说明。

The green piece is in the starting postion

After one click it moved 5 positions as it should have done.

as you can see it started counting the moves from the Go cell but I want it to continue from where it stopped and I can't do it

1 个答案:

答案 0 :(得分:0)

我会有一个课程。这将包含它所在位置的当前索引。 从0开始 你滚5。

Piece.Position += roll;

然后它知道它的位置由5索引。 通过转换器将其绑定或查找它,并将该块移至位置[5]中定义的位置 下次滚动时,将结果添加到5.说出7.它的索引变为12.然后用它来索引它的位置条目。

不要使用一段UI来保留状态,使用一个类。