边界目标 - 鼠标行走pygame

时间:2015-07-14 13:42:11

标签: python pygame mouseevent

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

英雄(运动员)课程:

    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 因为我希望他停止行走。他停了下来,但仍然完全被阻挡了。我不知道为什么。 你能帮忙吗?

1 个答案:

答案 0 :(得分:1)

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

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

self.__myHero.x的值仍然大于或等于740.因此,您应该做一些事情以避免self.__myHero.x的值在if语句中大于或等于740,否则您将继续设置{ {1}} None