def MoveWithMouse(self): """ MoveWithMouse calculates the new position of the sprite based on the mouse's position """ # record the current, unchanged position of the sprite # self.old_loc = self.rect.topleft self.old_loc = self.actual_position # okay doing this one actually plays the dead animation, but the camera still follows an invisible santa into the void # grabs the x and y coordinate of the mouse's position on the screen x = self.target_location[0] y = self.target_location[1] # find the distance from sprite's current position and desired one dx = x - self.x dy = y - self.y # use the arctan function to find the angle from the horizontal to the desired position angle = math.atan2(dy, dx) # perform basic trig to move a multiple of `self.speed` towards the desired position # Using "min(5, self.distance/10)" lets the sprite move at a speed of 5 to self.distance/10, # so that its speed is a function of the distance from it to the mouse self.distance = straightDistance(self.x, self.y, x, y) self.x += int(min(5, self.distance/10) * math.cos(angle)) self.y += int(min(5, self.distance/10) * math.sin(angle))
def setScrollDirection(self, loc=None): """If keys are pressed or mouse is clicked, set a goal location to scroll toward. """ self.target_location = loc self.cardinal_direction = getCardinalDirection((self.cx, self.cy), self.target_location) self.distance_to_target = straightDistance((self.cx, self.cy), self.target_location) print(self.target_location) print(self.cardinal_direction) print(self.distance_to_target)
def MoveWithMouse(self): """ Moves a player toward the location of a mouse click. """ x = self.target_location[0] # get location from saved y = self.target_location[1] # target # Get the angle to move (in radians) dx = x - self.x dy = y - self.y angle = math.atan2(dy, dx) # if we acheive the target location don't keep moving if straightDistance(self.x, self.y, x, y) > 10: self.x += int(self.speed * math.cos(angle)) self.y += int(self.speed * math.sin(angle))
def MoveWithMouse(self): """ MoveWithMouse calculates the new position of the sprite based on the mouse's position """ # record the current, unchanged position of the sprite self.old_loc = self.rect.center x = self.target_location[0] y = self.target_location[1] # find the distance from sprite's current position and desired one dx = x - self.x dy = y - self.y # use the arctan function to find the angle from the horizontal to the desired position angle = math.atan2(dy, dx) # if the euclidian distance from the original sprite position to the desired is within 10 pixels # perform basic trig to move a multiple of `self.speed` towards the desired position if straightDistance(self.x, self.y, x, y) > 10: self.x += int(self.speed * math.cos(angle)) self.y += int(self.speed * math.sin(angle))