## Pathfinding with pygame

### Pathfinding with pygame

im stuck! im trying to create pathfinding for me and my friends RPG

so we are trying to make the enemy find its way to the player

since the camera is focused on the player which means the player is always at 350,470 but I don't know where to start

anyone have any ideas

Here is the code
ChristianCareaga

Posts: 53
Joined: Sat Jun 22, 2013 9:54 am

### Re: Pathfinding with pygame

Well technically if you want to do it properly you are going to have to learn the A* algorithm.

I just posted an update to my astar study in Completed Scripts here:
[pygame] A* pathfinding demo

You could also try using a library. I have had NetworkX recommended to me as a decent solution. This will however create another dependency for your game making it more trouble for users to run.

-Mek
• Use code tags when posting code.
• Include any errors with your post (in code tags).
• Make examples the minimum length to demonstrate your issue.

Mekire

Posts: 1473
Joined: Thu Feb 07, 2013 11:33 pm
Location: Tucson, Arizona

### Re: Pathfinding with pygame

Mekire wrote:Well technically if you want to do it properly you are going to have to learn the A* algorithm.

I just posted an update to my astar study in Completed Scripts here:
[pygame] A* pathfinding demo

You could also try using a library. I have had NetworkX recommended to me as a decent solution. This will however create another dependency for your game making it more trouble for users to run.

-Mek

I looked into that a bit but it looks kinda hard guess im gonna have to try it
ChristianCareaga

Posts: 53
Joined: Sat Jun 22, 2013 9:54 am

### Re: Pathfinding with pygame

Well depending on your needs it might be over kill for you.

If your enemy agents are restricted to motion in four directions (up,down,left,right) then you can easily figure out (from the vector between the enemy and the player) which direction(s) would bring the enemy closer.

So a naive approach would be:
Code: Select all
`1. find the directions that reduce distance between player and enemy.2. test if these directions are unoccupied by obstacles.3. If a tested cell is unoccupied then move in that direction;     else pick a direction that doesn't bring you closer and move that way.`

This will create an enemy that tries to chase in the direction of your player when possible. It will however be completely incapable of maneuvering around obstacles intelligently. If this is sufficient to your needs, then try it. Otherwise yes, you will have to learn about actual path finding algorithms.

-Mek
• Use code tags when posting code.
• Include any errors with your post (in code tags).
• Make examples the minimum length to demonstrate your issue.

Mekire

Posts: 1473
Joined: Thu Feb 07, 2013 11:33 pm
Location: Tucson, Arizona

### Re: Pathfinding with pygame

My partner actually figured it out using A star

there is still alot to do to make it better but the enemy does find its way around obstacles and to the player

tell us what you think