#2. Object Oriented Programming

Table of Contents


In the development of the Red Riding Hood, I used an object-oriented approach. By using classes and inheritance, we created a game where every element, including the Player and Wolf, behaves as an independent object with its own properties and methods.


Inheritance

The core of our games is built on inheritance. Instead of rewriting movement, we learned specific characters can “inherit” traits from a base class.

Example: In Level 2 of Red Riding Hood, the Wolf class uses the extends keyword to inherit from the Character base class

Image 8 Image 8


OOP Hierarchy

In our Red Riding Hood game, we didn’t write the same code over and over for every character. Instead, we used a class hierarchy.

The Parent (Character): This is the “base class”
The Children (Wolf and Player): These are “sub classes”

By calling a special function super(), the wolf reaches up into the Parent class and grabs all the important foundations like gravity and screen position, so we don’t have to code them again.

Image 8


Object Literals

We avoided “hard-coding” numbers directly into our movement logic, and aimed for data driven design. We used Object Literals to store sprite data, speeds, and image paths.

Example: Our sprite_data_red object stores the height (192) and width (144) of the character. If we want to change the character’s size, we only have to change it in one place, and the rest of the game updates automatically.


State Management and Collision

State Management occurs to track what is happening in the game. When red riding hood collects 5 cookies, the “state” changes to show the congrats popup

Image 8


API Integration

To manage the transition between levels, I used API-style calls to the game engine. By using the transitionToLevel() function, Level 1 “talks” to the backend controller to stop the current loop and fetch the data for Level 2. This ensures a smooth flow between different parts of the software.