140 likes | 263 Vues
This document explores the implementation of the FlameBullet and Player classes in a gaming context using C#. It highlights the attributes and methods defined in these classes, such as Texture2D, Rectangle, and Vector2, as well as their roles in game mechanics. It emphasizes inheritance, showing how FlameBullet extends GameObject by adding specific properties and methods. Key concepts include overriding inherited methods to customize behavior, the utility of access modifiers, and the benefits of well-structured, maintainable code for game development.
E N D
FlameBullet - Texture2D tex; // Attributes - Rectangle rect; - Vector2 pos, origin; - double rotation; - boolisAlive; - Game1 mainGame; - int age; - Color color; - public void Update(); // Methods - public void Draw(); Player - Texture2D tex; // Attributes - Rectangle rect; - Vector2 pos, origin; - double rotation; - boolisAlive; - Game1 mainGame; - Vector2 bulletVel; - double xPart, yPart; - public void Update(); // Methods - public void Draw();
Find all the common attributes and methods… FlameBullet - Texture2D tex; // Attributes - Rectangle rect; - Vector2 pos, origin; - double rotation; - boolisAlive; - Game1 mainGame; - int age; - Color color; - public void Update(); // Methods - public void Draw(); Player - Texture2D tex; // Attributes - Rectangle rect; - Vector2 pos, origin; - double rotation; - boolisAlive; - Game1 mainGame; - Vector2 bulletVel; - double xPart, yPart; - public void Update(); // Methods - public void Draw();
GameObject - Texture2D tex; // Attributes - Rectangle rect; - Vector2 pos, origin; - double rotation; - boolisAlive; - Game1 mainGame; - public void Update(); // Methods - public void Draw(); FlameBullet : GameObject - Texture2D tex; // Attributes - Rectangle rect; - Vector2 pos, origin; - double rotation; - boolisAlive; - Game1 mainGame; - int age; - Color color; - public void Update(); // Methods - public void Draw(); Player : GameObject - Texture2D tex; // Attributes - Rectangle rect; - Vector2 pos, origin; - double rotation; - boolisAlive; - Game1 mainGame; - Vector2 bulletVel; - double xPart, yPart; - public void Update(); // Methods - public void Draw();
GameObject - Texture2D tex; // Attributes - Rectangle rect; - Vector2 pos, origin; - double rotation; - boolisAlive; - Game1 mainGame; - public void Update(); // Methods - public void Draw(); FlameBullet : GameObject - Texture2D tex; // Attributes - Rectangle rect; - Vector2 pos, origin; - double rotation; - boolisAlive; - Game1 mainGame; - int age; - Color color; - public void Update(); // Methods - public void Draw(); Player : GameObject - Texture2D tex; // Attributes - Rectangle rect; - Vector2 pos, origin; - double rotation; - boolisAlive; - Game1 mainGame; - Vector2 bulletVel; - double xPart, yPart; - public void Update(); // Methods - public void Draw();
GameObject - Texture2D tex; // Attributes - Rectangle rect; - Vector2 pos, origin; - double rotation; - boolisAlive; - Game1 mainGame; - public void Update(); // Methods - public void Draw(); FlameBullet : GameObject - int age; // Attributes - Color color; - public void Update(); // Methods - public void Draw(); Player : GameObject - Vector2 bulletVel; // Attributes - double xPart, yPart; - public void Update(); // Methods - public void Draw(); This class has 8 attributes! - inherited 6 - added 2 more This class has 9 attributes! - inherited 6 - added 3 more
GameObject - Texture2D tex; // Attributes - Rectangle rect; - Vector2 pos, origin; - double rotation; - boolisAlive; - Game1 mainGame; - public void Update(); // Methods - public void Draw(); FlameBullet : GameObject - int age; // Attributes - Color color; - public void Update(); // Methods - public void Draw(); Player : GameObject - Vector2 bulletVel; // Attributes - double xPart, yPart; - public void Update(); // Methods - public void Draw();
GameObject - Texture2D tex; // Attributes - Rectangle rect; - Vector2 pos, origin; - double rotation; - boolisAlive; - Game1 mainGame; - public void Update(); // Methods - public void Draw(); FlameBullet : GameObject - int age; // Attributes - Color color; Player : GameObject - Vector2 bulletVel; // Attributes - double xPart, yPart; This class has 8 attributes and 2 methods This class has 9 attributes and 2 methods
GameObject - Texture2D tex; // Attributes - Rectangle rect; - Vector2 pos, origin; - double rotation; - boolisAlive; - Game1 mainGame; - public void Update(); // Methods - public void Draw(); FlameBullet : GameObject - int age; // Attributes - Color color; Player : GameObject - Vector2 bulletVel; // Attributes - double xPart, yPart; • What if we don’t like the methods we inherited? • Game objects usually update differently • Fancier draw?
GameObject - Texture2D tex; // Attributes - Rectangle rect; - Vector2 pos, origin; - double rotation; - boolisAlive; - Game1 mainGame; - public void Update(); // Methods - public void Draw(); FlameBullet : GameObject - int age; // Attributes - Color color; - public void Update(); // New code - public void Draw(); Player : GameObject - Vector2 bulletVel; // Attributes - double xPart, yPart; - public void Update(); // New code - public void Draw(); • We override those methods: • “Redefine” those methods • New (specific) code
Inheritance • “Absorbing” the properties from another class • Benefits • Good design (organization) • Smaller code • Maintainable code! • To “override” a method is to “redefine” it
Access Modifiers in C# • public – “everyone” can see it (i.e. access it) • private – only the class that defined it can see it • protected – can be seen only by the class that defined it or any child classes • internal – we won’t talk about this…
Why not? GameObject Man : GameObject - string name; // Attributes - … - public void Update(); // Overridden - public void Draw(); - public void Run(); // new Wolf : GameObject - intnumTeeth; // Attributes - … - public void Update(); // Overridden - public void Draw(); - public void Run(); // new WolfMan : Man, Wolf - public void Run(); // ????
“Diamond of Death” GameObject Man : GameObject - string name; // Attributes - … - public void Update(); // Overridden - public void Draw(); - public void Run(); // new Wolf : GameObject - intnumTeeth; // Attributes - … - public void Update(); // Overridden - public void Draw(); - public void Run(); // new WolfMan : Man, Wolf - public void Run(); // ????
Inheritance • You inherit from your grandparent too! Object GameObject Weapon Enemy Bow Bow Magic Orc Poodle Professor