简体   繁体   中英

Permanently change canvas Postion in C# WPF

I am trying to make a Monopoly Game using WPF C# on Visual Studio. I Have a problem I have made a dice Class that gives me random values for 2 Dices.

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

And a Static Class Board to Store the Positions of each cell on the Board, also to get the current position of the players piece

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

and this is the function responsible for moving the player's current Position

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 problem is that the canvas always moves from the start position and doesn't continue on moving on the board

images are linked for further explanation.

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

I would have a Piece class. This would contain the current index of where it is located. It starts at say 0.
You roll 5.

Piece.Position += roll;

It then knows it's in position indexed by 5. Bind that via a converter or look it up and the piece goes to the location defined in Positions[5].
Next time you roll, you add the result to 5. Say that's 7. It's index becomes 12. That is then used to index the entry for it's location.

Don't use a piece of UI to retain the state, use a class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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