我刚开始一个XNA项目,我想在Monogame中用Atari制作Asteroids。目前,我有船的运动。我刚刚修改了代码中的一些内容。现在的问题是小行星。我正在尝试研究如何制作它们,但我没有任何好的结果。
我的代码:
private Texture2D texture;
private Rectangle destinationRectangle;
private Rectangle sourceRectangle;
private float rotationSpeed;
private Vector2 rotationOrigin;
private float rotationAngle;
private float speed;
private Vector2 velocity;
public Asteroide(Texture2D texture, int x, int y)
{
}
public Asteroide(Texture2D texture, int x, int y, int width, int height, float speed, float rotationSpeed)
{
this.texture = texture;
this.rotationSpeed = rotationSpeed;
destinationRectangle = new Rectangle(x - width / 2, y - height / 2, width, height);
sourceRectangle = new Rectangle(0, 0, width, height);
rotationOrigin = new Vector2(texture.Width * 0.5f, texture.Height * 0.5f);
this.speed = speed;
Random rnd = new Random();
int degree = rnd.Next(-45, 46);
double radian = degree * Math.PI / 180;
ChangeAngle(radian);
}
public void ChangeAngle(double radian)
{
velocity.X = (float)Math.Cos(radian) * speed;
velocity.Y = -1f * (float)Math.Sin(radian) * speed;
}
public void Update(GameTime gameTime)
{
Move(gameTime);
}
private void Move(GameTime gameTime)
{
destinationRectangle.X += (int)(velocity.X * gameTime.ElapsedGameTime.TotalSeconds);
destinationRectangle.Y += (int)(velocity.Y * gameTime.ElapsedGameTime.TotalSeconds);
}
private void Rotation(GameTime gameTime)
{
rotationAngle += rotationSpeed * (float) gameTime.ElapsedGameTime.TotalSeconds;
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(texture, destinationRectangle, sourceRectangle, Color.White, rotationAngle, rotationOrigin, SpriteEffects.None, 0f);
}
我想要的是一颗不断移动并产生各种尺寸小行星的小行星。它们应覆盖大,中,小尺寸的所有屏幕。我知道我必须在某处使用Random,但我不知道在哪里。