Software development gives you a lot of ways to think about solving problems
My favourite is object oriented design. Luckily C# is particularly suited to that.
Lets take your enemy planes.
In C# you can create a class
public class EnemyPlane
{
}
And in this class you can define the behaviours that your plane will make
public class EnemyPlane
{
public void Move()
{
//TODO: add movement code
}
public void Shoot()
{
//TODO: add shooting code
}
public void FindPlayer()
{
//TODO: add in player finding code
}
//There will be more methods than this
}Each method in the above class, and the rest that should be written to make the EnemyPlane class functional, will do something different and will represent a unique behaviour of the plane.
You'll probably want to store some state information about your plane (such as its position, health, etc.). You do that in fields, and then access the data via properties.
public class EnemyPlane
{
private Point2D position = new Point2D(0,0);
private int health = 100;
public Point2D Position
{
get { return position; }
}
public int Health
{
get { return health; }
}
}
You'll notice that I'm using the keywords public and private. These are access modifiers that tell the C# compiler what other parts of your code can access the methods, fields and properties (collectively known as members) of your class.
Private means only things within the class can access the member; the fields
health and
position are examples of this.
Public means that anything can access them.
For example the following code will cause the compiler to throw an error:
EnemyPlane enemy = new EnemyPlane();
Console.WriteLine(enemy.health);
Because the health field is private. The following code would compile fine (given that there is the correct supporting code)
EnemyPlane enemy = new EnemyPlane();
Console.WriteLine(enemy.Health);
This is because Health is public getter property (which gets value stored in the health field) so anything can read from it. however it is readonly, as the property doesn't implement a set accessor. So the following code will fail to compile:
EnemyPlane enemy = new EnemyPlane();
enemy.Health = 400;
The Health property is readonly, so you can't assign to it.
To make a read/write property you need to have both a getter and a setter.
public class Player
{
private string name;
public string Name
{
get {return name;}
set { name = value; }
}
}
So with this code you can set the name of a player like so:
Player player1 = new Player()
player1.Name = "Cedric";
Console.WriteLine(player1.Name);
As you can see, the Name property of the Player class allows for both reading and writing.
As an aside, it is possible to have a write only property (setter only), but I haven't come across a good reason to do this yet.
That's a really light introduction to some of the facets of OOP. You should really check out books and website on the subject. I can recommend
Object Thinking by David West as the best book on the subject that I have read, and the best programming book in general. (I might be in a minority for that last bit.)
Once you have created an EnemyPlane class, you can then create exciting things like collections of EnemyPlanes and do exciting things, like iterate over them. (Well, I find these things exciting :laugh:)
Read up on OOP, and learn how to exploit C# to its fullest in this regard. You will find this will help you to write cleaner code and it will also be easier to maintain.