At this point the graphics of the game should be done, so we have to write the logic. It’ll be important for that logic to be done in a very methodical way, otherwise you’ll get quite confused. Let’s go over the general logic for the program and the logic for each specific method:
We will want our snake to move along a path.
Each SnakePart will be placed where its “front” neighbor last was.
So the head sets the path and when the next time tick happens, the user will either make the head move one of 4 directions or the head will continue moving forward.
SnakePart #2, the one directly behind the head, will always move to where the head was in the last tick.
SnakePart #3 will move to where SnakePart #2 last was, and so on down the line.
So what are ticks? Well, it’s a timer that goes off after a set amount of time. So the movement methods themselves just tell the program, we’re going to position things for now and when the “tick” happens, you make sure to redraw everything as we’ve set it up.
Your job here is to:
Finish the moveUp(), moveDown(),moveRight(), and moveLeft() methods. Each one of these will need to only move the head because…
redrawSnake() should take care of redrawing or placing the rest of the body where each of the objects should go. Make sure to add each one of these parts to the canvas again. .getX() and .getY() will be helpful.
Finish method growSnake() where you will add a new SnakePart when this method is called. You don’t have to check if the snake touched the Ball here, that’s for another class to handle.
Create intersectsWithBall() and intersectsWithSnake(). These return true or false depending on whether the head touches any other SnakePart or the ball.