Topic 1 - Variables and Data Types
Topic 2 - Conditionals and Strings
Topic 3 - Loops
Topic 4 - Arrays
Topic 5 - File Handling
Semester 1 Projects
Topic 6 - Classes/Objects and Methods
Topic 7 - ArrayLists
Semester Projects

Part 2b: Snake Movement

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:

  1. We will want our snake to move along a path.
  2. Each SnakePart will be placed where its “front” neighbor last was.
  3. 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.
  4. SnakePart #2, the one directly behind the head, will always move to where the head was in the last tick.
  5. 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:

  1. Finish the moveUp(), moveDown(), moveRight(), and moveLeft() methods. Each one of these will need to only move the head because…
  2. 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. 
  3. 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.
  4. Create intersectsWithBall() and intersectsWithSnake(). These return true or false depending on whether the head touches any other SnakePart or the ball.