c# - 如何在运行游戏之前运行控制台应用程序?

标签 c# console

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

我希望用户指定游戏的分辨率,并将其放入 PreferredBackBuffer 中。

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

这是我的“游戏”。我省略了 Player.cs,因为我认为为了我的问题没有必要显示它。

Main.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
}

最佳答案

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

关于c# - 如何在运行游戏之前运行控制台应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13770791/

相关文章:

linux - 如何在 Ubuntu 或 Debian 上设置物理硬件终端?

c - 如何不让终端显示任何内容?

C# Console.Readkey - 等待特定输入

c# - 如何连接控制台应用程序和 Windows 窗体应用程序 C#

c# - 能否从 ActionFilter 中获取方法中使用的参数值?

c# - 为什么刷新列表后列表中的选定项目会移动到不同的屏幕位置

c# - MVVM - 静态 View 模型

c# - 不了解 DbContext——每个应用程序或每个模型

c# - 如何在不降低应用程序速度的情况下将冗长的控制台重定向到 C# 中的 TextBox

javascript - Chrome 控制台访问 DOM