Well, i have a problem with the snake in my problem... if it runs into the body it dies. But, if I press, for example, up when i am going down, it loses. How do i keep my self from doing that? Here is the code for the movement:
public KeyboardPanel()
{
addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
switch(e.getKeyCode())
{
case KeyEvent.VK_DOWN:
arrowNum = 1; // Creates a variable that creates the continuous foward
snakeY+= 10;
break;
case KeyEvent.VK_UP:
arrowNum = 2;
snakeY-= 10;
break;
case KeyEvent.VK_RIGHT:
arrowNum = 3;
snakeX += 10;
break;
case KeyEvent.VK_LEFT:
arrowNum = 4;
snakeX -= 10;
break;
default: e.getKeyChar();
}
repaint();
}
And this is the code that checks if the snake runs into itself:
snake =new Rectangle(snakeX, snakeY, 10, 10);
for(int i = 2; i < bodyX.size(); i++)
{
Rectangle body = new Rectangle(bodyX.get(i).intValue(), bodyY.get(i).intValue(), 10, 10);
if(snake.intersects(body))
{
System.out.println("Game Over >:-O");
System.exit(0);
}
}