边界目标鼠标行走 pygame

boundaries target-mouse walking pygame

我必须用鼠标移动播放器。我会更好地解释: 当我点击屏幕时,我有一个目标,我的英雄从他的实际位置移动到新位置。 我必须设定界限,但我遇到了一些问题。 我尝试了我在这里发布的解决方案,但是当玩家触及边界时,他会被困在这些边界上。

英雄(玩家)class:

    def get_direction(self, target):
    '''
    Function:
        takes total distance from sprite.center
        to the sprites target
        (gets direction to move)
    Returns:
        a normalized vector
    Parameters:
        - self
        - target
            x,y coordinates of the sprites target
            can be any x,y coorinate pair in
            brackets [x,y]
            or parentheses (x,y)
    '''
    if self.target: # if the square has a target
        position = Vector(self.rect.centerx, self.rect.centery) # create a vector from center x,y value
        target = Vector(target[0], target[1]) # and one from the target x,y
        self.dist = target - position # get total distance between target and position
        direction = self.dist.normalize() # normalize so its constant in all directions
        return direction 

def distance_check(self, dist):
    '''
    Function:
        tests if the total distance from the
        sprite to the target is smaller than the
        ammount of distance that would be normal
        for the sprite to travel
       (this lets the sprite know if it needs
        to slow down. we want it to slow
        down before it gets to it's target)
    Returns:
        bool
    Parameters:
        - self
        - dist
            this is the total distance from the
            sprite to the target
            can be any x,y value pair in
            brackets [x,y]
            or parentheses (x,y)
    '''
    dist_x = dist[0] ** 2 # gets absolute value of the x distance
    dist_y = dist[1] ** 2 # gets absolute value of the y distance
    t_dist = dist_x + dist_y # gets total absolute value distance
    speed = self.speed ** 2 # gets aboslute value of the speed
    if t_dist < (speed): # read function description above
        return True

def Walking(self):
    '''
    Function:
        gets direction to move then applies
        the distance to the sprite.center
        ()
    Parameters:
        - self
    '''
    self.dir = self.get_direction(self.target) # get direction
    if self.dir: # if there is a direction to move            
        if self.distance_check(self.dist): # if we need to stop
            self.rect.center = self.target # center the sprite on the target    
        else: # if we need to move normal    
            self.x += (self.dir[0] * self.speed) # calculate speed from direction to move and speed constant
            self.y += (self.dir[1] * self.speed)
            self.rect.center = (round(self.x),round(self.y)) # apply values to sprite.center

1 级:

def ProcessInput(self, events):
    for event in events:
        if event.type == pygame.KEYDOWN: 
            if event.key == pygame.K_LEFT:
                self.__myHero.moveLeft()
            if event.key == pygame.K_RIGHT:
                self.__myHero.moveRight()
        if event.type == pygame.MOUSEBUTTONDOWN:
            self.__myHero.target = event.pos
            #if event.type == pygame.MOUSEBUTTONDOWN:
                #print(pygame.mouse.get_pos())
                #self.__myHero.target = pygame.mouse.get_pos() # set the sprite.target to the mouse click position



def Update(self):
    #Put your game logic in here for the scene.
    #self.SwitchToScene(SecondScene())
    if self.__myHero.x >= 740:
        self.__myHero.target = None
    else:
        self.__myHero.Walking()


def Render(self, display_game):
    display_game.blit(self.image_background, (0, 0))
    display_game.blit(self.__myHero.getImage(), self.__myHero.rect.topleft)
    pygame.display.update()

主要:

if not quit_attempt:
    active_scene.ProcessInput(filtered_events)
    active_scene.Update()
    active_scene.Render(display_game)        
    active_scene = active_scene.next

Update()函数中我使用了以下代码:

if self.__myHero.x >= 740:
    self.__myHero.target = None

如果我的英雄越界,我会改变他的目标并将其设置为 None 因为我希望他停止走路。他停下了自己,但仍然完全被挡住了。我不知道为什么。 你能帮忙吗?

如果您的英雄越界,您将其目标更改为 None 以停止行走。这里的问题在于执行此操作的 if 语句。 输入此语句后:

if self.__myHero.x >= 740:
    self.__myHero.target = None

self.__myHero.x 的值仍然大于或等于 740。因此你应该做一些事情来避免 self.__myHero.x 的值在 if 语句中大于或等于 740,否则你将保留将 None 设置为 self.__myHero.target