[英]How do I move coordinates [x,y] from a 2D array to the right? c#
Hello so recently I have been given an assignment to create a small game, but see I have ran into a problem. 您好,最近我被分配了一个小游戏,但是看到我遇到了一个问题。 When I try to move my Player who starts out at a set position he will shift to the right once but it seems he is still locked at the starting spot and upon entering the number to shift him right again, the same answer(coordinates I have printed out) are the same and game board is also the same. 当我尝试移动开始于设定位置的播放器时,他将向右移动一次,但似乎他仍被锁定在起始点,并且在输入数字后再次将其向右移动时,相同的答案(我拥有的坐标(打印出来)相同,游戏板也相同。 https://gyazo.com/9f23419a3300232d3f3d9e1168202cf3 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 "";
}
Every time you call the player
method, the position is the same because you are using local variables to store the position. 每次调用player
方法时,位置都是相同的,因为您使用的是局部变量来存储位置。
What I assume you want is to use a global variable, and set the position when the game starts. 我假设您要使用的是全局变量,并在游戏开始时设置位置。
Move the variable declarations outside of your method: (Put these inside the class but not inside the method) 将变量声明移到方法之外:(将它们放在类中,而不是方法中)
int playerX, playerY;
Set the position when the map is created. 设置地图创建时的位置。
map = // Something
playerX = map.GetLength(0)-3; //rows
playerY = map.GetLength(1)-25; //columns
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.