Prohibiting the snake from going through itself

Feb 18, 2017 at 6:58pm
I'm making a snake game in the command prompt. The basics are close to be finished. I've created the area where the snake can move using a 2D array. I move the snake around using SetConsoleCursorPosition. Basically what I have left is prohibiting the snake from going "through" itself.
I'm unsure how to do it. If you don't understand what I mean, basically you should not be able to turn towards the direction you came from. E.g, Going left, shouldn't be able to turn right and go through the body of the snake. I've managed to make the snake die if it runs into itself but it shouldn't be possible to even do what I'm describing. SORRY for terrible explanation, please ask if you don't understand what I mean.

**The way the snake moves is, start at coordinates 1,1. Press arrow right, x coordinate will be upped by one, new coordinates 2,1. Last piece of the snake will be erased every time a new head is drawn.
Feb 18, 2017 at 8:35pm
@Krasit
Before printing the snake, check what is located at the site being moved to. If it's clear or fruit (if you've gotten to that part where it's added), then let snake move. But if it's a wall or the value for the snake, just go to the routine that prints that you died.
Feb 18, 2017 at 8:38pm
ah. You have to ignore input that corresponds to the opposite direction you are currently going. If you are going east, and get west, you ignore that input. If you are going north, and get south, ignore that input. so you have to track current direction & user's input, and play off each other.

Topic archived. No new replies allowed.