【发布时间】:2018-04-28 13:51:02
【问题描述】:
编辑:修复了问题。我为每颗小行星创建了一个新的 Random,因此它们之间没有足够的时间。但是当我手动处理代码时,已经有足够的时间不创建相同的值了。
这是代码。用 WASD 控制,用空间射击,然后按“L”生成小行星。
https://github.com/Geblin/Monogame-Asteroids
小行星在被击中时会分裂成 2-3 颗较小的小行星,而那些较小的小行星具有相同的值,尽管我给它们的是随机值。我已经浏览了代码,似乎它们最初得到了不同的值。
这是小行星类。所以我输入了一个比例和一个位置,这样我就可以在不同的位置产生更小的小行星。这里的角度和速度是随机选择的。
public Asteroid(Texture2D texture, float scale, Vector2 pos)
{
rand = new Random();
this.texture = texture;
this.scale = scale;
this.pos = pos;
angle = degreesToRadian(rand.Next(0,360));
direction = new Vector2((float)Math.Cos(angle), (float)Math.Sin(angle));
origin = new Vector2(texture.Width / 2, texture.Height / 2);
isVisible = true;
sourceRectangle = new Rectangle(0, 0, texture.Width, texture.Height);
boundingCircle = new BoundingCircle(pos, texture, scale);
speed = (float)rand.NextDouble() * (3 - 1) + 1;
setupRotation();
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(texture, pos, sourceRectangle, Color.White, angle, origin, scale, SpriteEffects.None, 1f);
}
public void Update(GameTime gameTime)
{
boundingCircle.x = pos.X;
boundingCircle.y = pos.Y;
pos += direction * speed;
//Sets the asteroids bounds
if (pos.X > 2120)
pos.X = -200;
else if (pos.X < -200)
pos.X = 2120;
if (pos.Y > 1280)
pos.Y = -200;
else if (pos.Y < -200)
pos.Y = 1280;
angle += (float)rotationDirection;
}
public float degreesToRadian(int degrees)
{
float radian = (float)(Math.PI / 180) * degrees;
return radian;
}
//Sets a random rotation speed and rotation direction.
public void setupRotation()
{
rotationDirection = rand.NextDouble() * (0.01f - 0.005f) + 0.005f;
int x = rand.Next(2);
if (x == 1)
rotationDirection = -rotationDirection;
}
这是小行星生成器类。 SetRandomSpawn 方法适用于第一个产卵小行星。那些在窗外产生并飘入的。在主类中,我有代码检查激光和小行星之间的碰撞,然后在同一位置产生较小的。
public AsteroidSpawner(List<Asteroid> asteroidList)
{
rand = new Random();
this.asteroidList = asteroidList;
}
public void LoadContent(ContentManager content)
{
texture = content.Load<Texture2D>("Asteroid");
}
public void Draw(SpriteBatch spriteBatch)
{
foreach (Asteroid asteroid in asteroidList)
asteroid.Draw(spriteBatch);
}
public void Update(GameTime gameTime)
{
UpdateAsteroids(gameTime);
if (Keyboard.GetState().IsKeyDown(Keys.L))
SpawnAsteroid(SetRandomSpawn(), 1f, 1);
}
public void SpawnAsteroid(Vector2 pos, float scale, int amount)
{
for (int i = 1; i <= amount; i++)
{
Asteroid newAsteroid = new Asteroid(texture, scale, pos);
asteroidList.Add(newAsteroid);
}
}
public void UpdateAsteroids(GameTime gameTime)
{
foreach (Asteroid asteroid in asteroidList)
{
asteroid.Update(gameTime);
}
for (int i = 0; i < asteroidList.Count; i++)
{
if (!asteroidList[i].isVisible)
{
asteroidList.RemoveAt(i);
i--;
}
}
}
//Sets a random spawn outside of screen bounds
public Vector2 SetRandomSpawn()
{
int side = rand.Next(4);
//Each number represents a side
switch (side)
{
// Left
case 0:
return new Vector2(2120, rand.Next(0, 1080));
// Top
case 1:
return new Vector2(rand.Next(0, 1920), 1280);
// Right
case 2:
return new Vector2(-200, rand.Next(0, 1080));
//Bottom
case 3:
return new Vector2(rand.Next(0, 1920), -200);
default:
throw new ArgumentException("Incorrect CrystalTypeEnum");
}
}
【问题讨论】:
-
请提供minimal reproducible example而不是repo链接