Question
Help with object orientated Java code for movement of snake. I need to implement four methods for up, down, left and right for movement of
Help with object orientated Java code for movement of snake.
I need to implement four methods for up, down, left and right for movement of the snake, well the head part at least that make re-use of a method that calculates a lot of this already. The method in need to call in is :
private void moveTo(int newHeadX, int newHeadY) // provided { int oldHeadX = getHeadXPos(); int oldHeadY = getHeadYPos(); int oldBodyX = getBodyXPos(); int oldBodyY = getBodyYPos(); if (checkX(newHeadX) && checkY(newHeadY)) { head.setXPos(newHeadX); head.setYPos(newHeadY); delay(100); body.setXPos(oldHeadX); body.setYPos(oldHeadY); delay(100); tail.setXPos(oldBodyX); tail.setYPos(oldBodyY); delay(100); } else { System.out.println("Snake cannot cross boundary!"); }
Based on what i know about this, it should be a fairly short piece of code to implement as it mainly uses the above method, but can't for the life of me figure this out.
Here's the question that accompanies it and the diameter of the head movement is 10 :
"In this part of the question you will write methods to move the snake one place, to the right, left, up or down. Remember that when a snake moves, first the head moves (according to which method youre writing), then the body moves to where the head was, then the tail moves to where the body was. Note the delay() method provided for you. This allows a delay to be inserted after each of the head moving, the body moving, and the tail moving. Without this, the movement might be too quick for you to observe.
Consider the provided helper method moveTo(int newHeadX, int newHeadY). This method determines whether or not the position of a head with this x position and yposition would be valid, and if so, moves the head to that position, then moves the body to where the head was, then moves the tail to where the body was, delaying after each move as described above. If there is not enough room for the snake to move to the new position, then the message Snake cannot cross boundary! is output. Uncomment the lines of code in this method now.
Write the public instance method right() which has no arguments and returns no value. This method should calculate the new position of the head, one head diameter to the right of where it currently is, and then move the snake to that position if possible.
Write the public instance method left() which has no arguments and returns no value. This method should calculate the new position of the head, one head diameter to the left of where it currently is, and then move the snake to that position if possible.
Write the public instance method up() which has no arguments and returns no value. This method should calculate the new position of the head, one head diameter above where it currently is, and then move the snake to that position if possible.
Write the public instance method down() which has no arguments and returns no value. This method should calculate the new position of the head, one head diameter below where it currently is, and then move the snake to that position if possible."
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started