Jump to content

i think i am missing a basic concept

- - - - -

  • Please log in to reply
9 replies to this topic

#1
justanothernoob

justanothernoob

    Newbie

  • Members
  • PipPip
  • 22 posts
Hey, I've been trying to learn to program for about 2 months now. Started with VB, now switched to C# (like it much better) and I'm beginning to think I am missing something I should know.

Say in a game, i had 10 enemy planes that you had to shoot. Each plane is the same, but they are different object. They all have health, they all lose health when hit by a laser. Here's what I would do:
(ep stands for enemy plane)

bool ep1active;

bool laserhitep1;

int ep1health;

int ep1speed;


bool ep2active;

bool laserhitep2;

int ep2health;

int ep2speed;


bool ep3active;

bool laserhitep3;

int ep3acive;

int ep3active; 

etc.. all the way to 10.

When I add in something else, such as when a laser hits a plane, I do this.

if (laserhitep1 == true)

ep1health = ep1health - 1

//copy and paste the above line 

//and change the digits to 2

if (laserhitep2 == true)

ep2health = ep2health - 1



It's pretty time consuming having to copy and paste, and go through and change digits so much. I have a feeling there's a much easier way.

Can anyone tell me what I should look into or point me in the right direction. I hope my question is clear enough, if it's not, just ask. Thanks.

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
You're missing arrays and classes. You'll get there as you learn more about programming (it usually comes after 2 months).
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Matt Ellen

Matt Ellen

    Newbie

  • Members
  • PipPip
  • 14 posts
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.

#4
emmachallis

emmachallis

    Newbie

  • Members
  • Pip
  • 3 posts
Download the Yellow Book 2010 Edition from this website: Rob Miles - C# Yellow Book

It's a free book that should help you learn C# programming :)

#5
Matt Ellen

Matt Ellen

    Newbie

  • Members
  • PipPip
  • 14 posts
Also once you have the basics, read Eric Lippert's blog as it will teach you EVERYTHING about C#. Seriously. Also a lot about programming in general.

#6
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200

Matt Ellen said:

Also once you have the basics, read Eric Lippert's blog as it will teach you EVERYTHING about C#. Seriously. Also a lot about programming in general.
Good posts Matt. :)
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#7
Matt Ellen

Matt Ellen

    Newbie

  • Members
  • PipPip
  • 14 posts
Yeah, Eric Lippert really knows his stuff.

It's not really for beginners though.

#8
AIGuy

AIGuy

    Learning Programmer

  • Members
  • PipPipPip
  • 64 posts
This reminds of when I used to program in QB. I wasted several hours on the is sort of thing, but on the other hand, I appreciated OOP much more now than I ever would have had I skipped that chapter in my life. :) Any way, don't worry justanothernoob. You seem to have the OOP extreme basics in your code there. A great start. All you're missing is the language knowledge to implement it correctly. Which you pretty much have now from Matt.

OOP is the best way to program (for most projects) and C# is (I think) the best language to do it in. I would suggest reading up on the basics of Object-Oriented Programming and then learning how the code works in C#. I personally did this by reading the "Head First C#" book. It goes over the basics of OOP and C# and have you do little programing assignments along the way. I liked it, but it was the only beginners book I read, so I can't call it the best.

Any way, welcome to programming, welcome to OO programming, welcome to C# and welcome to the forums. :)

#9
sam_coder

sam_coder

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 372 posts

AIGuy said:

OOP is the best way to program (for most projects) and C# is (I think) the best language to do it in.

thats a bold statement! :c-laugh:

C# IS a great language though!

#10
justanothernoob

justanothernoob

    Newbie

  • Members
  • PipPip
  • 22 posts
Thanks everybody for the help, I think I've figured it out for the most part.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users