Code

OOP Examples: A Guide to Object-Based Programming, Part 8

OOP Examples: A Guide to Object-Based Programming, Part 8

Introductory Python Course: For All Skill Levels ➞ Four impressive projects for your portfolio and the opportunity to ask questions to an expert. Find out what skills you can acquire in the course.

Learn More

Congratulations to everyone who made it through all the previous materials and reached the stage of practical classes on object-oriented programming in C#. Ahead of us, we will create a simple console game. We will apply the OOP skills we have already mastered and also learn a few new techniques.

  • Classes and objects are fundamental concepts in object-oriented programming. A class can be thought of as a template or model that defines the structure and behavior of objects created based on it. It describes the properties and methods that will be available to instances of this class.

    An object, in turn, represents a specific instance of a class. It has all the characteristics defined in the class and can interact with other objects. Each object can have its own unique values ​​for attributes, which makes it individual, despite the fact that it is based on the same class.

    Thus, classes serve as a basis for creating objects, and objects allow you to implement the functionality defined by the class in practical programming scenarios.

  • Characteristics of interaction with objects.
  • Access modifiers and encapsulation are important concepts in object-oriented programming. Access modifiers determine the level of access to classes and their members, allowing you to control which elements can be visible and accessible from other parts of the program. There are several types of modifiers, such as public, private, and protected, each with its own characteristics and uses.

    Encapsulation, in turn, involves hiding an object's internal implementation from the outside world. This is achieved by restricting access to data and methods, which helps protect the object's state and prevent its incorrect use. Thus, encapsulation contributes to the creation of more secure and manageable software solutions by ensuring data integrity and simplifying interaction with objects.

  • Polymorphism and Method Overloading.
  • Polymorphism.
  • Inheritance and a little about polymorphism.
  • Abstract classes and interfaces are important concepts in object-oriented programming. They allow developers to create flexible and extensible structures by providing a foundation for inheritance and implementation of functionality.

    An abstract class is a class that cannot be instantiated on its own. Instead, it serves as a foundation for other classes that inherit its properties and methods. Abstract classes can contain both abstract methods, which have no implementation, and regular methods with a defined implementation. This allows for the creation of shared functionality that inheriting classes can override or use by default.

    Interfaces, in turn, define a set of methods that a class must implement but do not contain their implementations. This provides a greater degree of abstraction and allows classes with different inheritance hierarchies to implement the same interfaces. Using interfaces helps create a more decoupled and modular architecture, where classes can easily interact with each other by following common contracts.

    Ultimately, both abstract classes and interfaces play a key role in software design, allowing developers to organize code more efficiently and create solutions that are easier to maintain and extend in the future.

  • Practical.

What the game will be like

The game will be a console project with symbolic graphics, where players will be able to move around various locations, fight with non-player characters, as well as open inventory and use items.

The game will be organized as follows:

  • When the game starts, the InitGame() method is called, which is responsible for creating game objects, items, and other elements.
  • Then the Update() method will be called, which is responsible for updating the current state games.

Inside the Update() method, you need to implement a loop that will perform the following operations:

  • Create a visualization of the location or items in the inventory.
  • Determine which key was pressed by the player.
  • Transfer the keystroke to the controller.
  • Depending on which key was pressed, the controller will initiate a call to methods of game objects, such as Move() or Use().

In the process of managing the gameplay, we use three static classes that act as controllers.

  • LocationController is responsible for tracking player actions within the location.
  • InventoryController is responsible for tracking player interactions with the inventory.
  • GraphicsController is responsible for controlling the output of graphic information.

Information about game parameters, including the size of the location and the list of objects, is stored in the static Game class. Specific objects will be represented through the GameObject (as the primary), Player, and NPC classes. Position and movement within the environment will be controlled by the Position class.

The object implementation system uses the Item class as the base class, as well as its derivatives Potion and Meal. If an item has a usability, it implements the IUsable interface.

The project is not very large, but if you try to present it entirely in an article, there will be no room for text. Therefore, only the key points will be presented. For a complete overview of the project, download the entire source code from the repository on GitHub.

The process of developing game elements

Let's start by defining the GameObjects class, which will serve as the basis for creating the Player and NPC classes.

Pay attention to the Die() method. This method is triggered when an object's health reaches zero. It removes the object from the Game.Objects collection, which allows the memory it occupies to be freed. Destructors perform a similar function; they are activated before an object is finally deleted from memory.

The NPC class will be the basis for developing the game's artificial intelligence logic. The Player class, in turn, will include methods for managing the player character. For example, in the future, we will add the ability to use inventory items in this class.

As mentioned earlier, created objects will be stored in a collection of the static Game class. Below is how this is implemented:

You may have noticed the GameMode data type—it's an enumeration, which makes list creation more convenient.

One option that can replace enumeration classes is numbers. We could express it like this:

However, this may not be entirely convenient, since you will have to constantly keep track of what number you specified and for what purpose.

Graphics

In order to provide the display of objects and locations, we will create a GraphicsController class.

The formation of objects is carried out using symbols that vary depending on the direction in which the object is oriented.

Now we need to make changes to the Program class to create the first instances and display them on the console screen.

Sorry, but your request does not contain text for rewriting. Please provide the text you would like to rewrite.

Control

In order to ensure the movement of an object controlled by the player, it is necessary to implement a control system.

This class tracks which key the player presses and passes the received commands on. For example, it interacts with the Position class, which controls the movement of objects in the game space.

It is possible to test the movement process.

A combat system was also introduced along with the movement:

Inventory

The inventory system is based on the Item class:

The Potion and Meal classes have virtually the same structure, differing only in the text messages they display. Therefore, in this case, the code for only one of these classes will be presented:

Note that this class implements the IUsable interface:

In order for the user to be able to view items in their inventory, a new method must be implemented in the GraphicsController:

In addition, in the Program class, it is necessary to supplement the switch statement with a new option for modes:

Control will be carried out through the InventoryController:

Now let's turn to the Player object.

Two new keywords are presented here:

  • is - checks for the presence of the specified interface in the given object.
  • as - ensures the implementation of the interface.

Sorry, but I don't see the text you want to rewrite. Please provide the text, and I will be happy to help you with its revision.