From the course: Object-Oriented Programming with C#
Unlock this course with a free trial
Join today to access over 25,300 courses taught by industry experts.
Creating a key and gold - C# Tutorial
From the course: Object-Oriented Programming with C#
Creating a key and gold
- [Instructor] Now that we've created the chest, it's time to make a key to unlock it and gold to find. Let's go ahead and create a new key class for us to work with. After you've added the name space and have created the public class key that extends item, we can begin adding the code that makes the key work. The first thing we need to do is add a private field to store a reference to the house. Next, we'll create a constructor for the key and we'll pass in a reference to the house. Now we can save the reference of the house to the house field in our key class and let's make sure that we toggle the CanTake and single use flags in the base item class. We want to make sure that the key disappears from the player's inventory once they use it. This will ensure that the player is able to pick the key up and that it disappears from the player's inventory after it's been used. Now let's go ahead and override the name property…