在编写C#控制台代码后在XNA中可视化游戏

时间:2013-11-25 14:49:02

标签: c# visual-studio xna console

这是一个简单的问题,但我无法在其他任何地方找到答案。

我是编程新手,我在C#中编写了一个完整的桌面游戏。我想用XNA代表它。我希望游戏打开简单的控制台应用程序(我已经拥有)和游戏可视化。

我的想法是让玩家在向XNA窗口发送数据之前轮到他,并让它读取数据并相应地更新它的图像。除了简单地接收数据并更新它的图形之外,XNA窗口不需要做任何其他事情。我只是想知道这是否可行,是否有更简单的方法来执行此操作以及是否应该使用Visual Studio Windows窗体应用程序而不是XNA。

提前致谢。

2 个答案:

答案 0 :(得分:0)

这可能适用于您想要做的事情:

首先创建一个新的XNA项目。

然后执行以下操作:

在Microsoft Visual C#中,右键单击解决方案资源管理器中的项目。然后单击“Properties”并在“Application”选项卡中选择“Console Application”作为Output-Type。

我假设您了解XNA的基础知识。

现在,在更新循环中,您可以使用Console.ReadLine()从控制台获取用户输入,然后更新您需要的任何内容。

不幸的是,这不足以满足您的需求,您将不得不做出一些妥协。您应该添加您对XNA项目所做的棋盘游戏类,并稍微调整它们以便在XNA中工作。您将能够从用户那里获得输入,但是您无法直接从控制台向XNA提供输出。 Console.WriteLine()也可用,因此您可以按常规输出文本。

一旦你走得那么远,你应该根据你的代码从控制台需要的内容开始看问题所在。一旦您知道它们可能是什么,请随时再次发布这些问题,我将帮助您提出解决方案。

也就是说,这样做的唯一真正好处就是Console.ReadLine()来获取输入。除此之外,代码与常规XNA项目完全相同。

答案 1 :(得分:0)

所以我创建了一个类,你可以输入字母大写和小写做空格和退格,看起来像这样。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace TileGame
{
    class KeyboardEntering
    {
        private KeyboardState ksCurr;
        private KeyboardState ksPrev;

        public KeyboardEntering()
        {

        }

        public string Update(string enterString)
        {
            string exitString = enterString;
            ksCurr = Keyboard.GetState();
            if (!IsPressed(Keys.LeftShift) && !IsPressed(Keys.RightShift))
            {
                if (OnRelease(Keys.A)) exitString += "a";
                if (OnRelease(Keys.B)) exitString += "b";
                if (OnRelease(Keys.C)) exitString += "c";
                if (OnRelease(Keys.D)) exitString += "d";
                if (OnRelease(Keys.E)) exitString += "e";
                if (OnRelease(Keys.F)) exitString += "f";
                if (OnRelease(Keys.G)) exitString += "g";
                if (OnRelease(Keys.H)) exitString += "h";
                if (OnRelease(Keys.I)) exitString += "i";
                if (OnRelease(Keys.J)) exitString += "j";
                if (OnRelease(Keys.K)) exitString += "k";
                if (OnRelease(Keys.L)) exitString += "l";
                if (OnRelease(Keys.M)) exitString += "m";
                if (OnRelease(Keys.N)) exitString += "n";
                if (OnRelease(Keys.O)) exitString += "o";
                if (OnRelease(Keys.P)) exitString += "p";
                if (OnRelease(Keys.Q)) exitString += "q";
                if (OnRelease(Keys.R)) exitString += "r";
                if (OnRelease(Keys.S)) exitString += "s";
                if (OnRelease(Keys.T)) exitString += "t";
                if (OnRelease(Keys.U)) exitString += "u";
                if (OnRelease(Keys.V)) exitString += "v";
                if (OnRelease(Keys.W)) exitString += "w";
                if (OnRelease(Keys.X)) exitString += "x";
                if (OnRelease(Keys.Y)) exitString += "y";
                if (OnRelease(Keys.Z)) exitString += "z";
            }

            if (IsPressed(Keys.LeftShift) || IsPressed(Keys.RightShift))
            {
                if (OnRelease(Keys.A)) exitString += "A";
                if (OnRelease(Keys.B)) exitString += "B";
                if (OnRelease(Keys.C)) exitString += "C";
                if (OnRelease(Keys.D)) exitString += "D";
                if (OnRelease(Keys.E)) exitString += "E";
                if (OnRelease(Keys.F)) exitString += "F";
                if (OnRelease(Keys.G)) exitString += "G";
                if (OnRelease(Keys.H)) exitString += "H";
                if (OnRelease(Keys.I)) exitString += "I";
                if (OnRelease(Keys.J)) exitString += "J";
                if (OnRelease(Keys.K)) exitString += "K";
                if (OnRelease(Keys.L)) exitString += "L";
                if (OnRelease(Keys.M)) exitString += "M";
                if (OnRelease(Keys.N)) exitString += "N";
                if (OnRelease(Keys.O)) exitString += "O";
                if (OnRelease(Keys.P)) exitString += "P";
                if (OnRelease(Keys.Q)) exitString += "Q";
                if (OnRelease(Keys.R)) exitString += "R";
                if (OnRelease(Keys.S)) exitString += "S";
                if (OnRelease(Keys.T)) exitString += "T";
                if (OnRelease(Keys.U)) exitString += "U";
                if (OnRelease(Keys.V)) exitString += "V";
                if (OnRelease(Keys.W)) exitString += "W";
                if (OnRelease(Keys.X)) exitString += "X";
                if (OnRelease(Keys.Y)) exitString += "Y";
                if (OnRelease(Keys.Z)) exitString += "Z";
            }
            if (OnRelease(Keys.Space)) exitString += " ";

            if (OnRelease(Keys.Back))
            {
                if(exitString.Length != 0) exitString = exitString.Substring(0, exitString.Length - 1);
            }

            ksPrev = ksCurr;
            return exitString;
        }

        public bool OnRelease(Keys key)
        {
            if (ksCurr == null) return false;
            if (ksPrev == null) return false;
            return ksCurr.IsKeyUp(key) && ksPrev.IsKeyDown(key);
        }
        public bool IsPressed(Keys key)
        {
            if (ksCurr == null) return false;
            return ksCurr.IsKeyDown(key);
        }
    }
}

然后使用它我创建一个KeyboardEntering对象并将其设置为ke,更新看起来像这样

drawStringTest = ke.Update(drawStringTest);

然后我只用DrawString方法绘制字符串。我希望人们能发现这有用。我知道这是一个老帖子,XNA不是以前的样子,但不管哈哈。