Thursday, 18 October 2012

Great programming analogies No.2. Age of Empires. Part 3


This is the third in a series of posts where I plan to describe my favourite analogy. Age of Empires. In this post, I get started with the Age of Empires analogy in order to describe a class in C# and what properties and methods it will require.

You can read the first part of this series here.

You can read the second part of this series here.




So I am going to write a computer game. I think I'll call it Age of Empires.
 
HTML5 player
It involves the player building up armies and conquering their enemies.
So a key ingredient here is soldiers.
The game is going to be full of soldiers.
So I had better define a class for soldier.
When designing a class, we need to consider what characteristics or properties that we need in order to represent a soldier and distinguish it from others.
After a few minutes of brainstorming I might come up with the following properties:
X and Y location. Where is soldier on the map?
Life points. How much damage can the soldier take before dying?
Sprite. The graphic used to represent the soldier.
Speed. How fast can the soldier move?
Faction. What side does the soldier belong to?

Let's not forget behaviours or methods. i.e. What abilities does our soldier have? Let's put that another way, what will I want to be able to do with a soldier?
An aside. Have you noticed my vocabulary? I am talking about a soldier as if it were a real thing. I am talking about characteristics(properties) that a soldier might have and abilities/behaviours (methods) that soldiers can have.
That's the thing with OOP. You begin to visualise them as things. As objects! And treat them as such.
Yes I know it's just data and code, but the moment you begin think of the objects in your application as objects, things, entities etc., you are pretty much there.

So back on track. I was thinking about some methods for my soldier class.
I came up with:
Move(x,y). Soldier moves to a specified location at a rate based upon its Speed property.
Strike(opponent). Soldier will 'hit' another soldier.

Ah but that requires some more properties:
Strength. Amount of 'damage' done per strike. i.e. how many life points removed from opponent.
Toughness. How many points of damage can be taken when struck by another soldier before life points are removed. e.g. soldier with Strength of 4 strikes a soldier with a toughness of 3 and so inflicts 1 damage point. Reducing opponent's life points by 1 point.
In terms of game play, a soldier would be instructed to "move over here" or "strike this guy".
So I probably need one more method:
Attack(opponent).  Approach nominated soldier and begin striking it.

I guess that's it for now.
So the first draft of the Soldier class would come out as:

UML class diagram.

So how might that look in code?
I have written the class up in C#.
Let's start with the properties. As you can see, I have declare public properties for each of those that I identified in my UML diagram above. I haven't shown all of the private fields that actually store the data.






Here is an example of one of the properties and its associated private fields:



 
I then added the three methods to the class: Attack, Move and Strike.



 
Here is the Strike method that I wrote:




It is passed another soldier instance as a target and is assumed to be "in contact" with it. i.e. stood next to it. The code determines the difference between the Strength of the attacker and the Toughness of the opponent. Should there be an advantage, the difference is removed from the opponent's LifePoints property.
Rather than spend time concentrating on producing compelling graphics, my code simply outputs messages to the Output window so that I (and you) can follow what is going on.

Here is the Move method.




Note that it simply "transports" the soldier to the desired destination. In the actual method, I would determine the most direct path between the Soldier's current location and the required destination. The Move method would then advance (move) the soldier along the path at a rate determined by its Speed property.
During the game, each Soldier will be prompted to "do something" repeatedly. i.e. as long as the game is running, all of the pieces should be doing something. Either advancing to an opponent, striking an opponent or standing still.
Imagine a control loop that continually prompts each Soldier to "do something". Well, if a Soldier was moving toward a distant location, each instruction to "do something" might cause it's Move method to move a little further along it's predetermined path.
Although the Strike method causes the damage to adjacent soldiers, this doesn't reflect what a player might call upon it to do. They are likely to select a Soldier and then right-click on an opponent's soldier. In this case, the Soldier must Move into contact with the opponent and then strike it.
Hence the third method, Attack.



This method, tells the Soldier to Move into contact with the opponent and then Strike.
Note that this method is calling the Move and Strike methods for the current object (Soldier).

Testing. I then wrote a simple WPF program that instantiated two Soldier objects and then instructed one of them to Move to a location and then Attack another Soldier at a second location.




It's early days but I fell that I have made a good start.

I have been able to create a class to represent a Soldier and equip it with the properties and methods that it needs to act and behave as a Soldier.



Flash player


In the next post, I will take my first look at inheritance and explore how the Age of Empires analogy can help explain the purpose and syntax of inheritance.

Training?

If you are interested in UML or C# (we can do all of this in VB.NET too and once I finish the series, I intend to double back and reproduce everything in VB.NET) training, we offer the following courses:







See you soon

Phil Stirpé
"I don't do average!"

4 comments:

  1. As a new C# Developer and a huge fan of the AoE Series I just want to say that these videos look like they are heading in just the right direction for me. I eagerly await your next video in this series, as I currently lack any sort of OOP foundation.

    Thank You.

    ReplyDelete
    Replies
    1. đồng tâm
      game mu
      cho thuê nhà trọ
      cho thuê phòng trọ
      nhac san cuc manh
      số điện thoại tư vấn pháp luật miễn phí
      văn phòng luật
      tổng đài tư vấn pháp luật
      dịch vụ thành lập công ty
      http://we-cooking.com/
      chém gió
      trung tâm tiếng anh

      Từng đám từng đám xương trắng lớn , trong tiếng ma sát cót két , từ mặt đất màu đen đứng thẳng lên , dưới ánh trăng của mặt trăng tím chiếu xuống , sắc trắng bệch của những bộ xương này hiện lên rất rõ ràng . Cùng lúc đó Diệp Âm Trúc cũng phát giác ra một bí mật , cũng là vì ánh trăng rằm chiếu xuống đủ sáng hắn mới có thể phóng tầm mắt nhìn ra xa , mới thấy ở đó thứ gì như một tấm gương phản xạ lại ánh mắt .

      Long vực này thật ra cũng không quá lớn như người ta tưởng tượng , nó sở dĩ có vẻ rộng lớn khôn cùng là do bên kia có một mặt gương cực lớn phản xạ toàn bộ cảnh vật lại . Bất quá Diệp Âm Trúc lúc này không có đủ thời gian chú ý đến , bởi vì đám Long cốt cực lớn đã hoàn toàn đứng lên . Từ trên người bọn chúng , mặc dù không hề cảm thụ được khí tức sinh mạng nhưng lại hoàn toàn bao phủ ma pháp nguyên tố khổng lồ cùng với linh hồn lực . Cốt Long , Diệp Âm Trúc chưa bao giờ thấy hoặc đọc qua sách vở nào nói về loại sinh vật như thế này .

      Cho dù dưới tấm kinh lớn phản xạ bên kia sinh ra thêm hằng hà sa số ảo ảnh nhưng dưới mắt đếm sơ bộ

      Delete
  2. Thanks Daniel

    I have had great success using this analogy in the classroom over the years and it has stood the test of time.

    The next batch of videos and transcripts have been worked out but there is still some post production to be done.

    Am finishing off a dissertation for my Masters this month then hope to be back up to full speed by January 2013.

    Please bear with me.

    I hope you find them useful.

    Regards


    Phil

    ReplyDelete
  3. I will be sticking around that's for sure, anytime someone relates computer games with programming it just seems to click easier for me.

    Thank you for the quick reply I really appreciate what you're doing here and I look forward to the future videos.

    Daniel

    ReplyDelete