Hi,
I have to make a snake game. i need an array to store allposition of the snakes body. i will also need a variable to store the number of parts in the snakes body. I know that i have to use the number of parts ++ but i dont know where i have to put this, nor where to declare it.
Im pretty sure it should be in SnakeClass but not sure how to pass a parameter from SnakeClass to SnakeJPanel Class.
I first triedto write a method in SnakeClass but i could not pass the parameter to Jpanel class , (Somehow i was getting a compile error).
Below is my work i have done so far, so please help.

**
  * @author  insert your name here
  * @version   S2, 2006
*/
import java.awt.*;
public class Snake {
 private static final int MAX_BODY_PARTS = 1000;
 private int direction;
 private int left, top;
 private Rectangle[] rects ;
 private int numberOfParts;
 public Snake () {
  left = SnakeConstants.SNAKE_START_X;
  top = SnakeConstants.SNAKE_START_Y;
  direction = SnakeConstants.SNAKE_RIGHT;
  rects = new Rectangle[1000];
  rects[0] = new Rectangle( SnakeConstants.SNAKE_START_RECT );
  numberOfParts = 1;
 }
 public void add(){
  numberOfParts++;
 }
 public void drawSnake( Graphics g ) {
  g.setColor( Color.RED );
  g.fillRect( left, top, SnakeConstants.SIZE, SnakeConstants.SIZE );
  Rectangle r;
  for (int i=0; i<numberOfParts; i++ ) {
   r = rects[i];
   g.setColor( Color. GREEN);
   g.fillRect( r.x, r.y, r.width, r.height );
  }
 }
 public void move() {
  int previousX = left;
  int previousY = top;
  int leftSnake = rects[numberOfParts-1].x;
  int topSnake = rects[numberOfParts-1].y;
  if ( direction == SnakeConstants.SNAKE_UP ) {
   top = top - SnakeConstants.SIZE;
  } else if ( direction == SnakeConstants.SNAKE_DOWN ) {
   top = top + SnakeConstants.SIZE;
  } else if ( direction == SnakeConstants.SNAKE_RIGHT ) {
   left = left + SnakeConstants.SIZE;
  } else if ( direction == SnakeConstants.SNAKE_LEFT ) {
   left = left - SnakeConstants.SIZE;
  }
  rects[numberOfParts] = new Rectangle( leftSnake, topSnake, SnakeConstants.SIZE, SnakeConstants.SIZE );
  if ( intersectsBoundary() ) {
   left = previousX;
   top = previousY;
  }
 }
 public void setDirection( int dir ) {
  direction = dir;
 }
 public boolean intersectsBoundary() {
  Rectangle snakeR = new Rectangle( left, top, SnakeConstants.SIZE, SnakeConstants.SIZE );
  return  snakeR.intersects( SnakeConstants.NORTH_BOUNDARY )|| snakeR.intersects( SnakeConstants.SOUTH_BOUNDARY )|| snakeR.intersects( SnakeConstants.WEST_BOUNDARY )|| snakeR.intersects( SnakeConstants.EAST_BOUNDARY ) ;
 }
}
/**
  *
  * @author  Adriana Ferraro
  * @version   S2, 2006
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SnakeJPanel extends JPanel implements ActionListener, MouseListener, KeyListener {
 private Timer t;
 private Snake snake;
 private YumYum yumyum;
 public SnakeJPanel() {
  t = new Timer( 200, this );
  snake = new Snake();
  yumyum = new YumYum();
  addKeyListener( this );
  addMouseListener( this );
 }
//-------------------------------------------------------------------
//-------- Move the Snake object and, after the move, ---------------
//-------- check that it has not collided with the  -----------------
//-------- NoNos.  Also check if the snake --------------------------
//-------- has eaten the YumYum object ------------------------------
//-------------------------------------------------------------------
  public void actionPerformed( ActionEvent e ) {
  snake.move();
  repaint();
  }
//-------------------------------------------------------------------
//-------- Draw all the Snake game objects. -------------------------
//-------------------------------------------------------------------
 public void paintComponent( Graphics g ) {
  super.paintComponent( g );
  Rectangle gameArea = SnakeConstants.GAME_AREA;
 
 

  g.setColor(Color.WHITE);
  g.fillRect( gameArea.x, gameArea.y, gameArea.width, gameArea.height );
  drawGrid(g);
  snake.drawSnake(g);
  yumyum.drawYum(g);
 }
//-------------------------------------------------------------------
//-------- Draw a grid which helps to check that --------------------
//-------- the snake game objects are lined up.  --------------------
//-------------------------------------------------------------------
 private void drawGrid(Graphics g ) {
  int jump = SnakeConstants.SIZE;
  int x = SnakeConstants.TOP_LEFT.x;
  int y = SnakeConstants.TOP_LEFT.y;
  int endX = SnakeConstants.TOP_RIGHT.x;
  int endY = SnakeConstants.TOP_RIGHT.y + SnakeConstants.HEIGHT;
  for( ; x<=endX; ) {
   g.setColor(Color.LIGHT_GRAY);
   g.drawLine( x, y, x, endY );
   if ( x == 0 || x==endX ) {
    g.setColor(Color.BLACK);
    g.drawLine( x, y, x, endY );
   }
   x = x + jump;
  }
  x = SnakeConstants.TOP_LEFT.x;
  for( ; y<=endY; ) {
   g.setColor(Color.LIGHT_GRAY);
   g.drawLine( x, y, endX, y );
   if ( y == 0 || y==endY ) {
    g.setColor(Color.BLACK);
    g.drawLine( x, y, endX, y );
   }
   y = y + jump;
  }
 }
//-------------------------------------------------------------------
//-------- When a key is pressed the snake starts. ------------------
//-------- The arrow keys change the direction of the ---------------
//-------- Snake object. --------------------------------------------
//-------------------------------------------------------------------
 public void keyPressed( KeyEvent e ) {
  t.start();
  int code = e.getKeyCode();
  if ( code == KeyEvent.VK_UP ) {
   snake.setDirection( SnakeConstants.SNAKE_UP );
  } else if (  code == KeyEvent.VK_DOWN ) {
   snake.setDirection( SnakeConstants.SNAKE_DOWN );
  } else if (  code == KeyEvent.VK_LEFT) {
   snake.setDirection( SnakeConstants.SNAKE_LEFT );
  } else if (  code == KeyEvent.VK_RIGHT ) {
   snake.setDirection( SnakeConstants.SNAKE_RIGHT );
  }
 }
 public void keyReleased( KeyEvent newEvent ) { }
 public void keyTyped( KeyEvent newEvent ) { }
   public void mousePressed( MouseEvent newEvent ) {
  requestFocusInWindow();
   }
    public void mouseReleased( MouseEvent newEvent ) { }
    public void mouseClicked( MouseEvent newEvent ) { }
    public void mouseEntered( MouseEvent newEvent ) { }
    public void mouseExited( MouseEvent newEvent ) { }
}
/*
*/

I put the code in Netbeans, but you've still got a few classes to write.
Theres loads of ways of representing the snake.
You could use a double array of ints, then if a square is 0, its empty, if its 1, its part of the snake, 2 could be food.
If your variables are static, which might help for some of them, then you just call them/update them using the class name and '.'.
Not much I can do with your program at the mo, because too much stuff missing and I don't really understand how you're trying to do it exactly.
I recommend keeping variables collected into their own groups, of course sometimes you will need a variable from another class, but if your unnecesarily throwing variables from one class to another things can get messy and its harder to debug that way.
Let me know how you get on

This article has been dead for over six months. Start a new discussion instead.