2

我想要做的是运行一个控制台应用程序,让用户输入一些细节,然后将用户输入传递给我的游戏。

我希望用户指定游戏的分辨率,以放入 PreferredBackBuffer。

这可能吗?我知道我可以编写一个设置文件并从中读取,但我宁愿把那部分删掉并直接执行。

这是我的“游戏”。我省略了 Player.cs,因为我认为为了我的问题不必展示。

主文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text.RegularExpressions;
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 SimplePlayer
{
    public class Main : Microsoft.Xna.Framework.Game
    {
    const string fileName = "AppSettings.dat";

    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    Texture2D spaceTexture;
    Player playerShip;

    public Main()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";

        ReadRes();
    }

    protected override void Initialize()
    {

        base.Initialize();
    }

    protected override void LoadContent()
    {
        // Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = new SpriteBatch(GraphicsDevice);
        spaceTexture = Content.Load<Texture2D>("spaceBackground");
        Texture2D playerTexture = Content.Load<Texture2D>("shipSprite");
        playerShip = new Player(GraphicsDevice, new Vector2(400, 300), playerTexture);

    }

    protected override void UnloadContent()
    {
    }

    protected override void Update(GameTime gameTime)
    {
        // Allows the game to exit
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
           this.Exit();

        playerShip.Update(gameTime);

        UpdateInput();

        base.Update(gameTime);
    }

    private void UpdateInput()
    {
        KeyboardState keyState = Keyboard.GetState();
        GamePadState gamePadState = GamePad.GetState(PlayerIndex.One);

        if (keyState.IsKeyDown(Keys.Up) || gamePadState.DPad.Up == ButtonState.Pressed)
        {
            playerShip.Accelerate();
        }

        if (keyState.IsKeyDown(Keys.Down) || gamePadState.DPad.Down == ButtonState.Pressed)
        {
            playerShip.MoveReverse();
        }

        if (keyState.IsKeyDown(Keys.Left) || gamePadState.DPad.Left == ButtonState.Pressed)
        {
            playerShip.StrafeLeft();
        }

        if (keyState.IsKeyDown(Keys.Right) || gamePadState.DPad.Right == ButtonState.Pressed)
        {
            playerShip.StrafeRight();
        }
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        spriteBatch.Begin();
        spriteBatch.Draw(spaceTexture, Vector2.Zero, Color.White);
        playerShip.Draw(spriteBatch);
        spriteBatch.End();

        base.Draw(gameTime);
    }

        public void SetWindowSize(int x, int y)
        {
        graphics.PreferredBackBufferWidth = x;
        graphics.PreferredBackBufferHeight = y;
        graphics.ApplyChanges();
        }
    }
}

程序.cs

using System;

namespace SimplePlayer
{
#if WINDOWS
    static class Program
    {
        static void Main(string[] args)
        {
            using (Main game = new Main())
            {
                game.Run();
            }
        }
    }
#endif
}
4

2 回答 2

2

您的游戏可执行文件实际上可能只是控制台应用程序的可执行文件。使用完成后输入他的首选项,您的控制台应用程序可以执行您的游戏,将首选项作为命令行参数传递。

于 2012-12-07T20:53:34.453 回答
0

您可以执行以下操作以在代码中启动可执行文件:

const string ex1 = "C:\\";
const string ex2 = "C:\\SomeDirectory";

ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.FileName = "YourGame.exe";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = "-f j -o \"" + ex1 + "\" -z 1.0 -s y " + ex2;

try
{
    // Start the process
    using (Process exeProcess = Process.Start(startInfo))
    {
    exeProcess.WaitForExit();
    }
}
catch
{
    // Log error.
}
于 2012-12-07T20:58:39.040 回答