• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Not quite programming the centipede game's centipede movement correctly (Java in Eclipse)

 
Greenhorn
Posts: 3
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I haven't taken a look at your code (hint: UseCodeTags), but here is an idea:

Let your centipede consist of a linked list of body segments. Then, when your centipede moves, start at the tail, and update the segment's location to that of the segment before it. Finally, give your centipede's head the new location.

Welcome to JavaRanch!
 
Michael Fiebig
Greenhorn
Posts: 3
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I figured it out. I posted the same problem elsewhere, and someone posted a new way of looking at the problem:

1) Remove the old tail point. (Unless the snake ate 'recently'.)
2) Add a new head point to the end of the existing head point.
3) Leave the other points *the* *same*.

I was overthinking it and tried to make a data type that accounted for each coordinate of the snake and each direction it had to move. This approached solved the problem. This program deals with basic Java animation with overriding the paintComponent method of a JPanel and using a timer, so I can post it when I'm done if anyone wants to see it.
 
Michael Fiebig
Greenhorn
Posts: 3
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I completed version 1.0 of this game and called it "Snake". Here is a link to download the source code if anyone wants to know how to do basic animation in Java, or if anyone would like to improve my code. I think there is probably a more effective approach.

http://www.mediafire.com/?h92md5fz0rmxuzs

This is a link to the video of this game:

http://www.youtube.com/watch?v=9gRG0PH5OAQ
 
reply
    Bookmark Topic Watch Topic
  • New Topic