Hello , I am trying to make the classic snake game in
Java and facing some issues , well I am able to do some of the coding but not able to understand some of it and not able to implement some of the logic. The problem that I am facing is that if the snake is going up and I press the down key then the snake move on itself and the games ends , same thing happens on side , it is moving right and I press the left arrow key the snake moves on itself and the game is over . So how and I stop the snake to move on itself . The code when the snake eats one of its body part is
And I am not able to understand the logic behind this piece of code which adds a new snake body part when the snake eats food
the complete program is this
Class with the main function
The JPanel Class
The Snake Class
The Food Class
Thank you and have a nice day : )