您好最近我已经获得了创建小游戏的任务,但是看到我遇到了问题。当我试图移动我的球员,他开始在一个固定的位置,他将向右移动一次,但似乎他仍然锁定在起始位置,并在输入数字后再次向右移动,同样的答案(坐标我有打印出来)是相同的,游戏板也是一样的。 https://gyazo.com/9f23419a3300232d3f3d9e1168202cf3
public string player(int movement)
{
int playerX = map.GetLength(0)-3; //rows
int playerY = map.GetLength(1)-25; //columns
if (movement == 8)
{
playerX--;
map[playerX, playerY] = "P";
Console.WriteLine("The player is located at: " + playerX +","+ playerY);
}
else if (movement == 6)
{
playerY++;
map[playerX, playerY] = "P";
Console.WriteLine("The player is located at: " + playerX + "," + playerY);
}
else if (movement == 2)
{
playerX++;
map[playerX, playerY] = "P";
Console.WriteLine("The player is located at: " + playerX + "," + playerY);
}
else if (movement == 4)
{
playerY--;
map[playerX, playerY] = "P";
Console.WriteLine("The player is located at: " + playerX + "," + playerY);
}
else
{
map[playerX,playerY] = "P";
}
return "";
}
答案 0 :(得分:1)
每次调用player
方法时,位置都是相同的,因为您使用局部变量来存储位置。
我想你想要的是使用全局变量,并在游戏开始时设置位置。
将变量声明移到方法之外:(将它们放在类中但不在方法内部)
int playerX, playerY;
设置地图创建时的位置。
map = // Something
playerX = map.GetLength(0)-3; //rows
playerY = map.GetLength(1)-25; //columns