rowlandsfc
Active member
Hi so ive created a game for college using windows forms app where enemies drop from the top of the form, the enemies are a class and using a list 5 are created as the form loads. what i would like to do is ensure that when the 5 are created that they do not appear in the exact same location. i have created a list to store the locations of each one as they are created, what i cant figure out is how do i check these locations find any duplicates delete them then replace them with a new one. any help or simply pointing me in right direction would be appreciated
enemy class:
class EnemyShips
{
// variables for movign the enemy ships
public PictureBox e;
public int xPos, yPos;
public int speed;
public Boolean isDisposed = false;
Random random = new Random();
public static List<Point> position = new List<Point>();
public EnemyShips(Panel f)
{
e = new PictureBox(); // creates a new insatnce of a picturebox
// set the appearance of e
e.Width = 42;
e.Height = 24;
e.Image = Minefield.Properties.Resources.enemy;
e.BackColor = Color.Transparent;
e.SizeMode = PictureBoxSizeMode.AutoSize;
e.Visible = false;
for (int i = 0; i < 10; i++)
{
int stepWidth = 42; //
int maxStep = 400 / stepWidth; // here calculate the maximum number of step that can be made
int stepAmountX = random.Next(1, maxStep);
xPos = stepAmountX * stepWidth;
speed = random.Next(2, 5);
e.Location = new Point(xPos, yPos);
f.Controls.Add(e); // needed to add the control to the panel
}
position.Add(e.Location);
}
public void moveEnemy(Panel f)
{
e.Visible = true; // make the enemy visible
yPos = yPos + speed; // moves the enemy
e.Location = new Point(xPos, yPos);
}
}
}
adding enemies:
private void enemyTimer_Tick(object sender, EventArgs e)
{
if(enemyNumber > 4)
{
arrayTimer.Enabled = true; // we enable the other timer which controls the enemy movement
enemyTimer.Enabled = false; // after 5 enemies we stop timer so we dont get more
}
else
{
enemies.Add(new EnemyShips(this.panel1)); // create a new isntance of enemy using the array
enemyNumber++;
}
if (enemiesKilled > 19)
{
bossTimer.Enabled = true;
}
}