Understanding Classes and Objects
Clear Code
Introduction
- An object is a container for variables and functions
- A monster in a game might have
- Variables - health, energy, stamina, damage
- Functions - attack, movement, animations
- Variables called Attributes in an object
- Functions called Methods in an object
- An object doesn't have to have methods
- An obstacle in a game might only have a position and size
- But a move method of a monster or player might be able to change the position of the obstacle
- A Class is a blueprint for an object / instance
- Instances of an object
- Can have different attributes - 80 health for monster 1, 40 health for monster 2
- Cannot have different methods, but only interact with its own instance attributes
- Classes can inherit from another class
- Shark could inherit from a generic Monster class
- Shark could have its own bite attack, but use health and energy from the monster class
- Classes make it much easier to work with scope
Class Introduction