of the gridor equal to cellwidth (which is past the right edgesince the rightmost cell coordinate is one less than cellwidththe head has also moved off the grid if the coordinate of the head (which is stored in wormcoords[head][' ']is either - (which is past the top edgeor cellheight (which is past the bottom edgeall we have to do to end the current game is to return out of rungame(when rungame(returns to the function call in main()the next line after the rungame(call (line is the call to showgameoverscreen(which makes the large --game over|text appear this is why we have the return statement on line line loops through every body segment in wormcoords after the head (which is at index this is why the for loop iterates over wormcoords[ :instead of just wormcoordsif both the 'xand 'yvalues of the body segment are the same as the 'xand 'yof the headthen we also end the game by returning out of the rungame(function detecting collisions with the apple check if worm has eaten an apply if wormcoords[head][' '=apple[' 'and wormcoords[head][' '=apple[' '] don' remove worm' tail segment apple getrandomlocation(set new apple somewhere else del wormcoords[- remove worm' tail segment we do similar collision detection check between the head of the worm and the apple' xy coordinates if they matchwe set the coordinates of the apple to random new location (which we get from the return value of getrandomlocation()if the head has not collided with an applethen we delete the last body segment in the wormcoords list remember that negative integers for indexes count from the end of the list so while is the index of the first item in the list and is for the second item- is for the last item in the list and - is for the second to last item the code on lines to (described next in the --moving the worm|sectionwill add new body segment (for the headin the direction that the worm is going this will make the worm one segment longer by not deleting the last body segment when the worm eats an applethe overall length of the worm increases by one but when line deletes the last body segmentthe size remains the same because new head segment is added right afterwards