28 lines
1.1 KiB
Python
28 lines
1.1 KiB
Python
class Particle:
|
|
MAX_HISTORY = 5 # maximal so viele Undos möglich
|
|
|
|
def __init__(self, position, velocity=(0, 0), acceleration=(0, 0), history=()):
|
|
self.position = position
|
|
self.velocity = velocity
|
|
self.acceleration = acceleration
|
|
self.history = list(history)
|
|
|
|
def update(self):
|
|
new_velocity = (self.velocity[0] + self.acceleration[0],
|
|
self.velocity[1] + self.acceleration[1])
|
|
new_position = (self.position[0] + new_velocity[0],
|
|
self.position[1] + new_velocity[1])
|
|
# Vorversion (self) an die neue History anhängen, alte Liste bleibt unverändert
|
|
new_history = (self.history + [self])[-Particle.MAX_HISTORY:]
|
|
return Particle(new_position, new_velocity, self.acceleration, new_history)
|
|
|
|
def undo(self):
|
|
if not self.history:
|
|
return self # nichts mehr zum Zurückgehen
|
|
return self.history[-1] # Vorversion (hat ihre eigene History)
|
|
|
|
def __repr__(self):
|
|
return f"Particle(pos={self.position}, vel={self.velocity})"
|
|
|
|
|
|
|