【发布时间】:2021-04-26 16:12:52
【问题描述】:
所以我在 Windows 控制台中制作了一个小游戏(只是为了好玩和测试理论),但我遇到了 DrawGame() 方法的问题。
我正在创建一个新的字符串数组 (string[] _CompleteMap),其值是从 CurrentMap.MapData(也是一个字符串数组)分配的:
string[] _CompleteMap = CurrentMap.MapData;
我使用CurrentMap.MapData 作为空白地图,然后更改字符串中的字符以表示玩家和生物。
但是当我对_CompleteMap 进行更改时,它似乎也在更改CurrentMap.MapData 中的值...
我不知道为什么会这样,它会留下用户走过的痕迹。
任何支持都会有所帮助,如果需要,我可以发布代码。
using System;
using System.Text;
using System.Threading;
using ConsoleAdventure.Engine.BaseClasses;
using Database = ConsoleAdventure.Engine.Database;
namespace ConsoleAdventure
{
class Program
{
private static bool whileGameScene = true;
private static Engine.BaseClasses.MapBase CurrentMap;
static unsafe void Main(string[] args)
{
// Load and populate the databases.
Database.ItemDB.LoadItemDB();
Database.MapDB.LoadMapDB();
// Set demo map
CurrentMap = Database.MapDB.Maps[0];
// Hide cursor
Console.CursorVisible = false;
// Start a new thread to capture the user inputs
new Thread(() =>
{
while (true)
{
Thread.CurrentThread.IsBackground = true;
ConsoleKeyInfo cki = Console.ReadKey();
if (cki.Key == ConsoleKey.A && Player.Data.Location.X > 0)
Player.Data.Location.X--;
else if (cki.Key == ConsoleKey.D)
Player.Data.Location.X++;
else if (cki.Key == ConsoleKey.W && Player.Data.Location.Y > 0)
Player.Data.Location.Y--;
else if (cki.Key == ConsoleKey.S)
Player.Data.Location.Y++;
CheckWarpPoints();
}
}).Start();
// Draw the game
while (whileGameScene)
{
DrawGame();
}
}
/// <summary>
/// Check if player has stood on a warp zone
/// </summary>
private static void CheckWarpPoints()
{
foreach (MapBase.WarpPoint warp in Database.MapDB.Maps[Player.Data.CurrentMap].WarpPoints)
{
if (Player.Data.Location.X == warp.From.X && Player.Data.Location.Y == warp.From.Y)
{
Player.Data.CurrentMap = warp.ToMapIndex;
Player.Data.Location = warp.To;
}
}
}
public static void DrawGame()
{
string[] _CompleteMap = CurrentMap.MapData;
StringBuilder _sbRow = new StringBuilder();
foreach (var _object in CurrentMap.MapObjects)
{
_sbRow = new StringBuilder(_CompleteMap[_object.CurrentLocation.Y]);
_sbRow[_object.CurrentLocation.X] = _object.Icon;
_CompleteMap[_object.CurrentLocation.Y] = _sbRow.ToString();
}
_sbRow = new StringBuilder(_CompleteMap[Player.Data.Location.Y]);
_sbRow[Player.Data.Location.X] = 'X';
_CompleteMap[Player.Data.Location.Y] = _sbRow.ToString();
for (int i = 0; i < _CompleteMap.Length; i++)
{
Console.SetCursorPosition(0, i);
Console.WriteLine(_CompleteMap[i]);
}
}
}
}
【问题讨论】: