Snake extension...and Random Food placement

I am creating a Snake Game, and i have a couple questions

Page 1 of 1

2 Replies - 1223 Views - Last Post: 03 May 2009 - 01:38 PM Rate Topic: -----

#1 DraDallas2009  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 02-May 09

Snake extension...and Random Food placement

Posted 03 May 2009 - 12:44 PM

I am trying to create a Snake game. I was wanting to know how to make the snake "grow" Here is what i have from the snake...is there a way i can use this to make the extensions of the snake?

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+= 1; 
					   break;
				  case KeyEvent.VK_UP:  
					  arrowNum = 2;
					  snakeY-= 1; 
					  break;
				  case KeyEvent.VK_RIGHT:
					   arrowNum = 3;
					   snakeX += 1; 
					   break;
				  case KeyEvent.VK_LEFT: 
					  arrowNum = 4;
					  snakeX -= 1; 
					  break;
				  default: e.getKeyChar();
			  }
			  
			  repaint();
		  }
	  });
	}
	
		public void paintComponent(Graphics g)
   		{
   		
   			super.paintComponent(g);
   			g.setColor(Color.red);
   			g.fillRect(foodX, foodY, 9, 9);
   		
   			g.setColor(Color.green);
   			g.fillOval(snakeX, snakeY, 9, 9);
   			
   			g.setColor(Color.blue);
   			g.drawRect(10, 10, BARRIER_X, BARRIER_Y);
   		}	
	} 


also, to make the food pop up randomly after the first one has been set, how do i go about that? Here is my code for the random food so far. I would like to know where would this be set. Thanks
	private int foodPlaceX;
	private int foodPlaceY;
	Random foodPlace = new Random();
	foodPlaceX = foodPlace.nextInt(279);
	foodPlaceY = foodPlace.nextInt(389);



Is This A Good Question/Topic? 0
  • +

Replies To: Snake extension...and Random Food placement

#2 DraDallas2009  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 02-May 09

Re: Snake extension...and Random Food placement

Posted 03 May 2009 - 12:44 PM

I am trying to create a Snake game. I was wanting to know how to make the snake "grow" Here is what i have from the snake...is there a way i can use this to make the extensions of the snake?

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+= 1; 
					   break;
				  case KeyEvent.VK_UP:  
					  arrowNum = 2;
					  snakeY-= 1; 
					  break;
				  case KeyEvent.VK_RIGHT:
					   arrowNum = 3;
					   snakeX += 1; 
					   break;
				  case KeyEvent.VK_LEFT: 
					  arrowNum = 4;
					  snakeX -= 1; 
					  break;
				  default: e.getKeyChar();
			  }
			  
			  repaint();
		  }
	  });
	}
	
		public void paintComponent(Graphics g)
   		{
   		
   			super.paintComponent(g);
   			g.setColor(Color.red);
   			g.fillRect(foodX, foodY, 9, 9);
   		
   			g.setColor(Color.green);
   			g.fillOval(snakeX, snakeY, 9, 9);
   			
   			g.setColor(Color.blue);
   			g.drawRect(10, 10, BARRIER_X, BARRIER_Y);
   		}	
	} 


also, to make the food pop up randomly after the first one has been set, how do i go about that? Here is my code for the random food so far. I would like to know where would this be set. Thanks
	private int foodPlaceX;
	private int foodPlaceY;
	Random foodPlace = new Random();
	foodPlaceX = foodPlace.nextInt(279);
	foodPlaceY = foodPlace.nextInt(389);


Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,858
  • Joined: 06-March 08

Re: Snake extension...and Random Food placement

Posted 03 May 2009 - 01:38 PM

Double topics merged

My suggestion:
Forget your paint method
Make your panel a GridLayout of 500X400 or whatever you want
fill it with an array of JLabel[500][400]
makes all these JLabel setOpaque(true)

to move the snake:
- change the color of the JLabel ahead (can be North, East, West, South) of the head
- change the color of the JLabel pointed by the tail

You can keep in an array of JLabel (or even better an ArrayList) the list of all the squares where the snake is
With an ArrayList you can insert at the head and delete the last one
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1