简体   繁体   English

如何将坐标[x,y]从2D数组向右移动? C#

[英]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.

相关问题 如何在C#中读取Win32 WM_MOVE lParam x,y坐标? - How do I read the Win32 WM_MOVE lParam x,y coordinates in C#? 将轴坐标保存在2D vector3数组中,然后将结果打印到控制台C#Unity - Saving a x y coordinates in a 2d vector3 array then printing the results to the console C# Unity 如何在 C# 中将四个 2D 网格点与平滑曲线连接起来,并在某个时间返回 x 和 y 位置? - How do I connect four 2D grid points with a smooth curve in C# and give the x and y location back at a certain time? 如何从Texture2D XNA c#获取颜色和坐标(x,y)? - How to get Color and coordinates(x,y) from Texture2D XNA c#? 如何在C#中声明Enum的2D数组? - How do I declare a 2D array of Enum in C#? 如何将C int代码中的2D int数组返回给C#? - How do I return a 2D int array from C code to C#? 如何根据条件将队列中的2D数组放入另一个2D数组中? C# - How do I put 2D arrays from a queue into another 2D array based on a condition? C# 如何使用 C# 从表中的二维数组输出值 - How do I output values from a 2D array in a table using C# 如何将2D数组拆分为较小的2D数组(块)的列表? - c# How do i split a 2D array into a list of smaller 2D arrays (chunks)? C#如何计算XYZ坐标来移动一组对象? - C# How to calculate X Y Z coordinates to move a group of objects?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM