Hello all

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.

I’m not really sure what you’re asking. Are you asking how to stop using a snake and to use something more like Pac-Man?

It would be just the head of the snake moving round the screen. No tail involved no growing when it eats things. In essence yes like pacman.

You cannot program a game without knowing how to program.
Copying and pasting code will get you no-where.
Learn to program, then in a month, start programming your first game :slight_smile:

I appreciate what your saying there Saucymeatman and I’m the first to say I have got along way to go but at this moment I am trying to learn. I do not expect to program a game for a long time yet what I am doing is playing about with code.

As said in the first post I am reading a book all about that, I have taken game code from one part of the book and thought what would happen if I tried this and how would I go about it. To my mind that is a good way to learn by breaking things and trying to put them back together.

As I have tried for a few days to work it out by adapting the code used in the book and failed as my knowledge is not good enough to see what I’m missing. I am not here asking for someone to rewrite the code for me but to maybe help me see what it is I am missing. Every day I learn something new and I hope to keep on learning.

Rather try to write your own code while learning. The way you are doing it now you will only learn how to hijack other’s code, but not to actually set up an even simple application from scratch.
Make your own so that you really understand what you are doing.

Instead of starting with a solution and trying to work backwards, maybe try it the other way around: can you create an MCVE that demonstrates exactly what you’re confused about?

Start from scratch, and if you really want to use code you found from the internet, then take it one small step at a time. Only add as much code as it takes to show us exactly what you’re confused about. That way we can copy and paste your code into our own editors to see what’s going on.

yep see what you mean KevinWorkman will work on that tomorrow and get back to you thanks all for the replies so far :slight_smile: