def __init__(self,world,vel,mass,max_speed,max_turn_rate,max_force,**kwargs): self._velocity = Vector2(vel) self.max_speed = max_speed self._max_turn_rate = max_turn_rate self._max_force = max_force self.mass = mass # create a reference to steering behavior and # pass a reference of ourselves to it self._steering = SteeringBehaviors(self) self._steering.seek_on() self._world = world super().__init__(Vehicle.IMG_FILE, **kwargs) #TODO: Check that we have a position in kwargs #if not ('pos' in kwargs): # raise ValueError("you must specify a pos arg. See pygamezero Actor documentation") self.exact_pos = Vector2(self.pos) self.angle = self.velocity.as_polar()[1] self._heading = Vector2(cos(radians(self.angle)), -sin(radians(self.angle))) self._side = self._heading.rotate(90)
class Vehicle(Actor2): IMG_FILE = 'player' # Document the constructor. Essentially what we are doing here # is handling all of the vehicle specific args as positional parameters # everything passed in is wrapped up as kwargs and passed into the pgzero # Actor superclass. Look at the Actor class documentation to see what the valid # args are for this. # # This class will also handle the image for the Vehicle internally. def __init__(self,world,vel,mass,max_speed,max_turn_rate,max_force,**kwargs): self._velocity = Vector2(vel) self.max_speed = max_speed self._max_turn_rate = max_turn_rate self._max_force = max_force self.mass = mass # create a reference to steering behavior and # pass a reference of ourselves to it self._steering = SteeringBehaviors(self) self._steering.seek_on() self._world = world super().__init__(Vehicle.IMG_FILE, **kwargs) #TODO: Check that we have a position in kwargs #if not ('pos' in kwargs): # raise ValueError("you must specify a pos arg. See pygamezero Actor documentation") self.exact_pos = Vector2(self.pos) self.angle = self.velocity.as_polar()[1] self._heading = Vector2(cos(radians(self.angle)), -sin(radians(self.angle))) self._side = self._heading.rotate(90) def update(self,dt): steering_force = self._steering.calculate() accel = steering_force / self.mass self.velocity += accel * dt truncate_ip( self.velocity, self.max_speed) self.exact_pos += self.velocity * dt if self.velocity.length() > 0.0001: self._heading = self._velocity.normalize() self._side = self._heading.rotate(90) self.pos = self.exact_pos self.angle = -self.velocity.as_polar()[1] def toggle_behavior(self,behavior): self._steering.toggle_behavior(behavior) @property def velocity(self): return self._velocity @velocity.setter def velocity(self,vec): self._velocity = Vector2(vec) @property def world(self): return self._world