我想要做的是运行一个控制台应用程序,让用户输入一些细节,然后将用户输入传递给我的游戏。
我希望用户指定游戏的分辨率,以放入 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
}