嗨,我是 xna 的新手,我正在尝试制作一个简单的游戏,其中您的小船可以四处移动并避免小行星从顶部掉到底部。我让船在移动,一颗小行星坠落,但我不知道如何让许多小行星从相同的纹理中坠落,以及如何让它们不时坠落。到目前为止,这是我的小行星类:
namespace Asteroids
{
class Asteroids
{
Texture2D AsteroidTexture;
Vector2 Position;
Random random = new Random();
float AsteroidSpeed = 5;
public void Initialize()
{
Position.Y = 0;
Position.X = random.Next(0, 1000);
}
public void Update()
{
Position.Y += AsteroidSpeed;
if (Position.Y > 600)
{
Position.Y = 0;
Position.X = random.Next(0, 1000);
}
}
public void Load_Content(ContentManager Content)
{
AsteroidTexture = Content.Load<Texture2D>("asteroid");
}
public void Draw(SpriteBatch SpriteBatch)
{
SpriteBatch.Draw(AsteroidTexture, Position, Color.White);
}
}
}
这是我的 Game1 课:
namespace Asteroids
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
KeyboardState keyboardState;
Ship ship;
Asteroids asteroids;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
ship = new Ship();
asteroids = new Asteroids();
asteroids.Initialize();
this.graphics.PreferredBackBufferWidth = 1000;
this.graphics.PreferredBackBufferHeight = 600;
//this.graphics.IsFullScreen = true;
this.graphics.ApplyChanges();
base.Initialize();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
ship.Load_Content(this.Content);
asteroids.Load_Content(this.Content);
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
keyboardState = Keyboard.GetState();
if (keyboardState.IsKeyDown(Keys.Escape))
this.Exit();
ship.Update();
asteroids.Update();
base.Update(gameTime);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
spriteBatch.Begin();
asteroids.Draw(this.spriteBatch);
ship.Draw(this.spriteBatch);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
提前致谢!