【问题标题】:Moving blip in console在控制台中移动 blip
【发布时间】:2013-06-27 21:42:10
【问题描述】:

我想知道这件事。如何在控制台中将一个符号从一侧移动到另一侧。 我有用于在控制台上移动 blip 的函数 walk()。它使用基本条件系统并将速度值添加到位置。但这就是问题所在: 当我想从 0、0 移动到 60、80 时,它的行为是这样的。 它通常会在到达最小位置 (60) 时以对角线方式移动。然后它只会在 Y 坐标中滑动到 80。 所以: 直到 60 岁; 60 位置它以对角线方式滑动。 当它达到 60 时,它将停留在 x60 并滑动到 y80。 但这是合乎逻辑的。 我想找到一种在起点和终点之间滑动而不挂坐标的方法。 呃,我的英语。这是视频:https://dl.dropboxusercontent.com/u/89067882/problem.avi 视频说明一切。

【问题讨论】:

  • 没关系。它仍然需要调整

标签: c# windows console slide


【解决方案1】:

以下示例演示了如何使 blip 以预定义的速度和更新间隔移动。运行它,看看会发生什么......

*我向 System.Drawing 添加了一个引用,因此我可以使用Point 结构。

与@Arsalan00 一样,我们首先计算从 A 到 B 所需的 X 和 Y 的变化。然后,给定预定义的速度,我们计算在 A 和 B 之间行驶所需的时间。下一步我们使用循环和Stopwatch 类来确定自从我们开始移动 blip 以来已经过去了多少时间,在预定义的更新间隔时间内使用Sleep() 暂停。通过秒表的经过时间,我们可以计算经过的“时间百分比”相对于所需的总行程时间。通过时间百分比,我们可以计算出 X 和 Y 的计算变化应该在多远,并将其添加到起点:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Diagnostics;

namespace ConsoleApplication1
{
    class Program
    {

        public const double MillisecondsBetweenMoves = (double)100;
        public const double DistancePerSecond = (double)10;

        static void Main(string[] args)
        {
            Point ptStart = new Point(0, 0);
            Point ptStop = new Point(70, 15);

            int deltaX = ptStop.X - ptStart.X;
            int deltaY = ptStop.Y - ptStart.Y;
            double DistanceToTravel = Math.Sqrt((deltaX * deltaX) + (deltaY * deltaY));

            Console.Clear();
            Console.SetCursorPosition(ptStart.X, ptStart.Y);
            Console.Write("a");
            Console.SetCursorPosition(ptStop.X, ptStop.Y);
            Console.Write("b");

            double TimeRequiredInMilliseconds = DistanceToTravel / DistancePerSecond * (double)1000;
            Stopwatch SW = new Stopwatch();
            SW.Start();
            while (SW.ElapsedMilliseconds < TimeRequiredInMilliseconds)
            {
                System.Threading.Thread.Sleep((int)MillisecondsBetweenMoves);
                Point position = new Point(
                    ptStart.X + (int)((double)SW.ElapsedMilliseconds / TimeRequiredInMilliseconds * (double)deltaX),
                    ptStart.Y + (int)((double)SW.ElapsedMilliseconds / TimeRequiredInMilliseconds * (double)deltaY)
                    );
                Console.Clear();

                Console.SetCursorPosition(ptStart.X, ptStart.Y);
                Console.Write("a");
                Console.SetCursorPosition(ptStop.X, ptStop.Y);
                Console.Write("b");

                Console.SetCursorPosition(position.X, position.Y);
                Console.Write("X");
            }

            Console.Clear();
            Console.SetCursorPosition(ptStart.X, ptStart.Y);
            Console.Write("a");
            Console.SetCursorPosition(ptStop.X, ptStop.Y);
            Console.Write("b");
            Console.SetCursorPosition(ptStop.X, ptStop.Y);
            Console.Write("X");

            Console.ReadLine();
        }

    }
}

【讨论】:

    【解决方案2】:

    计算水平距离和垂直距离。从它们中得出一个比例,即垂直/水平。 使用该比率来决定您朝某个方向前进的机会。

    例如,如果水平距离为 40,垂直距离为 20,则比率为 0.5。这意味着每 1 次水平移动需要进行 0.5 次垂直移动,或者每 2 次水平移动需要进行 1 次垂直移动。这样做,直到你到达非常接近目的地。之后,使用您的常规 walk() 函数步行接下来的 2,3 块。

    【讨论】:

      猜你喜欢
      • 2013-01-02
      • 1970-01-01
      • 1970-01-01
      • 2017-09-05
      • 1970-01-01
      • 2012-05-03
      • 2016-12-10
      • 2012-07-26
      相关资源
      最近更新 更多