Hi! Hope it's okay to post in this thread. I'm new to programming and have been following the Python/libtcod tutorial. I saw there were some snippets of code involving a fix for mobs getting caught on corners earlier on in this thread, but I figured I'd try and come up with my own method.

def move(self, dx, dy):

if not is_blocked(self.x + dx, self.y + dy):

self.x += dx

self.y += dy

self.wait = self.speed

elif self is not player:

#It's blocked, can we get around it?

#-------------------------------------------------------

#Diagonal Shenanigans/Caught on corner

#-------------------------------------------------------

#Move RIGHT+UP

if dx == 1 and dy == 0 and player.y < self.y: dy = -1

#Move RIGHT+DOWN

elif dx == 1 and dy == 0 and player.y > self.y: dy = 1

#Move LEFT+UP

elif dx == -1 and dy == 0 and player.y < self.y: dy = -1

#Move LEFT+DOWN

elif dx == -1 and dy == 0 and player.y > self.y: dy = 1

#Move DOWN+LEFT

elif dx == 0 and dy == 1 and player.x < self.x: dx = -1

#Move DOWN+RIGHT

elif dx == 0 and dy == 1 and player.x > self.x: dx = 1

#Move UP+LEFT

elif dx == 0 and dy == -1 and player.x < self.x: dx = -1

#Move UP+RIGHT

elif dx == 0 and dy == -1 and player.x > self.x: dx = 1

#-------------------------------------------------------

#Mob probably stuck in a straight line

#

#This doesn't work when you end up with 4+ enemies in a

#line, but that might be a little too crazy to deal with

#-------------------------------------------------------

#Stuck going LEFT or RIGHT

if (dx == -1 and dy == 0 and player.y == self.y) or (dx == 1 and dy == 0 and player.y == self.y):

if not is_blocked(self.x + dx, self.y + 1):

dy = 1 #Move DOWN

elif not is_blocked(self.x + dx, self.y + -1):

dy = -1 #Move UP

#Stuck UP or DOWN

elif (dx == 0 and dy == -1 and player.x == self.x) or (dx == 0 and dy == 1 and player.x == self.x):

if not is_blocked(self.x + 1, self.y + dy):

dx = 1 #Move RIGHT

elif not is_blocked(self.x + -1, self.y + dy):

dx = -1 #Move LEFT

#Now try again

if not is_blocked(self.x + dx, self.y + dy):

self.x += dx

self.y += dy

self.wait = self.speed

I -kinda- feel like my code is a bit redundant, or maybe I should be working this into move_towards, but it works how I expect and makes things pretty fun.

I'd love to hear any tips or improvements.

Also! It seems that attachments on these forums all lead to a 404 page, would there be a repository anywhere for those files?

EDIT: Just realized I should make some checks so this doesn't affect the player. (Fixed now)

And just for fun, here it is in action:

https://gfycat.com/FrenchPersonalAoudad