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);
   				}			 
   			}

Umm, isn't that the point of the game? Moving up is technically considered colliding with itself. So, it's doing what it should. If you want it to do nothing when trying to move backwards into itself (like in the example you provided), put an if statement in saying, if you try to turn 180 degrees (or try to move backwards), don't, and then continue moving in the current direction.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.