Assignment 5: The Snake Game



Download 291.77 Kb.
View original pdf
Page6/7
Date10.03.2024
Size291.77 Kb.
#63800
1   2   3   4   5   6   7
Assignment5-COMP1010-Fall2022
ComputerNetworks
startingApples
apples. Use
drawCircles
to draw the apples. Pick your own colour for the apples, but a shade of red would be traditional. Make your program create a set of 5 apples on the grid, and draw them. (But they won’t do anything yet)

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
n
elements of the arrays
x
and
y
that 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
i
where it was found. If none is found, return
-1
Define a function
void deleteApple(int eatenApple)
which will delete the apple at the given index (
eatenApple
is 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
snakeGrowthRate
to 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
moveSnake
function 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.

Download 291.77 Kb.

Share with your friends:
1   2   3   4   5   6   7




The database is protected by copyright ©ininet.org 2024
send message

    Main page