Reputation: 1
I'm making an asteroids type game but I'm struggling to randomize the movement of them.
This makes it go from top to bottom but i want them so asteroids move in all directions, how could i do this?
public void Move()
{
for (int i = 0; i < asteroidList.Count; i++)
asteroidList[i] = new Vector2(asteroidList[i].X, asteroidList[i].Y + 2);
}
Upvotes: 0
Views: 278
Reputation: 8503
Each Asteriod
could have a CurrentPosition
property (probably a Point
(not sure if you need integer or float X and Y co-ordinates) and a CurrentVelocity
property (a 2 dimensional (probably float
)) vector whose X component represents the velocity in the X axis and Y component represents the velocity in the Y axis).
When you create your asteroids you assign the CurrentPosition, either from the position of the parent asteroid that was just destroyed and is breaking into pieces or when you randomly initialize the new asteroids for the next "level".
During Move()
you increment the CurrentPosition
by CurrentVelocity
.
If you assign each Asteroid
a property defining the inherent Physics of the motion you could support interesting things like spinning asteroids, accelerating or decelerating asteroids, etc.
Upvotes: 1
Reputation: 942518
You ought to add another property to the Asteroid class. Say MotionVector. Initialize its X and Y members from Random.Next(). Now you just need to add these to the X and Y properties inside this loop.
Upvotes: 1