It really depends on how you want to use your class structure. If you are just trying to connect KX_GameObjects together in a parent/child relationship then you don’t even need custom classes.
If you want to model a car where you can change the wheels out to change the car’s performance then building something like this starts to make a bit more sense.
But how you design your classes all comes back to the operations you need them to perform and/or the types of calculations they need to do. So I imagine a system where you will have a car (that has 4 wheels) and the traction of the car depends on the type of wheels on it.
# Defines a car object
class Car(object):
def __init__(self):
# Tracks wheels on the car.
self.wheels = None
def getTraction(self):
return self.wheels.getTraction()
class Wheels(object):
def __init__(self, traction):
self.traction = traction
def getTraction(self):
# Note, in this case you don't need a function. But if the traction was different depending on
# your surface then you might need to compute. For example, racing slicks are great for
# drag racing but have terrible traction in the rain.
return self.traction
Then you can build a car like this:
car = Car()
car.wheels = Wheels(1.0) # Friction rating of 1.0, whatever that might mean.
print(car.getTraction())
car.wheels = Wheels(2.0) # Got an upgrade!
print(car.getTraction())
But what if you want to track each wheel separately? You might have a complicated simulator where you track wear on each wheel separately. Then your Car class needs to have a separate Wheel that has a traction and health rating.
You probably want to keep the system fairly simple where a Car class holds Wheels, Engine, Exhaust, Suspension classes that model the specific parts of your car.
If you need to get super fancy where you have a collection of ‘parts’ and you can create a vehicle by combining them then you might need something like a Parts class where you can inherit Wheels, Engine, Exhaust, etc classes so they are all parts. Then your Car class would be a collection of Parts and your Car would need some fancy logic to validate that it had a valid combination of Parts assigned to it.
So, what kind of car game are you building? Because what you want to do determines how you should go about doing it.