def NewTransform(cls, car, nextMove): # Cases when vehicle want to change intensity of turning (turning more or less). We need to check if it is not # turned to its limits. if nextMove == Move.LEFT and car.slipAngle > -cls.maximalSlipAngle: car.slipAngle -= cls.voluntaryChangeOfSlipAngle elif nextMove == Move.RIGHT and car.slipAngle < cls.maximalSlipAngle: car.slipAngle += cls.voluntaryChangeOfSlipAngle else: # If vehicle don't want to change the intensity of turning it will go back to driving straight ahead by default, if car.slipAngle > 0: car.slipAngle -= cls.changeOfSlipAngleDuringReturning elif car.slipAngle < 0: car.slipAngle += cls.changeOfSlipAngleDuringReturning # Define some abbreviations f_s = cls.sideAccelerationFactor f_l = cls.longitudinalAccelerationFactor v_0 = cls.velocityValue # Determine acceleration sideAcceleration = Vector(f_s * car.slipAngle, 0).rotate(-car.rot + 90) longitudinalAcceleration = Vector(f_l * (v_0 - car.velocity.length()), 0).rotate(-car.rot) resultantAcceleration = sideAcceleration + longitudinalAcceleration # Determine transform parameters (we determined acceleration before. It was necessary for this step) newVelocity = car.velocity + cls.deltaTime * resultantAcceleration newPos = car.pos + cls.deltaTime * car.velocity return newPos, newVelocity.angle, newVelocity
def scaledImage(cls, image, scale): # We can scale in two dimensions. Statements below solves problem when "scale" argument was passed as single # float. In that case, we scale image in two dimensions keeping the proportions. try: scale[0] except TypeError: scale = Vector(scale, scale) newSize = Vector(image.get_rect().size).ScaledByVector(scale) return pg.transform.scale(image, newSize.asInt())
def SetUpCamera(cls): cls.ExecuteIfHasNotBeenYet(SetUpManager.SetUpBaseCar) BaseCar.map.CalculateSize() Camera.mapSize = BaseCar.map.size Camera.windowSize = Vector(SETTINGS.DISPLAYER.WINDOW_WIDTH, SETTINGS.DISPLAYER.WINDOW_HEIGHT) Camera.Create()
def CalculateOriginalRelativePos(cls, originalRectOfCar, coeff): # Calculate position of radar if topleft(!) corner of car is placed on the beginning of coordinate system. originalPos = Vector(coeff * originalRectOfCar.width, 0.5 * originalRectOfCar.height) # Calculate position of radar if center(!) of car is placed on the beginning of coordinate system. cls.originalRelativePos = originalPos - originalRectOfCar.center
def InitialVelocity(self): ''' Calculate initial velocity. Must be used before first step, but after setting "rot" attribute. :return: Vector velocity ''' return Vector(self.__class__.velocityValue, 0).rotate(-self.rot)
def CalculateSize(self): ''' Calculate size of map. See: "size" and "margin" in class description. ''' width = max( [barrier.limitingRect.right for barrier in self.listOfBarriers]) height = max( [barrier.limitingRect.bottom for barrier in self.listOfBarriers]) self.size = Vector(width + self.__class__.margin, height + self.__class__.margin)
def SetUpDisplayer(cls): pg.init() Displayer.windowSize = Vector(SETTINGS.DISPLAYER.WINDOW_WIDTH, SETTINGS.DISPLAYER.WINDOW_HEIGHT) Displayer.backgroundColor = SETTINGS.DISPLAYER.BACKGROUND_COLOR Displayer.meshColor = SETTINGS.DISPLAYER.MESH_COLOR Displayer.framesPerSecond = SETTINGS.DISPLAYER.FRAMES_PER_SECOND Displayer.captionColor = SETTINGS.DISPLAYER.CAPTION_COLOR Displayer.captionFontName = SETTINGS.DISPLAYER.CAPTION_FONT_NAME Displayer.captionFontSize = SETTINGS.DISPLAYER.CAPTION_FONT_SIZE Displayer.ConnectSpritesManager()
def LoadFromFile(self, filename): ''' Load map from file. Example of proper constructed file: " line_1: 100 200 270 // car: position (2 first), rotation (lat one) line_2: 300 400 90 2.5 0.5 // 1st barrier: position (2 first), rotation (middle), scale (2 last) line_3: 600 700 180 2 1 // 2nd barrier: position (2 first), rotation (middle), scale (2 last) line_4: 100 100 45 1.2 1.2 // 3rd barrier: position (2 first), rotation (middle), scale (2 last) . . . line_n+1: 350 100 30 1.5 1.5 // n'st barrier: position (2 first), rotation (middle), scale (2 last) ''' lines = FilesManager.LinesFromFile(filename) # Divide the data into parts for the car and barriers respectively firstLine, rest = lines[0], lines[1:] # Convert to list of floats firstLine = BuiltInTypesConverter.StringToFloats(firstLine) # Unpack rawCarSuggestedPos, rawCarSuggestedRot = firstLine[0:2], firstLine[4] # Convert to our coordinate system. carSuggestedPos, carSuggestedRot = Map.ConvertPos( rawCarSuggestedPos), Map.ConvertRot(rawCarSuggestedRot) # Create the right objects. self.carSuggestedPos, self.carSuggestedRot = Point( carSuggestedPos), carSuggestedRot # Steps in loop below are similar to steps above. for line in rest: line = BuiltInTypesConverter.StringToFloats(line) rawPos, rawRot, rawScale = line[0:2], line[4], line[2:4] pos, rot, scale = Map.ConvertPos(rawPos), Map.ConvertRot( rawRot), Map.ConvertScale(rawScale) barrier = Barrier() barrier.pos, barrier.rot, barrier.scale = Point(pos), rot, Vector( scale) # Calculate parameters necessary for algorithms. barrier.Create() self.listOfBarriers.append(barrier)
def CalculateOriginalRelativePos(cls, originalRectOfCar, coeff): ''' Calculate "originalRelativePos" which will be useful for following work of the class. :param originalRectOfCar: pygame.Rect :param coeff: float 0<...<1 Determine where radar is placed on the car. Examples: coeff=0 -> radar is on the rear edge of car coeff=0.5 -> radar is in the middle of car coeff=1 -> radar is on the front edge of car ''' # Calculate position of radar if topleft(!) corner of car is placed on the beginning of coordinate system. originalPos = Vector(coeff * originalRectOfCar.width, 0.5 * originalRectOfCar.height) # Calculate position of radar if center(!) of car is placed on the beginning of coordinate system. cls.originalRelativePos = originalPos - originalRectOfCar.center
def SetUpBarrier(cls): cls.ExecuteIfHasNotBeenYet(SetUpManager.SetUpPathsManager) Barrier.baseSize = Vector(pg.image.load(PathsManager.GetPath("barrier")).get_size())
def InitialVelocity(self): return Vector(self.__class__.velocityValue, 0).rotate(-self.rot)
def test_0(self): self.assertEqual(Vector(20, 34).angle, 301)
def test_0(self): b1 = Barrier() b1.scale = Vector([2.1, 3.1]) b1.CreateOriginalRect() self.assertEqual(b1.original_rect.bottomright, (67, 99))