Pac-man anticipated turns, need help

I'm attempting to build a pacman game. I don't need it to be even close to perfect, but I'm struggling with the anticipated turns feature. (How you can click in a direction towards a wall, and it keeps going in the previous direction until the direction you pressed is clear before initiating the turn) The way I've done it theoretically works, but it switches between lagging and super speed, and sometimes it doesn't even work. Normally, efficiency isn't what I'm going for since I only code for fun on and off, but the way I did this pushes the limits of the site. The code is horrendous, but I don't know a better way to do it.

Just click on the pacman sprite if it's not already there and scroll down.

Pac-man

This is my last year attemp to Pac-Man, if it could help you

(in french)

When you press a direction, store that in a variable called something like, next direction. Each frame ut will then check if you can go in that direction. If you can't, it keeps going in the current direction. If you can, it set's the current direction to the next direction, and keeps going. If it detects a wall in the current direction, just make it stop.

Thank you so much. You put the jumbled mess I was thinking into words so I could actually accomplish it. It still took some trial and error but it got there. Also, I learned that snap has this weird thing with point in -90 vs point in 270 degrees? It sees the number as the same (it will point them in the same direction) but uses/reads it differently. You can check out the code under the "when I receive when?" block. I had to use -90 or 270 for it to work.