Skip to content

Instantly share code, notes, and snippets.

@houkensjtu
Last active February 17, 2020 15:41
Show Gist options
  • Save houkensjtu/3d77cb8fede6f8f56a1f2f4a3a957353 to your computer and use it in GitHub Desktop.
Save houkensjtu/3d77cb8fede6f8f56a1f2f4a3a957353 to your computer and use it in GitHub Desktop.
A car simulator
class Component:
def __init__(self, p, s):
self.size = 0
self.power = p
self.status = s
class Engine(Component):
def __init__(self, p, s, a):
super().__init__(p, s)
self.accel = a
class Car:
def __init__(self, e, m, d, t):
self.engine = e
self.mass = m
self.distance = d
self.time = t
def drive(self, dt):
self.time += dt
self.distance += 0.5 * self.engine.accel * dt**2
def plot(self):
print("The current time is", self.time)
print("Distance = ", self.distance)
if __name__ == "__main__":
GM = Engine(100, True, 10)
Toyota = Engine(70, True, 11)
mycar = Car(GM, 1.5, 0, 0)
for t in range(0, 10, 1):
mycar.drive(1.0)
mycar.plot()
mycar = Car(Toyota, 1.2, 0, 0)
for t in range(0, 10, 1):
mycar.drive(1.0)
mycar.plot()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment