简体   繁体   中英

Visualizing a game in XNA after having written the C# console code

This is a simple question but i couldn't find the answer anywhere else.

I'm new to programming and I wrote a complete boardgame in C#. I was thinking of representing it with XNA. I would like the game to open the simple console app (which i already have) and a game visualization.

My idea would be to let a player play his turn before sending data to the XNA window and have it read that data and update it's imagery accordingly. The XNA window does not need to do anything else apart from simply receive the data and update it's graphics. I just want to know if this is possible, if there is an easier way to do this and if I should be using Visual Studio Windows form application rather than XNA.

Thanks in advance.

This might work for what you want to do:

Start by creating a new XNA project.

Then do the following:

In Microsoft Visual C# right-click your project in Solution Explorer. Then click on "Properties" and in the "Application" tab select "Console Application" as your Output-Type.

I am assuming you know the basics of XNA.

Now, in the update loop, you can use Console.ReadLine() to get user input from the console, and then update whatever you need to.

Unfortunately, this won't quite suffice for your needs alone, you will have to make some compromises. You should add the Board game classes that you made to the XNA project, and slightly adapt them to work in XNA. You will be able to get input from the user, but you will not be able to give output directly from the console to XNA. Console.WriteLine() is also available, so you will be able to output text as per usual.

Once you get that far, you should start to see where the issues lie based on what your code requires from the console. Feel free to post again on here with those issues, once you know what they might be, and I will help to come up with a solution.

That said, it seems like the only real benefit of doing this would be the Console.ReadLine() to get input. Other than that, the code would be pretty identical to a regular XNA project.

So i made a class that you can enter in letters upper and lower case do spaces and backspace which looks like this.

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);
        }
    }
}

Then to use this I make a KeyboardEntering Object and set it to ke and the update looks like this

drawStringTest = ke.Update(drawStringTest);

then i simply draw the string with the DrawString Method. I hope people can find this helpful. I know this is a old post and XNA isnt what it used to be but whatever haha.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM