繁体   English   中英

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

[英]Permanently change canvas Postion in C# WPF

我正在尝试在Visual Studio上使用WPF C#制作一个大富翁游戏。 我有一个问题,我做了一个骰子类,可以给我2个骰子的随机值。

骰子

    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);
    }

问题是画布始终从起始位置移动,而不会在板上继续移动

图像链接在一起以进行进一步说明。

绿色部分在起始位置

一键单击后,它移动了5个位置,这应该已经完成​​。

如您所见,它开始计算Go单元格中的移动,但是我希望它从停止的位置继续,而我做不到

我要上个棋子课。 这将包含其所在位置的当前索引。 从0开始。
你滚5。

Piece.Position += roll;

然后,它知道它的位置在5索引处。通过转换器将其绑定或查找它,然后零件将到达Positions [5]中定义的位置。
下次滚动时,将结果添加到5。假设为7。其索引变为12。然后将其用于为其位置索引条目。

不要使用UI来保留状态,而要使用类。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM