首页 > 解决方案 > 主机游戏角色

问题描述

自从开始开发我的游戏以来,我学到了很多东西。现在我正处于需要实现适当的玩家角色的阶段。我一直在尝试这样做一段时间,但我无法让它与我的运动系统一起工作。这是我的运动系统(有碰撞):

ConsoleKeyInfo input = Console.ReadKey(true);
                    switch (input.KeyChar)
                    {
                        case 'w':
                            if (y >= 1 && data[y - 1][x] != '#')
                            {
                                oldx = Console.CursorLeft;
                                oldy = Console.CursorTop;
                                Console.SetCursorPosition(x + 0, y - 1);
                                x = Console.CursorLeft;
                                y = Console.CursorTop;
                                //if (x == 11 && y == 11 )            
                            }
                            break;

                        case 'a':
                            if (x >= 1 && data[y][x - 1] != '#')
                            {
                                oldx = Console.CursorLeft;
                                oldy = Console.CursorTop;
                                Console.SetCursorPosition(x - 1, y + 0);
                                x = Console.CursorLeft;
                                y = Console.CursorTop;
                            }
                            break;

                        case 's':
                            if (y < Console.WindowHeight - 1 && data[y + 1][x] != '#')
                            {
                                oldx = Console.CursorLeft;
                                oldy = Console.CursorTop;
                                Console.SetCursorPosition(x + 0, y + 1);
                                x = Console.CursorLeft;
                                y = Console.CursorTop;
                            }
                            break;

                        case 'd':
                            if (x < Console.WindowWidth - 1 && data[y][x + 1] != '#')
                            {
                                oldx = Console.CursorLeft;
                                oldy = Console.CursorTop;
                                Console.SetCursorPosition(x + 1, y + 0);
                                x = Console.CursorLeft;
                                y = Console.CursorTop;
                            }
                            break;
                    }

我的地图是一个 ascii 地图,以 ConsoleColor 必须提供的颜色显示。在玩家角色后面,应该重绘相应的地图图块,而不是重绘整个地图(必须这样做在游戏过程中真的很烦人)

当您退出地图时,将绘制一张新地图,并将玩家传送到该地图的入口。

我将如何将角色的绘制实现为子程序?

先感谢您!

标签: c#asciiconsole-application

解决方案


试试这些

protected static void WriteAt(string s, int x, int y)
{
    try
    {
       Console.SetCursorPosition(origCol+x, origRow+y);
       Console.Write(s);
    }
    catch (ArgumentOutOfRangeException e)
    {
       Console.Clear();
       Console.WriteLine(e.Message);
    }
}

Console.CursorTop 属性

获取或设置光标在缓冲区内的行位置。

Console.CursorLeft 属性

获取或设置光标在缓冲区内的列位置。

Console.SetCursorPosition(Int32, Int32) 方法

设置光标的位置。


推荐阅读