ASSIGNMENT 5: The Snake Game DEPARTMENT AND COURSE NUMBER COMP 1010 5
Question 4: Eat Apples [5 marks Add code to your program so that whenever the head of the snake hits an apple,
the apple disappears, and the snake grows. Define
a function int searchArrays(int[] x,int[] y, int n, int start, int keyX, int keyY)which will try to find a pair of coordinates (
x[i],
y[i]) in the first
nelements of the arrays
xand
ythat are equal to (
keyX,
keyY).
But start the search at index start, not at index
0. (This is not important in this phase, but this same function will be used again in Question 5.) If
a matching coordinate is found, return the index
iwhere it was found. If none is found, return
-1Define a function
void deleteApple(int eatenApple)which will delete the apple at the given index (
eatenAppleis the index) from the list of apples, reducing the number of apples by 1. Use these two functions whenever the snake moves, to see if the
head of the snake hit an apple, and to remove the apple if it did. Use a global variable
snakeGrowthRateto define how fast the snake grows. This is the number of extra circles to add to the snake whenever an apple is eaten. Try a value of 3 at first. It should be a variable,
not a constant, because in Question 6 it will start to increase. To make the snake grow, you should modify the
moveSnakefunction so that
the circle at the tail does not disappear
every time the snake moves. This will make the snake grow by one circle, or not. The snake must not grow to its full size instantly. It
must grow one circle at a time, when it moves. Use a suitable global variable of some kind to keep track of whether the snake should grow or not, and by how much.
Share with your friends: