Hi there I’m hoping I can get some help here. I have been reading the book Begging Android games second edition. In which a game called Mr Nom (a snake game)is used as an example. I have been working through it and would like to adapt it a bit by taking out the snake and replacing it with a single character.
Now for the problem I cant seem to figure out how to adapt the code to just move a sprite without the tail.
Here is the original code:
public class Snake {
public static final int UP = 0;
public static final int LEFT = 1;
public static final int DOWN = 2;
public static final int RIGHT = 3;
public List<SnakePart> parts = new ArrayList<SnakePart>();
public int direction;
public Snake() {
direction = UP;
parts.add(new SnakePart(5, 6));
parts.add(new SnakePart(5, 7));
parts.add(new SnakePart(5, 8));
}
public void turnLeft() {
direction += 1;
if(direction > RIGHT)
direction = UP;
}
public void turnRight() {
direction -= 1;
if(direction < UP)
direction = RIGHT;
}
public void eat() {
SnakePart end = parts.get(parts.size()-1);
parts.add(new SnakePart(end.x, end.y));
}
public void advance() {
SnakePart head = parts.get(0);
int len = parts.size() - 1;
for(int i = len; i > 0; i--) {
SnakePart before = parts.get(i-1);
SnakePart part = parts.get(i);
part.x = before.x;
part.y = before.y;
}
if(direction == UP)
head.y -= 1;
if(direction == LEFT)
head.x -= 1;
if(direction == DOWN)
head.y += 1;
if(direction == RIGHT)
head.x += 1;
if(head.x < 0)
head.x = 9;
if(head.x > 9)
head.x = 0;
if(head.y < 0)
head.y = 12;
if(head.y > 12)
head.y = 0;
}
public boolean checkBitten() {
int len = parts.size();
SnakePart head = parts.get(0);
for(int i = 1; i < len; i++) {
SnakePart part = parts.get(i);
if(part.x == head.x && part.y == head.y)
return true;
}
return false;
}
}
I understand what all the code means I just cant seem to untangle tail parts.
there is also a snakepart class:
public class SnakePart {
public int x, y;
public SnakePart(int x, int y) {
this.x = x;
this.y = y;
}
}
I have been trying for a few days now to solve this I am very new to this any help would be much appreciated even if its just pointing me in the right direction.
Thanks for taking the time to look at this.