It is better to keep one question per post so each question gets the proper deserved attention.
The code you provided above is a bit hard to debug. It is better if you provide a minimum example demonstrating how the code works. Also, writing all in the same line makes it hard to read. I suggest you always aim to write easier to read code than compressed code.
This is avoided in the snake game by only implementing the side (left and right ) keys. If you are moving vertically, you can go right or left. If you are moving horizontally, you would go up or down, again, by only pressing the left or right keys. Next I assume you are comfortable with geometry and angles. One suggestion is to use an angle to describe direction. Say you start with an angle of 0 moving from right to left. Pressing the left arrow key will increase the angle by 90 making the direction up. If you press the right arrow key, you will subtract 90 making the direction “negative 90” meaning your snake moves down. Adding 90 or -90 allows you to control the direction of the snake. I find this easier instead of trying to implement all the arrow combinations. The possible angles you would get are all multiples of 90 keeping in mind we are working with degree units. To manage direction, you can use a switch statement: 0 to go left to right, +90 to go from bottom to top, -90 to go opposite and 180 or -180 to go from right to left. Angle larger that 180 should be mapped back to the range of 0 to 180.