python - boundaries target-mouse walking pygame -
i have move player mouse. i'll explain better: when click screen , have target , hero moves actual position new one. have put boundaries , , i'm having problems. tried solution i'm posting here, when player hits boundaries gets stuck these boundaries.
hero(player) class:
def get_direction(self, target): ''' function: takes total distance sprite.center sprites target (gets direction move) returns: normalized vector parameters: - self - target x,y coordinates of sprites target can x,y coorinate pair in brackets [x,y] or parentheses (x,y) ''' if self.target: # if square has target position = vector(self.rect.centerx, self.rect.centery) # create vector center x,y value target = vector(target[0], target[1]) # , 1 target x,y self.dist = target - position # total distance between target , position direction = self.dist.normalize() # normalize constant in directions return direction def distance_check(self, dist): ''' function: tests if total distance sprite target smaller ammount of distance normal sprite travel (this lets sprite know if needs slow down. want slow down before gets it's target) returns: bool parameters: - self - dist total distance sprite target can x,y value pair in brackets [x,y] or parentheses (x,y) ''' dist_x = dist[0] ** 2 # gets absolute value of x distance dist_y = dist[1] ** 2 # gets absolute value of y distance t_dist = dist_x + dist_y # gets total absolute value distance speed = self.speed ** 2 # gets aboslute value of speed if t_dist < (speed): # read function description above return true def walking(self): ''' function: gets direction move applies distance sprite.center () parameters: - self ''' self.dir = self.get_direction(self.target) # direction if self.dir: # if there direction move if self.distance_check(self.dist): # if need stop self.rect.center = self.target # center sprite on target else: # if need move normal self.x += (self.dir[0] * self.speed) # calculate speed direction move , speed constant self.y += (self.dir[1] * self.speed) self.rect.center = (round(self.x),round(self.y)) # apply values sprite.center
level 1:
def processinput(self, events): 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 sprite.target mouse click position def update(self): #put game logic in here 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()
main:
if not quit_attempt: active_scene.processinput(filtered_events) active_scene.update() active_scene.render(display_game) active_scene = active_scene.next
in update()
function used following code:
if self.__myhero.x >= 740: self.__myhero.target = none
if hero go on boundaries , change target , set none
because want stops walking. stops himself , remains blocked. don't know why. can help?
if hero goes on boundaries, change it's target none stop walking. problem here lies in if statement performs action. once enter statment:
if self.__myhero.x >= 740: self.__myhero.target = none
the value of self.__myhero.x
remains bigger or equal 740. therefore should avoid value of self.__myhero.x
bigger or equal 740 within if statement otherwise keep setting none
self.__myhero.target
Comments
Post a Comment