I am trying to make a snake game and I am wondering how I could get an element of an vector and then use the number for the y coordinate
like when using ararys
lets say the x element I am using is 3
then I could type in arrayY[3] and it would use the x coordinate for the 4th point. how could I do this for a vector. my current code looks like this
private Vector<Integer> snakeX = new Vector<Integer> (5,2);
private Vector<Integer> snakeY = new Vector<Integer> (5,2);
public void startGraphics (){
this.addKeyListener(this);
repaint();
snakeX.add(240);
snakeY.add(240);
snakeX.add(240);
snakeY.add(230);
snakeX.add(240);
snakeY.add(220);
}
public void paintComponent (Graphics g){
super.paintComponent (g);
for (int x : snakeX){
g.fillOval(x, snakeY.elementAt(snakeX.lastIndexOf (x)),10, 10);
System.out.println ("x: " + x + "\t y: " + snakeY.elementAt(snakeX.lastIndexOf (x)));
}
}
in this I add 3 circles. they should print out on top of each other like a snowman, but when I run the program there is only one circle, and it is at the last location. the indexOf(); doesn't work, because the x coordinates are all the same. How would I fix this?
Thanks for any help.