By Centipede, I'm talking about that old game where you control a centipede that can go in any direction, and its body follows, but stays on the path it's already been. For example, if the tip of the centipede is at the bottom right in this picture, and I try to move right one space, this happens:
*******
*
***
After moving right one space:
******
*
****
Note how the tail receded one point and the head gained a point, and the centipede's movement continues like this, and every time it eats something, the size of the centipede increases.
So far, I can move the centipede left, right, and down, but it starts out at two points and the entire body moves. For example:
*
*
And moving right three spaces makes the following (with the centipede's head at bottom):
---*
---*
The minus signs are inserted and represent white space. Notice how the entire body moves to the right rather than the intended goal:
(From this)
*
*
(To this)
When it moves right three spaces:
--**
Again with the minus signs. Note how the tail went down one and right two, as I moved it three spaces, and the movement goes by where the head is, not the tail, so the head went right three spaces and the body followed.
Anyway, here is the source code that is making it happen this way. I don't know where I'm going wrong, but if you find the bug, please let me know. Also, this is a personal project of mine I am doing to reacquaint myself with Java.
(both new_snake and my_snake are a custom data type called "Points" that holds two values--one value is a Point(x, y) and the other is a KeyDirections (which way the centipede is facing) as in UP, DOWN, LEFT, or RIGHT with KeyEvent values). Basically, every part of the centipede has a point on the map and a direction to which way it's going next, to keep track of it. But there's something wrong. The classes for Points and KeyDirections is posted after the "moveSnake" class just below.
"my_panel" is the main panel with the graphics of the board and the centipede (snake). "clearOldSnake" and "updateSnake" are methods in the main panel (my_panel), and are below as well.
private List<Points> my_snake;
private List<Points> new_snake;
private void moveSnake(int direction)
{
boolean skipit = true;
new_snake.clear();
my_panel.clearOldSnake(my_snake);
//If the desired direction is UP and the snake's not going down (can't go down to up, must go left or right first)
if (direction == 38 && my_snake_direction != 40)
{
for (int x = 0; x < my_snake.size(); x++)
{
new_snake.add(new Points(new Point(my_snake.get(x).getPoint().x - 1,
my_snake.get(x).getPoint().y), KeyDirections.UP));
}
}
//Want to go DOWN, but can't if going UP
else if (direction == 40 && my_snake_direction != 38)
{
for (int x = 0; x < my_snake.size(); x++)
{
new_snake.add(new Points(new Point(my_snake.get(x).getPoint().x + 1,
my_snake.get(x).getPoint().y), KeyDirections.DOWN));
}
}
//Want to go LEFT, but can't if going RIGHT
else if (direction == 37 && my_snake_direction != 39)
{
for (int x = 0; x < my_snake.size(); x++)
{
new_snake.add(new Points(new Point(my_snake.get(x).getPoint().x,
my_snake.get(x).getPoint().y - 1), KeyDirections.LEFT));
}
}
//Want to go RIGHT, but can't if going LEFT
else if (direction == 39 && my_snake_direction != 37)
{
for (int x = 0; x < my_snake.size(); x++)
{
new_snake.add(new Points(new Point(my_snake.get(x).getPoint().x,
my_snake.get(x).getPoint().y + 1), KeyDirections.RIGHT));
}
}
else
{
skipit = false;
}
if (skipit)
{
my_snake.clear();
for (int x = 0; x < new_snake.size(); x++)
{
my_snake.add(new_snake.get(x));
}
my_panel.updateSnake(my_snake);
checkForCollision();
}
}
public class Points
{
/*
* "thisPartsPoint" = the coordinates for this part of the snake's body
* "thisPartsDirection" = the direction this part of the snake's body is facing
*/
Point thisPartsPoint = new Point();
KeyDirections thisPartsDirection;
public Points(final Point the_point, final KeyDirections the_direction)
{
thisPartsPoint = the_point;
thisPartsDirection = the_direction;
}
public Point getPoint()
{
return thisPartsPoint;
}
}
public enum KeyDirections
{
UP(38),
DOWN(40),
LEFT(37),
RIGHT(39);
private int my_direction;
private KeyDirections(final int the_direction)
{
setDirection(the_direction);
}
public void setDirection(final int the_direction)
{
my_direction = the_direction;
}
public int getDirection()
{
return my_direction;
}
}
public void clearOldSnake(List<Points> points)
{
System.out.println("" + points.size());
for (int x = 0; x < points.size(); x++)
{
board[points.get(x).getPoint().x][points.get(x).getPoint().y] = Color.GREEN;
}
}
/*
* This updates the position of the snake by points (positions) on the map level
*/
public void updateSnake(List<Points> points)
{
for (int x = 0; x < points.size(); x++)
{
board[points.get(x).getPoint().x][points.get(x).getPoint().y] = Color.YELLOW;
}
repaint();
}
Any help is appreciated. I am sorry for the long post. I've been doing this in Eclipse. If you need me to post other parts of my program here, let me know. This is a personal project I'm working on to reacquaint myself with Java.