Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8093035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:24:56+00:00 2026-06-05T20:24:56+00:00

So I have been fooling around with Pygame and have run into a problem

  • 0

So I have been fooling around with Pygame and have run into a problem that I can solve, but only in-elegantly.

Basically I have a unit(at this point just a red square) on a plain field of black. If I click the Unit and then click anywhere else on the field the unit proceeds to that point. The algorithm itself works pretty flawlessly, even once obstacles are introduced it handles path-finding quickly.

My problem is in my implementation. The unit is a 16 by 16 square, the field is thus divided into 16 by 16 squares. If I change the unit’s destination while it is in transit it sometimes “back-tracks” to the previous “way-point” before continuing on its new route.

I understand why this happens, The path finding module treats each square as a point on a grid, so that the pixel coordinate of point (2,2) is actually (32,32), when I pass the mouse coordinates into the path method I divide them by 16 getting rounded down integers(so (34,37) becomes (2,2)) so while the unit may be between actual “grid-points” the path finding module finds the path from its last actual way point, not it’s current location. when it passes the new route to the unit, the unit has to “back-track” in order to follow the route the path-finding module has found

clearly every game developer that does any kind of path-finding has solved this so I’m hoping that I can find a more elegant solution.
I don’t want to find a path along every single pixel on the screen just waypoints, but the backtracking is a little irritating. Any suggestions?

as suggested below is the concerned code:

in the main .py file

if pygame.mouse.get_pressed()[0]: # if left mouse button pressed
    mouse_x,mouse_y = pygame.mouse.get_pos()
    if selected: # checks if a unit is selected
        unit.getRoute((mouse_x/16,mouse_y/16))

in the unit.py file

def getRoute(self,target):
    self.route = path((self.x/16,self.y/16), target)

def getNode(self):
    if self.route:
        self.node = route.pop(0)
        dif_x,dif_y = self.node[0] - self.x, self.node[1] - self.y
        if dif_x > 0:
            self.vector_x = self.speed
        if dif_x < 0:
            self.vector_x = self.speed * -1
        else:
            self.vector_x = 0
        if dif_y > 0:
            self.vector_y = self.speed
        if dif_x < 0:
            self.vector_y = self.speed * -1
        else:
            self.vector_y = 0
    else:
        self.node = None
        self.vector_x = 0
        self.vector_y = 0

def update(self):

    if self.route or self.node:
        if self.route and not self.node:
            self.getNode()
        if (self.x,self.y) == self.node:
            self.getNode()

    self.x += self.vector_x
    self.y += self.vector_y
    self.rect.topleft = (self.x,self.y)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T20:24:57+00:00Added an answer on June 5, 2026 at 8:24 pm

    You should take care of managing this first step in a special way. This because even if the unit is already moving the cell specified with it remains the same until it reaches the next one.

    Now I suggest you to make sure to update the cell of the unit as soon as half of the movement has been done (so that the pathfinder will search for the path from the next cell and not from the previous one). In addition you should manage the special situation with something like

    if unit is standing still
      compute pathfinding as you would normally do
    else
      compute pathfinding from the unit cell
      discard the first cell of the pathfinding route (since it would backtrack)
      compute the route directly to the next cell (by drawing a correct path)
    

    Of course this will be easier or harder according to how much freedom of movement you have, if you allow only orthogonal movements that it will be straightforward, if you allow diagonal movement then you should make care to not cross over an obstacle (then it depends on how much tolerance you want to small glitches, like the unit overlapping with a corner of an obstacle).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have been looking on some tutorials for drawing canvas using SurfaceView, but the only
Have been trying to fix this problem I have here. Basically I have a
I've just been fooling around with a ListBox control that I want to style
I've been fooling around with OpenCV and want to continue working with it, but
Have been hearing all around ArrayList getting obsolete these days. But I do have
I'm just starting to teach myself Objective C and have been fooling around with
Have been digging around the docs but cannot find if there is a function
have been sitting with this problem for a few days and really can't seem
I have been looking into the source code of python-mpdor and it mentions that
Have been trying to encrypt an xml file to a string so that I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.