【问题标题】:Winforms C# Monopoly - Move player around boardWinforms C# Monopoly - 在棋盘上移动玩家
【发布时间】:2018-12-17 16:58:15
【问题描述】:

我正在尝试使用 winforms 在 C# 中创建一个垄断游戏,我需要处理玩家图标在棋盘上移动的问题。

我本来想这样做的;

    private void movePlayerToNewSquare(int playerPos)
    {
        int playerPosition = playerPos;

        switch (playerPosition)
        {
            case 0:
                playerIcon1.Location = pictureBox1.Location;
                break;
            case 1:
                playerIcon1.Location = pictureBox2.Location;
                break;

playerPos 来自较早的函数,是一个从 0 到 39 的整数,它们在棋盘上的位置是棋盘上所有方格列表中的那个数字,即 0 = "Go",1 = "Old Kent Road “等等。我正在考虑为板上的每个方格设置一个不同的案例。但这似乎是一种冗长的做事方式。

我想知道在 C# 中是否有一种方法可以使用 playerPosition 整数作为图片框后面的数字,可能类似于;

pictureBox(playerPosition).Location 

任何帮助将不胜感激

【问题讨论】:

  • 尝试使用>的字典。
  • 如果你有一个列表中的所有方块,那么只需使用列表索引:playerIcon1.Location = squares[playerPos].Location;
  • 如果你有他们滚动的数字,比如int diceValue,那么你可以这样做:playerIcon1.Location = squares[(currentPosition + diceValue) % 40].Location;

标签: c# winforms


【解决方案1】:

您可以尝试的一种方法是创建一个 GameSquare 类并从 PictureBox 继承。然后在 GameSquare 中创建一个 Id 属性,生成一个 ID 为 1 - 40 的 Gamesquares 列表。

向您的玩家类添加一个属性,以跟踪他们所在的方格并将位置与 GameSqaure 位置匹配。像这样的:

public class Player : PictureBox
{
    public int id { get; set; }
    public string name { get; set; }
    public int currentGameSquare { get; set; }
    //etc, etc
}
public class GameSquare : PictureBox
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Value { get; set; }
    //etc..etc.     
}

 public class Game
{
   private List<GameSquare> gameBoard;
   private Player p;

    //you're going to populate square values and title somewhere else in your code.
    Dictionary<string, int> squareValues = new Dictionary<string, int>();

    public Game()
    {
        gameBoard = new List<GameSquare>();
        p = new Player();

        GenerateGameBoard(40);
    }

   public void GenerateGameBoard(int numSquares)
   {
       for (int i = 0; i < gameBoard.Count(); i++)
        {
            GameSquare s = new GameSquare()
            {
                Id = i,
                Name = gameBoard.ElementAt(i).Key
                Value = gameBoard.ElementAt(i).Value
                Location = new Point(someX, someY)  //however your assigning the board layout
                //Assign the rest of the properties however you're doing it
            };
            gameBoard.Add(s);
        }
    }
}

现在,当玩家滚动时,您可以执行以下操作:

Random r = new Random();

int[] dice = new int[2];
dice[0] = r.Next(1,6);
dice[1] = r.Next(1,6);
movePlayertoNewSquare(dice);

private void movePlayerToNewSquare(int[] diceroll)
{
    p.currentGameSquare += diceroll.Sum();
    //You would need logic to determine if the player passed go and account for that

    p.Location = gameBoard.Where(x => x.id == p.currentGameSquare).Single().Location);

}

希望你能明白

【讨论】:

  • 我喜欢两次调用 r.Next 但 r.Next(1,6) 的想法,因为有两个骰子..如果两者相同,那么它的另一个滚动就像你得到一个加倍,如果连续 3 次加倍,则入狱。从技术上讲,因为你有两个骰子,最短/最小的移动是 2?那么它将是 Next(2,12)
  • 好点。我没有考虑双打之类的。我稍微编辑了代码以解决它
  • 感谢您的回复,唯一的问题是这是一个 uni 作业,我们还没有涵盖课程,预计在没有课程的情况下会这样做。
【解决方案2】:

我设法找到了一个简单的方法来做到这一点,虽然这只是为一个玩家设置的,所以我将来需要调整它。

    private void movePlayerToNewSquare(int playerPos)
    {
        int playerPosition = playerPos;

        Control[] pB = this.Controls.Find("pictureBox" + (playerPosition + 1).ToString(), true);

        playerIcon1.Location = pB[0].Location;
        player1Offset();
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-25
    • 1970-01-01
    • 1970-01-01
    • 2021-12-30
    • 1970-01-01
    相关资源
    最近更新 更多