Winforms C#垄断-在棋盘上移动玩家
我正试图用winforms在C#中创建一个垄断游戏,我需要处理在棋盘上移动的玩家图标 我正在考虑这样做Winforms C#垄断-在棋盘上移动玩家,c#,winforms,C#,Winforms,我正试图用winforms在C#中创建一个垄断游戏,我需要处理在棋盘上移动的玩家图标 我正在考虑这样做 private void movePlayerToNewSquare(int playerPos) { int playerPosition = playerPos; switch (playerPosition) { case 0: playerIcon1.Locatio
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后面的数字,可能类似于
pictureBox(playerPosition).Location
任何帮助都将不胜感激。您可以尝试的一种方法是创建GameSquare类并从PictureBox继承。然后在GameSquare中创建一个Id属性,生成一个Id为1-40的GameSquare列表 将属性添加到玩家类中,以跟踪它们是什么正方形,并将位置与GameSquare位置匹配。大概是这样的:
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);
}
希望你能理解我的想法,我设法找到了一个简单的方法来实现这一点,尽管这只是为一名球员设置的,所以我需要在将来调整它
private void movePlayerToNewSquare(int playerPos)
{
int playerPosition = playerPos;
Control[] pB = this.Controls.Find("pictureBox" + (playerPosition + 1).ToString(), true);
playerIcon1.Location = pB[0].Location;
player1Offset();
}
尝试使用字典。如果列表中有所有方块,则只需使用列表索引:
playerIcon1.Location=squares[playerPos].Location
如果你有他们滚动的数字,比如int diceValue
,那么你可以做:playerIcon1.Location=squares[(currentPosition+diceValue)%40]代码>我喜欢两次调用r.Next的想法,但是r.Next(1,6)因为有两个骰子..如果两个骰子都是一样的,那么当你得到一个双骰子时,它就会像另一个骰子一样滚动,如果它连续3个双骰子就会进监狱。从技术上讲,当你有两个骰子时,最短/最小的移动是2?因此,这将是下一个(2,12)好的观点。我没有考虑过双打和其他什么。我对代码进行了一点编辑,以解释itThanks对响应的影响,唯一的问题是这是一个uni赋值,我们还没有涵盖类,预计在没有类的情况下也会这样做。